# Solution of differential equation by by an iterative method

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
t=0.5
x = 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}]
+a[n-1]*Integrate[t(1-s)(x''[n-1]+(x[n-1])^2-s^4-2),{s,t,1}]
NumberForm[a1 = {Table[x[i], {i, 0, 7}]}, 5]


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

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.

Clear[ndSolveByIterate]
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. • thanks dear. But I cant see the method im suggested in your code. Please Feb 27 at 7:14
• Hi @Junaid , it is there. If you look at the part defining Δx closely enough, you will find it. Feb 27 at 7:16
• Dear @Αλέξανδρος Ζεγγ, CAN I OBTAIN VALUES USING NumberForm[a1 = {Table[x[i], {i, 0, 7}]}, 5] FROM YOUR CODE? 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 = t;
h = 0.7;
Do[
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, but for some reason x (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}] 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...  • Dear @Kim thanks for your kind help. When I paste the code you suggested (x=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, x,...,x and I can get numerical values. Please explain and hel. thanks Feb 27 at 19:39
• @Junaid Oh, I forgot to add ;` at the end of the recursive formula. Now it's gonna work; please try again. Feb 27 at 21:40
• 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. Feb 28 at 10:20
• 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/… Apr 15 at 22:25