# How can I use the Newton-Raphson Method to solve nonlinear Differential Equations in Mathematica?

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?

• There is an exact solution y=1. What is the method for? Feb 6, 2019 at 20:47
• 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 Feb 6, 2019 at 20:53
• Exact solution is y[x]=Tanh[x] Feb 7, 2019 at 8:13
• 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. Feb 7, 2019 at 8:21
• NDSolve accommodates a variety of ODE solution algorithms. Feb 8, 2019 at 14:56

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]]

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}]]]

• Dear Alex, thanks, but in above is differential equation you solved? Feb 6, 2019 at 21:58
• @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. Feb 6, 2019 at 23:32
• 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} Feb 7, 2019 at 4:53
• 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? Feb 7, 2019 at 12:04
• 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? Feb 7, 2019 at 12:08

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]]

• Dear Ulrich, thanks I know NDsolve, but still looking for N-R code.. Feb 7, 2019 at 8:36
• 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? Feb 7, 2019 at 9:17
• Dear Ulrich, I want to solve nonlinear differential equation by Newton-Raphson methid which is usually used to solve nonlinear algebraic equation...Thanks Feb 7, 2019 at 11:44
• 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? Feb 7, 2019 at 11:52
• 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} Feb 7, 2019 at 11:58