Chapter10 Prob17
Chapter10 Prob17
10.17 Write a user-defined MATLAB function that solves a first-order ODE by applying the classical
third-order Runge–Kutta method, Eqs. (10.82), (10.83). For function name and arguments use
[x,y]=odeRK3(ODE,a,b,h,yINI). The input argument ODE is a name for the function that calcu-
dy
lates ------ . It is a dummy name for the function that is imported into odeRK3. The arguments a and b define
dx
the domain of the solution, h is the step size, and yINI is the initial value. The output arguments, x and y,
are vectors with the x and y coordinates of the solution.
Use the function odeRK3 to solve Problem 10.2. Write a MATLAB program in a script file that
solves the ODE in Problem 10.2 twice, once by using h = 0.8 and once by using h = 0.1 . The program
should also plot the exact solution (given in Problem 10.2) and plot the two numerical solutions (all in the
same figure).
Solution
The listing of the user-defined function odeRK3 is:
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
K2 = ODE(xhalf,yK1);
yK2 = y(i) - K1*h + 2*K2*h;
K3 = ODE(xhalf,yK2);
y(i+1) = y(i) + (K1 + 4*K2 + K3)*h/6;
end
The the user-defined function odeRK3 is used in following program (script file) to solve Problem 8.2.
clear, clc
ODEHW10_2 = @ (x,y) x-x*y/2;
a=1; b=3.4; yINI=1;
h1=0.8; h2=0.1;
[x1, y1] = odeRK3(ODEHW10_2,a,b,h1,yINI);
[x2, y2] = odeRK3(ODEHW10_2,a,b,h2,yINI);
fplot('2-exp((1-x^2)/4)',[1 3.4])
hold on
plot(x1,y1,'s',x2,y2,'*r')
hold off
xlabel('x'); ylabel('y')
legend('Exact','h=0.8','h=0.1',2)
disp('Numerical solution with h=0.8')
Answer=[x1;y1]
disp('Numerical solution with h=0.1')
Answer=[x2;y2]
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:
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
Answer =
Columns 1 through 7
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000
1.0000 1.0508 1.1034 1.1574 1.2121 1.2669 1.3212
Columns 8 through 14
1.7000 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000
1.3747 1.4269 1.4773 1.5257 1.5717 1.6152 1.6560
Columns 15 through 21
2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000
1.6940 1.7292 1.7615 1.7910 1.8178 1.8419 1.8635
Columns 22 through 24
3.1000 3.2000 3.3000
1.8828 1.8998 1.9148>>
2
Exact
1.9 h=0.8
h=0.1
1.8
1.7
1.6
1.5
y
1.4
1.3
1.2
1.1
1
1 1.5 2 2.5 3
x
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.