1
10.1 Consider the following first-order ODE:
dy
------ = x 2 ⁄ y from x = 0 to x = 2.1 with y ( 0 ) = 2
dx
(a) Solve with Euler’s explicit method using h = 0.7 .
(b) Solve with the modified Euler method using h = 0.7 .
(c) Solve with the classical fourth-order Runge–Kutta method using h = 0.7 .
2x 3
The analytical solution of the ODE is y = -------- + 4 . In each part, calculate the error between the true
3
solution and the numerical solution at the points where the numerical solution is determined.
Solution
(a) The following script file solves the ODE with Euler’s explicit method using h = 0.7 .
% Solving the ODE with Euler’s explicit method.
clear,clc
dF=@ (x,y) x^2/y;
Fsol=@ (x) sqrt(2*x.^3/3+4);
h=0.7; N=3;
x(1) = 0; y(1) = 2;
for i = 1:N
x(i+1) = x(i) + h;
y(i+1) = y(i) + dF(x(i),y(i))*h;
end
x
y
ys=Fsol(x);
error=ys-y
When the program is executed, the following results are displayed in the Command Window:
x =
0 0.7000 1.4000 2.1000
y =
2.0000 2.0000 2.1715 2.8033
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
2
error =
0 0.0564 0.2429 0.3863
(b) The following script file solves the ODE with modified Euler method using h = 0.7 .
clear, clc
% Solving the ODE with the modified Euler’s method.
clear,clc
dF=@ (x,y) x^2/y;
Fsol=@ (x) sqrt(2*x.^3/3+4);
h=0.7; N=3;
x(1) = 0; y(1) = 2;
for i = 1:N
x(i+1) = x(i) + h;
K1=dF(x(i),y(i));
yEU=y(i) + K1*h;
K2=dF(x(i+1),yEU);
y(i+1) = y(i) + (K1+K2)/2*h;
end
x
y
ys=Fsol(x);
error=ys-y
When the program is executed, the following results are displayed in the Command Window:
x =
0 0.7000 1.4000 2.1000
y =
2.0000 2.0858 2.4728 3.2600
error =
0 -0.0294 -0.0584 -0.0704
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
3
(c) The following script file solves the ODE with classical fourth-order Runge–Kutta method using
h = 0.7 .
% Solving the ODE with the classical fourth-order Runge-Kutta method.
clear,clc
dF=@ (x,y) x^2/y;
Fsol=@ (x) sqrt(2*x.^3/3+4);
h=0.7; N=3;
x(1) = 0; y(1) = 2;
for i = 1:N
x(i+1) = x(i) + h;
K1= dF(x(i),y(i));
K2= dF(x(i)+0.5*h,y(i)+K1*h/2);
K3= dF(x(i)+0.5*h,y(i)+K2*h/2);
K4= dF(x(i+1),y(i)+K3*h);
y(i+1) = y(i) + (K1+2*K2+2*K3+K4)*h/6;
end
x
y
ys=Fsol(x);
error=ys-y
When the program is executed, the following results are displayed in the Command Window:
x =
0 0.7000 1.4000 2.1000
y =
2.0000 2.0564 2.4147 3.1902
error =
1.0e-03 *
0 -0.0492 -0.2704 -0.5559
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.