0% found this document useful (0 votes)
101 views6 pages

Nonlinear ODE. Solve The Nonlinear ODE

The document describes using the backward Euler method to solve a nonlinear ordinary differential equation (ODE) numerically. It involves discretizing the ODE using an implicit formulation, which results in a nonlinear algebraic equation that must be solved at each time step using a root-finding method like Newton's method. The document walks through applying Newton's method by hand to solve for the first time step. It also provides MATLAB code that automates applying the backward Euler method for multiple time steps using a root-finding function.

Uploaded by

Marino Potenza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views6 pages

Nonlinear ODE. Solve The Nonlinear ODE

The document describes using the backward Euler method to solve a nonlinear ordinary differential equation (ODE) numerically. It involves discretizing the ODE using an implicit formulation, which results in a nonlinear algebraic equation that must be solved at each time step using a root-finding method like Newton's method. The document walks through applying Newton's method by hand to solve for the first time step. It also provides MATLAB code that automates applying the backward Euler method for multiple time steps using a root-finding function.

Uploaded by

Marino Potenza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

BACKWARD EULER METHOD. Also called implicit Euler method.

Linear and nonlinear ODEs can be solved


with this method. If the ODE is linear, the discretized equations can be solved directly (i.e., yields explicit
algebraic equations) by the 𝑦𝑖+1 . If the ODE is nonlinear, a root finding method must be used to find
𝑦𝑖+1 . This example should give the big picture of what an implicit method is about. Let’s work out a
nonlinear ode example (The same one selected in class).

Nonlinear ODE. Solve the nonlinear ODE:


𝑑𝑦
𝑑𝑡
= (1 + 2𝑡)√𝑦; y(0) = 1; t = [0,1]; ℎ = 0.1 Eq.1

Assuming you will pay attention to all FDM steps, let’s focus on the differences of the current method
with the forward Euler. Construct the discretized ODE using the implicit Euler method:

𝑦𝑗+1 = 𝑦𝑗 + ℎ𝑓(𝑡𝑗+1 , 𝑦𝑗+1 ) Eq.2


Realizing that 𝑓(𝑡𝑗+1 , 𝑦𝑗+1 ) = (1 + 2𝑡𝑗+1 )√𝑦𝑗+1 , then the discretized equation is:

𝑦𝑗+1 = 𝑦𝑗 + ℎ (1 + 2𝑡𝑗+1 )√𝑦𝑗+1 Eq.3 where 𝑡𝑗+1 = 𝑡𝑗 + ℎ

Eq. 3 is a nonlinear algebraic equation which needs to be solved by 𝑦𝑖+1 . A root finding method needs to
be used: Newton’s, secant, fixed-point, etc. Eq.3 was used to develop the matlab code in appendix.

QUICK SOLUTION. For a quick solution by hand, the first-time step (𝑖 = 0) in Eq.3:

𝑦1 = 𝑦𝑜 + ℎ (1 + 2𝑡1 )√𝑦1 ; Eq.4 where 𝑡1 = 𝑡0 + ℎ

In Eq.4 𝑦𝑜 , ℎ, 𝑡1 are known values. Insert them in,

𝑦1 = 1 + 0.1 (1 + 2(0.1))√𝑦1 ; Eq.5 and 𝑡1 = 0 + 0.1

Simplify Eq.5:
𝑦1 = 1 + 0.12 √𝑦1 ; Eq.6 and 𝑡1 = 0.1

Eq.6 is an implicit nonlinear equation, where the task at hand is to solve by 𝑦1 . Newton, secant, fixed-
point or any other root finding methods can be used.

NEWTON’S METHOD (NM). Due to its fans club, let’s start with the Newton’s method:
𝑓
𝑥𝑖+1 = 𝑥𝑖 − 𝑓′𝑖 Eq.7
𝑖
Where 𝑥𝑖+1 is the new root approximation of the function 𝑓𝑖 . This is an iterative method where the first
value of the root 𝑥𝑖 is usually extracted by inspection from a graph of 𝑓𝑖 𝑣𝑠 𝑥𝑖 .

Let’s change the variable notation of the Eq.6, so it looks more familiar (e.g., call the unknown 𝑥 = 𝑦1 ):

𝑥 = 1 + 0.12 √𝑥 or 0 = 1 + 0.12 √𝑥 -x Eq.8

Arrange Eq.8 to get an equation suitable for NM:


𝑓(𝑥) = 1 + 0.12 √𝑥 − 𝑥 Eq.9
In Eq.9 the goal is to get values of x for which 𝑓(𝑥) = 0 is satisfied. The first derivative of the function
(Eq.9) is required in NM:
0.06
𝑓 ′ (𝑥) = −1 + 𝑥 Eq. 10

A plot of the Eq.9 is shown, the oval region is where the root is:

To apply the Eq. 7 we need:


𝑓𝑖
𝑥𝑖+1 = 𝑥𝑖 −
𝑓𝑖′

where:
𝑓𝑖 = 1 + 0.12 √𝑥𝑖 − 𝑥𝑖 Eq. 9
0.06
𝑓𝑖′ = −1 + 𝑥 Eq.10
√ 𝑖

A sample computation of Eq.7 with 𝑖 = 0;

𝑓𝑖 𝑓0 1+0.12 √𝑥𝑜−𝑥𝑜
𝑥𝑖+1 = 𝑥𝑖 − ; ==> 𝑥1 = 𝑥𝑜 − = 𝑥𝑜 − 0.06 Eq.11
𝑓𝑖′ 𝑓0′ −1+
√𝑥𝑜
NM needs an initial estimate of the root. This can be any point close to the root. The obvious is 𝑥𝑜 = 1.
Please note that this is just the initial condition 𝑦0 = 1. The new root shouldn’t be too far away this
value, then:

1+0.12 √𝑥𝑜 −𝑥𝑜 1+0.12 √1−1


𝑥1 = 𝑥𝑜 − 0.06 =1− 0.06 = 1.12766 Eq. 12
−1+ −1+
√𝑥𝑜 √1

The problem doesn’t end here, the value of 𝑥1 = 1.12766 is not the root of Eq. 6 or Eq 8, it just a closer
estimate. Newton’s is an iterative method and requires that the estimated error to be:
𝑒𝑖 = |𝑥𝑖+1 − 𝑥𝑖 | < 𝑒𝑟𝑟𝑜𝑟 𝑡𝑜𝑙𝑒𝑟𝑎𝑛𝑐𝑒 Eq.13

The error tolerance is assumed by the customer. If we assume an error tolerance of 1𝑥10−5 (this means
0.00001). For the current iteration:
𝑒0 = |𝑥1 − 𝑥𝑜 | = |1.12766 − 1.0| = 0.12766 Eq.14

Because 𝑒0 = 0.12766 > 1𝑥10−5 , the process needs to be repeated, assuming that the new root
estimated is now 𝑥1 = 1.12766, thus Eq 7 for a new iteration yields (𝑖 = 1):

1+0.12 √𝑥1 −𝑥1 1+0.12 √1.12766−1.12766


𝑥2 = 𝑥1 − 0.06 = 1.12766 − 0.06 = 1.127415807 Eq.15
−1+ −1+
√𝑥1 √1.12766

The new error is (Eq.13):


𝑒1 = |𝑥2 − 𝑥1 | = |1.127415807 − 1.12766| = 0.000243768 Eq.16

Still 𝑒1 = 0.000243768 > 1𝑥10−5 then, repeat the process until Eq.13 is satisfied:
𝑒𝑖 < 𝑒𝑟𝑟𝑜𝑟 𝑡𝑜𝑙𝑒𝑟𝑎𝑛𝑐𝑒 (1𝑥10−5 )

Better use Excel for the next computations:

i x(i) f(i) f'(i) error(i)


0 1 0.12 -0.94
1 1.127659574 -0.000229994 -0.943498205 0.127659574
2 1.127415807 -7.4443E-10 -0.943492097 0.000243768
3 1.127415806 0 -0.943492097 7.89015E-10

Therefore, the last value of 𝑥 = 1.127415806 in the above table is the root and solves Eq.8, and Eq.6.
This is the value of 𝑦1 for the first-time step in the solution of ODE by Backward Euler in Eq.6.

NEXT TASKS: This means you are ready to compute the next time step. In our matlab code, all the burden
of the root finding step is alleviated by the function fsolve. Professor: didn’t you say ‘quick solution by
hand’? where is the ‘quick’part, shouldn’t be re-labeld as the ‘slow solution by hand’?

Summarizing our results so far:

𝑗 𝑡𝑗 𝑦𝑗
0 0 1
1 0.1 1.127415806
… … …

11 1.0

The equation for the next time step

𝑦2 = 𝑦1 + ℎ (1 + 2𝑡2 )√𝑦2

Where 𝑦1 = 1.127415806 , ℎ = 0.1, and 𝑡2 = 0.2

ASSIGNMENT: Student should:


(i) Work on one more time step (e.g., use Excel for the root finding step)
(ii) Run matlab code to generate the graph (appendix-1) and have a feeling where the root is at.
(iii) Run matlab code in appendix-2 and study the results and graph

APPENDIX-1

% Plot

clc, clear, clf

f=@(x) 1+0.12*sqrt(x)-x;
fplot(f,[0,2]);
grid on
title('1+0.12*sqrt(x)-x');

APPENDIX-2

In the code below four different values of h are used, each new one is half the previous one: ℎ𝑖+1 = 2𝑖.
The nonlinear algebraic equation is solved with the matlab function fsolve. The estimated error is
computed as 𝑒𝑖 = |𝑦𝑖𝑘 − 𝑦𝑖𝑘+1 |, where 𝑘 and 𝑘 + 1 are two successive iterations and 𝑖 is the node label.
Also, GDE is computed with the maximum error in each iteration.

% Backward Euler Method with MATLAB


% Solves IVP-ODE using implicit Euler's method
% Equation to solve: y'=(1+2*t)*sqrt(y); y(0)=1; t=[0,1];
% Author: Marco Arocha; File: EulerBackward.m

clc, clear
close all
clf

% instruction to write results on external file


fid=fopen('EulerOut2.m','w');

% Some parameters and auxiliary variables


h=0.2; a=0; b=1;d=2; dd=1;

% table title
fprintf(fid,'%7s %7s %7s %10s\n','i','t(i)','y(i)','er(i)');
for jj=1:1:4 % Four runs with different h’s.

t = a:h:b; % time vector


y = zeros(1,length(t));

y(1) = 1; % initial condition

fprintf(fid,'-------------------------------------------------\n');
fprintf(fid,' h =%f \n',h);
for ii=1:1:(length(t)-1)
% Root finding with matlab function fzero
yo=y(ii); tn=t(ii+1);
fun=@(yn) yo+h*(1+2*tn)*sqrt(yn)-yn;
y(ii+1)=fzero(fun,yo);
end

% Local Estimated Error + Output


if jj==1
Y=y;
for ii=1:1:numel(t)
fprintf(fid,'%7d %7.2f %7.3f\n',ii, t(ii), y(ii));
end
else
k=1;
for ii=1:d:numel(t)
er(ii)=abs(y(ii)-Y(k));
fprintf(fid,'%7d %7.2f %7.3f %10.3e\n',ii, t(ii), y(ii),er(ii));
k=k+dd;
end
GDE=max(er);
fprintf(fid,'GDE= %.3e \n',GDE);
Y=y;
end
h=h/2.0; % In each new iteration program halves the step size

data=['sb';'+r';'og';'*c']; % 4 different markers


celldata = cellstr(data); % cell array of strings
tt=numel(t), yy=numel(y)
plot(t,y,celldata{jj});
hold on % allows to draw several curves in single plot

if jj>=3, d=2*d; dd=dd*2; end

end

% Chulerias adicionales para la grafica


title('Backward Euler-O(h)---Numerical Solution---');
ylabel('y'); xlabel('t');
grid on
leyendas=[' h1 ';' h2 ';' h3 ';' h4 '];
leyendata=cellstr(leyendas);
legend(leyendata,'Location','northwest');

hold off
fclose(fid);
OUTPUT ('EulerOut2.m') Selected data points are shown:

i t(i) y(i) er(i) i t(i) y(i) er(i)


------------------------------------------------- -------------------------------------------------
h =0.200000 h =0.100000
1 0.00 1.000 1 0.00 1.000 0.000e+00
2 0.20 1.322 3 0.20 1.286 3.574e-02
3 0.40 1.806 5 0.40 1.717 8.894e-02
4 0.60 2.502 7 0.60 2.336 1.658e-01
5 0.80 3.470 9 0.80 3.197 2.729e-01
6 1.00 4.782 11 1.00 4.366 4.168e-01
GDE= 4.168e-01

i t(i) y(i) er(i) i t(i) y(i) er(i)


------------------------------------------------- -------------------------------------------------
h =0.025000
h =0.050000 1 0.00 1.000 0.000e+00
1 0.00 1.000 0.000e+00 5 0.10 1.116 3.548e-03
3 0.10 1.120 7.384e-03 9 0.20 1.262 7.831e-03
5 0.20 1.270 1.634e-02 13 0.30 1.441 1.301e-02
7 0.30 1.454 2.722e-02 17 0.40 1.657 1.927e-02
9 0.40 1.676 4.038e-02 21 0.50 1.917 2.678e-02
11 0.50 1.943 5.619e-02 25 0.60 2.225 3.574e-02
13 0.60 2.261 7.503e-02 29 0.70 2.589 4.632e-02
15 0.70 2.635 9.731e-02 33 0.80 3.015 5.874e-02
17 0.80 3.074 1.234e-01 37 0.90 3.512 7.319e-02
19 0.90 3.585 1.538e-01 41 1.00 4.087 8.988e-02
21 1.00 4.177 1.888e-01 GDE= 1.538e-01
GDE= 1.888e-01

You might also like