I want to solve a second order differential equation in the interval[-1:1], which does not have a analytic solution, \begin{eqnarray} y''(x) &=& k \phi^2(x)y(x) \\ \phi(x) &=& \frac{1}{2}\left(1-\sin\left[ \frac{\pi}{2}x\right] \right)\\ y'[1] &=& 1 \\ y[1] &=& 1 \end{eqnarray}
It is possible to get numerical solution of the equation using NDSolve. But I want to approach the solution iteratively starting with a Initial guess function for example, $y_0(x) = \frac{1}{2}(1+x)$ with appropriate boundary conditions, \begin{eqnarray} y_{n+1}''(x) &=& k\phi^2(x)y_n(x) \end{eqnarray}
To do this I have following code,
ClearAll;
k = 3955/100;
W = 1
phi[x_] := (1/2)*(1 - Sin[(Pi/2)*x/W])
vE[x_] := Piecewise[{{0, -W <= x < 0}, {x/W, 0 <= x <= W}}];
pcw[x_] := (W + x)/(2*W);
so = NDSolve[{u''[x] == k*phi[x]*phi[x]*u[x], u[-W] == 0, u[W] == 1},
u, {x, -W, W}, WorkingPrecision -> 22, InterpolationOrder -> All]
dd[x_] = Q[x] /.
First@DSolve[{Q''[x] == k*phi[x]*phi[x]*pcw[x], Q[W] == 1,
Q'[W] == 1}, Q, x];
Plot[Evaluate[{dd[t], u[t] /. so, vE[t], pcw[t]}], {t, -W, W}]
dd[x_] = Q[x] /.
First@DSolve[{Q''[x] == k*phi[x]*phi[x]*dd[x], Q[W] == 1,
Q'[W] == 1}, Q, x];
Plot[Evaluate[{dd[t], u[t] /. so, vE[t], pcw[t]}], {t, -W, W}]
dd[x_] = Q[x] /.
First@DSolve[{Q''[x] == k*phi[x]*phi[x]*dd[x], Q[W] == 1,
Q'[W] == 1}, Q, x];
Plot[Evaluate[{dd[t], u[t] /. so, vE[t], pcw[t]}], {t, -W, W}]
dd[x_] = Q[x] /.
First@DSolve[{Q''[x] == k*phi[x]*phi[x]*dd[x], Q[W] == 1,
Q'[W] == 1}, Q, x];
Plot[Evaluate[{dd[t], u[t] /. so, vE[t], pcw[t]}], {t, -W, W}]
Which is certainly not elegant way getting things done and also it is taking too long to run than I was expecting. How can I use recursion in this case?
The numeric solution is:
Plot[u[x] /. so, {x, -W, W}]
ClearAll;
accomplishes nothing; maybe you wantClear["Global
*"]. 2) Each of your definitions of
pcw` overrides the previous one, so that only the last definitionpcw[x_] := (W + x)/(2*W)
remains in effect at the end. Did you intend that? $\endgroup$Do
,While
,Nest
,NestWhile
etc. Nevertheless, I'm afraid it's hard to speed up the code if you wantdd[x]
to be analytic. BTW, shouldn't thephi[x] phi[t]
bephi[x]^2
? $\endgroup$