of 5
1
10.20 The user-defined MATLAB function Sys2ODEsRK2(ODE1,ODE2,a,b,h,yINI,zINI)
(Program 10-5), that is listed in the solution of Example 10-7, solves a system of two ODEs by using the
second-order Runge–Kutta method (modified Euler version). Modify the function such that the two ODEs
are entered in one input argument. Similarly, the domain should be entered by using one input argument,
and the two initial conditions entered in one input argument. For function name and arguments use
[t,x,y]=Sys2ODEsModEU(ODEs,ab,h,INI). The input argument ODEs is a name (dummy name
h0.05=
2
t(1) = ab(1); x(1) = INI(1); y(1) = INI(2);
N = round((ab(2) - ab(1))/h);
for i = 1:N
t(i+1) = t(i) + h;
K1 = ODEs(t(i),x(i),y(i));
K2 = ODEs(t(i+1),x(i)+K1(1)*h,y(i)+K1(2)*h);
h=0.1;
[t, x, y] = Sys2ODEsModEu(@ODEsHW10_5,ab,h,INI);
fplot('exp(-2*t)*(8*exp(5*t)-3)/5',[0 1.2])
hold on
fplot('2*exp(-2*t)*(2*exp(5*t)+3)/5',[0 1.2],'g')
plot(t,x,'*r')
plot(t,y,'sr')
hold off
3
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
dxydt(1)=2*x+2*y;
dxydt(2)=2*x-y;
When the program is executed the numerical solution is displayed in the Command Window and the fol-
lowing plot is displayed in the Figure Window:
Answer =
6.6692 8.8131 11.7248 15.6643 20.9818 28.1496
60
x
Exact x
Exact y
4
(b) The ODE in Problem 8.10 is:
The second-order equation is transformed to a system of two first-order ODEs.
Define , . With these definitions, the system of two first-order ODEs is:
h=0.05;
[x, y, w] = Sys2ODEsModEu(@ODEsHW10_11,ab,h,INI);
plot(x,y)
xlabel('x'); ylabel('y')
The listing of the user-defined function ODEsHW10_11 (the system of ODEs in Problem 8.5) that is used in
the solution is:
x0= 0.2=
wdy
dx
------
=
dw
dx
-------d2y
dx2
--------
=
5
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
x
y