3
$\begingroup$

Suppose we want to solve y'+y^2=1,y(0)=0,0⩽x⩽1 by using the Newton-Raphson method which usually used to solve the nonlinear algebraic equation by:

xn=xn−1− f(xn−1)/ f′(xn−1);

How can we do it for ODE?

$\endgroup$
5
  • $\begingroup$ There is an exact solution y=1. What is the method for? $\endgroup$ Feb 6, 2019 at 20:47
  • $\begingroup$ Hi Alex, sorry I correct the initial condition y(0)=0; I just want to know how can we apply Newton-Raphson method to nonlinear ODE in general. I was given this example just to make it clear to me for simple example...thanks $\endgroup$
    – user62716
    Feb 6, 2019 at 20:53
  • $\begingroup$ Exact solution is y[x]=Tanh[x] $\endgroup$ Feb 7, 2019 at 8:13
  • $\begingroup$ Dear Ulrich, yes I know, but my question to implement the Newton-Raphson method for nonlinear ODE as you know practical problem does not has an exact solution. $\endgroup$
    – user62716
    Feb 7, 2019 at 8:21
  • $\begingroup$ NDSolve accommodates a variety of ODE solution algorithms. $\endgroup$
    – bbgodfrey
    Feb 8, 2019 at 14:56

2 Answers 2

4
$\begingroup$

An example of using the method

y[0] = 1; dx = 1/100; 
f[y_] := 2*y^2 - y^3 + 10*Cos[2*y] - 2*Sin[3*y]; a = 1/2;
sol = Table[{i*dx, 
    y[i] = y1 /. 
      FindRoot[(y1 - y[i - 1])/dx + (a*f[y1] + (1 - a)*f[y[i - 1]]) ==
         0, {y1, y[i - 1]}]}, {i, 1, 100}];

Here we call FindRoot[],and it uses Newton's method as default. Compare with another numerical method

eq = ys'[x] + f[ys[x]] == 0;
sol1 = NDSolveValue[{eq, ys[0] == 1}, ys, {x, 0, 1}, MaxSteps -> 10^6]

Show[Plot[sol1[x], {x, 0, 1}, PlotRange -> All, 
  PlotLabel -> Row[{"a=", a*1.}], PlotStyle -> Red], ListPlot[sol]]

fig1

Use FixedPointList[] to find the root by the Newton method, then the code for solving a differential equation $y'+y^2-1=0,y(0)=0$ is

newtonsMethodList[f_, x0_, n_] := 
 With[{iter = # - f[#]/D[f[#], #]}, FixedPointList[iter &, x0, n]]
dx = 1/100; a = .5; x[0] = 0; y[0] = 0; F[y_] := y^2 - 1

Do[f[y1_] := (y1 - y[i - 1])/dx + (a*F[y1] + (1 - a)*F[y[i - 1]]); 
 x[i] = dx*i; 
 y[i] = Last[newtonsMethodList[f, y[i - 1], 30]];, {i, 1, 100}]

Show[Plot[Tanh[x], {x, 0, 1}, PlotStyle -> Orange], 
 ListPlot[Table[{x[i], y[i]}, {i, 0, 100}]]]

fig2

$\endgroup$
15
  • $\begingroup$ Dear Alex, thanks, but in above is differential equation you solved? $\endgroup$
    – user62716
    Feb 6, 2019 at 21:58
  • $\begingroup$ @user62716 The differential equation y'[x]+2*y[x]^2 - y[x]^3 + 10*Cos[2*y[x]] - 2*Sin[3*y[x]] ==0 is solved. I specifically chose a nonlinear function when integrating which some methods fail. $\endgroup$ Feb 6, 2019 at 23:32
  • $\begingroup$ Hi Alex, your code is confuse me, can we modify the below code for nonlinear ode( I found it in current web.):f[x_] := x^2 - 2*x + 1 newtonsMethodList[f_, x0_, n_] := With[{iter = # - f[#]/D[f[#], #]}, FixedPointList[iter &, x0, n]] x = newtonsMethodList[f, 0.5, 10] {0.5, 0.75, 0.875, 0.9375, 0.96875, 0.984375, 0.992188, 0.996094, \ 0.998047, 0.999023, 0.999512} f[x] {0.25, 0.0625, 0.015625, 0.00390625, 0.000976563, 0.000244141, \ 0.0000610352, 0.0000152588, 3.8147*10^-6, 9.53674*10^-7, 2.38419*10^-7} $\endgroup$
    – user62716
    Feb 7, 2019 at 4:53
  • $\begingroup$ Do you want to adapt the method of solving algebraic equations to solve differential equations? There are many algorithms. I showed one that uses the Newton method by calling 'FindRoot[]'. But you want to use FixedPointList? $\endgroup$ Feb 7, 2019 at 12:04
  • $\begingroup$ Dear Alex, as you can see before fixedpointlist the N-R method form already written:newtonsMethodList[f_, x0_, n_] := With[{iter = # - f[#]/D[f[#], #]}, FixedPointList[iter &, x0, n]] ; can we do it to my original nonlinear ODE? $\endgroup$
    – user62716
    Feb 7, 2019 at 12:08
0
$\begingroup$

What about an iterative approach which uses Newton method?

Series of righthand side of ode near y[t]=y0[t] `y'[t]=1-y[t]^2~1+y0[t]^2-2 y0[t] y[t]

The approximative ode is linear. Iterative solution gives a very good approximation

NestList[NDSolveValue[{y'[t] == 1 + #^2 - 2 # y[t], y[0] == 0},y[t], {t, 0, 1}] &, 0, 3]
Show[Plot[% , {t, 0, 1}],Plot[Tanh[t] , {t, 0, 1},PlotStyle->Red]]

enter image description here

$\endgroup$
9
  • $\begingroup$ Dear Ulrich, thanks I know NDsolve, but still looking for N-R code.. $\endgroup$
    – user62716
    Feb 7, 2019 at 8:36
  • $\begingroup$ I didn't get the basic purpose of your question. What is the relation between the Newton iteration xn==... and the ode you gave as an example? $\endgroup$ Feb 7, 2019 at 9:17
  • $\begingroup$ Dear Ulrich, I want to solve nonlinear differential equation by Newton-Raphson methid which is usually used to solve nonlinear algebraic equation...Thanks $\endgroup$
    – user62716
    Feb 7, 2019 at 11:44
  • $\begingroup$ That's clear, but you didn't tell how you want to apply the method. If you want a method like the one Trounev presented in his answer , you take an implicit method (Gauss, midpoint, ???) and try to solve it with Newton-Raphson? $\endgroup$ Feb 7, 2019 at 11:52
  • $\begingroup$ Dear Ulrich, I want it as we when solve the nonlinear algebraic equation:ClearAll["Global`*"] f[x_] := x^2 - 2*x + 1 newtonsMethodList[f_, x0_, n_] := With[{iter = # - f[#]/D[f[#], #]}, FixedPointList[iter &, x0, n]] x = newtonsMethodList[f, 0.5, 10] {0.5, 0.75, 0.875, 0.9375, 0.96875, 0.984375, 0.992188, 0.996094, \ 0.998047, 0.999023, 0.999512} f[x] {0.25, 0.0625, 0.015625, 0.00390625, 0.000976563, 0.000244141, \ 0.0000610352, 0.0000152588, 3.8147*10^-6, 9.53674*10^-7, 2.38419*10^-7} $\endgroup$
    – user62716
    Feb 7, 2019 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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