Let $y^{\prime\prime}(t)+y^{2}(t)-t^{4}-2=0$, where $0\leq t\leq 1$. The boundary conditions are $y(0)=0$ and $y(1)=1$ and so its exact solution is $y(t)=t^{2}$. Now I want to compute the solution by the following method; $x_{n}=x_{n-1}+\alpha_{n-1}\int_{0}^{t}s(1-t)[x^{\prime\prime}_{n-1}(s)+x^{2}_{n-1}(s)-s^{4}-2]ds+\alpha_{n-1}\int_{t}^{1}t(1-s)[x^{\prime\prime}_{n-1}(s)+x^{2}_{n-1}(s)-s^{4}-2]ds$ $n=0,1,2,..$ where the initial guess is $x_{0}(t)=t$. I have set the following code for $t=0.5$ so the exact solution in this case is $y(t)=0.5^{2}=0.25$ and $x_{0}(t)=t=0.5$. Now I set the following code:

Clear[x,s, T, a]

a[n_] := a[n] = 0.70
x[0] = 0.5;
x[n_] := x[n] = x[n-1]+a[n-1]*Integrate[s(1-t)(x''[n-1]+(x[n-1])^2-s^4-2),{s,0,t}]
NumberForm[a1 = {Table[x[i], {i, 0, 7}]}, 5]

and when I run the mathemtica I found some error. Please help me.

  • 3
    $\begingroup$ You define x as a sequence. But x'' does only exists for a continuous function, not a sequence. $\endgroup$ Feb 26 at 19:38
  • $\begingroup$ Dear, I have try the problem too much and in different ways, but I cant succeeded. $\endgroup$
    – Junaid
    Feb 26 at 21:10
  • $\begingroup$ A discrete approximation of x'' would be: x''[n]= x[n-1]-2 x[n]+x[n+1] $\endgroup$ Feb 27 at 8:24
  • $\begingroup$ BTW, I wonder if the posted iterative method bears a name or not. $\endgroup$ Mar 2 at 3:42

2 Answers 2


Below the trick is to use Interpolation, which can transform discrete values into a continuous function, so that the latter can be Derivatived directly and the difficulty mentioned by @DaneilHuber in the comment is overcome.

ndSolveByIterate[tsamplings_, xinitfunc_] := 
Module[{xinit = xinitfunc /@ tsamplings, α = .7, Δx, iterate, zero = 1.*^-4},
  Δx[t_, xlast_] := 
  Module[{xfunc = Interpolation[{tsamplings, xlast}\[Transpose]], integrand},
    integrand[s_] := xfunc''[s] + xfunc[s]^2 - s^4 - 2;
    α (NIntegrate[s (1 - t) integrand[s], {s, 0, t}] + 
       NIntegrate[t (1 - s) integrand[s], {s, t, 1}])
  iterate = # + ParallelTable[Δx[t, #], {t, tsamplings + zero}] &;
  NestWhile[iterate, xinit, Mean[Abs[# - #2]] > zero &, 2]

BTW, replacing NestWhile in the last step with NestWhileList, one can see datas generated in each iterative step.

enter image description here

  • $\begingroup$ thanks dear. But I cant see the method im suggested in your code. Please $\endgroup$
    – Junaid
    Feb 27 at 7:14
  • $\begingroup$ Hi @Junaid , it is there. If you look at the part defining Δx closely enough, you will find it. $\endgroup$ Feb 27 at 7:16
  • $\begingroup$ Dear @Αλέξανδρος Ζεγγ, CAN I OBTAIN VALUES USING NumberForm[a1 = {Table[x[i], {i, 0, 7}]}, 5] FROM YOUR CODE? $\endgroup$
    – Junaid
    Feb 27 at 9:55

There is no iteration in your code, and you're specifying the index of x as its variable, so the integrations inside the recursive formula will try to integrate over the index, which isn't correct. You could try the following:

x[0] = t;
h = 0.7;
    n = i;
    xs = x[n] /. t -> s;
    f = D[xs, {s, 2}] + xs^2 - s^4 - 2;
    x[n + 1] = x[n] + h*Integrate[s*(1 - t)*f, {s, 0, t}] + h*Integrate[t*(1 - s)*f, {s, t, 1}];
    Clear[n, xs, f];
, {i, 0, 7}]

You should expect that this might take some time, because the order of x and the number of terms increase exponentially with the order of 2, which also means that you're gonna need a very high numeric precision to compute properly. You can set the global precision by adding the following in the beginning:

$MinPrecision = 200;
$PreRead = (# /. s_String /; StringMatchQ[s, NumberString] && Precision@ToExpression@s == MachinePrecision :> s <> "`200." &);

For my PC, it took less than a second to get x[7], but for some reason x[8] (and higher) is taking forever (most likely due to precision overhead). Nevertheless, it yields the correct answer:

TableForm@Table[x[i] /. t -> 1/2, {i,0,7}]

enter image description here

UPDATE. If you prefer not to care about precision entirely and compute everything with infinite precision, then just define every number as rational form (such as h = 0.7 to h = 7/10). In such a case...

enter image description here

enter image description here

  • $\begingroup$ Dear @Kim thanks for your kind help. When I paste the code you suggested (x[0]=t upto {i,0,7}] then I get nothing. When I add TableForm@Table[x[i]/. t:>1/2,{i,0,7] then i get 1/2. x[1], x[2],...,x[7] and I can get numerical values. Please explain and hel. thanks $\endgroup$
    – Junaid
    Feb 27 at 19:39
  • $\begingroup$ @Junaid Oh, I forgot to add ; at the end of the recursive formula. Now it's gonna work; please try again. $\endgroup$
    – Shin Kim
    Feb 27 at 21:40
  • $\begingroup$ Thanks Dear Kim. Your suggested code now runs in my mathemtica. I have add the term x[(n+1)_]:= before x[n+1]. However, now the code works well for x8 but fails to go furhter. I have also add precision by adding them as u suggested. So what should I do next to successed in the furhter iterations. $\endgroup$
    – Junaid
    Feb 28 at 10:20
  • $\begingroup$ Thanks Dears Daniel Shin Kin for your kind help. I will b more thankful if u provide me a help to my new question at mathematica.stackexchange.com/questions/264403/… $\endgroup$
    – Junaid
    Apr 15 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.