You are on page 1of 4

14

2.0450
2.0500

9.1440
9.4173

The following plot is generated


10000

8000

6000

4000

2000
1950

2000

2050

22.11 A MATLAB M-file for the midpoint method can be developed as


function [t,y] = midpoint(dydt,tspan,y0,h)
% [t,y] = midpoint(dydt,tspan,y0,h):
%
uses the midpoint method to integrate an ODE
% input:
%
dydt = name of the M-file that evaluates the ODE
%
tspan = [ti, tf] where ti and tf = initial and
%
final values of independent variable
%
y0 = initial value of dependent variable
%
h = step size
% output:
%
t = vector of independent variable
%
y = vector of solution for dependent variable
ti = tspan(1);
tf = tspan(2);
t = (ti:h:tf)';
n = length(t);
% if necessary, add an additional value of t
% so that range goes from t = ti to tf
if t(n)<tf
t(n+1) = tf;
n = n+1;
end
y = y0*ones(n,1); %preallocate y to improve efficiency
for i = 1:n-1
hh = t(i+1) - t(i);
k1 = feval(dydt,t(i),y(i));
ymid = y(i) + k1*hh/2;
k2 = feval(dydt,t(i)+hh/2,ymid);
y(i+1) = y(i) + k2*hh;
end
plot(t,y)

Here is the test of the solution of Prob. 22.5. First, an M-file holding the differential equation is written as
function dp = dpdt(t, p)
dp = 0.026*(1-p/12000)*p;

Then the M-file can be invoked and the results plotted with the following script
PROPRIETARY MATERIAL. The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual
may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the
publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their
individual course preparation. If you are a student using this Manual, you are using it without permission.

15

clear,clc,clf
[t,p]=midpoint(@dpdt,[1950 2050],2560,5);
disp([t',p])
plot(t',p)
1950
1955
1960
1965
1970
1975
1980
1985
1990
1995
2000
2005
2010
2015
2020
2025
2030
2035
2040
2045
2050

2560
2831.4
3122
3431.4
3758.5
4102
4459.8
4829.8
5209.4
5595.5
5985
6374.7
6761.2
7141.3
7512.2
7871.1
8215.7
8544.1
8854.9
9147
9419.9

10000

8000

6000

4000

2000
1950

2000

2050

22.12 A MATLAB M-file for the fourth-order RK method can be developed as


function [t,y] = rk4(dydt,tspan,y0,h)
% [t,y] = rk4(dydt,tspan,y0,h):
%
uses the fourth-order Runge-Kutta method to integrate an ODE
% input:
%
dydt = name of the M-file that evaluates the ODE
%
tspan = [ti, tf] where ti and tf = initial and
%
final values of independent variable
%
y0 = initial value of dependent variable
%
h = step size
% output:
%
t = vector of independent variable
%
y = vector of solution for dependent variable
ti = tspan(1);
tf = tspan(2);
t = (ti:h:tf)';
n = length(t);
PROPRIETARY MATERIAL. The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual
may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the
publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their
individual course preparation. If you are a student using this Manual, you are using it without permission.

16
% if necessary, add an additional value of t
% so that range goes from t = ti to tf
if t(n)<tf
t(n+1) = tf;
n = n+1;
end
y = y0*ones(n,1); %preallocate y to improve efficiency
for i = 1:n-1
hh = t(i+1) - t(i);
k1 = feval(dydt,t(i),y(i));
ymid = y(i) + k1*hh/2;
k2 = feval(dydt,t(i)+hh/2,ymid);
ymid = y(i) + k2*hh/2;
k3 = feval(dydt,t(i)+hh/2,ymid);
yend = y(i) + k3*hh;
k4 = feval(dydt,t(i)+hh,yend);
phi = (k1+2*(k2+k3)+k4)/6;
y(i+1) = y(i) + phi*hh;
end
plot(t,y)

Here is the test of the solution of Prob. 22.2. First, an M-file holding the differential equation is written as
function dy = dydx(x, y)
dy = (1+4*x)*sqrt(y);

Then the M-file can be invoked and the results plotted with the following script
clear,clc,clf
[x,y]=rk4(@dydx,[0 1],1,0.1);
disp([x,y])
plot(x,y)
0
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
0.9000
1.0000

1.0000
1.1236
1.2996
1.5376
1.8496
2.2500
2.7556
3.3856
4.1616
5.1076
6.2500

7
6
5
4
3
2
1
0

0.2

0.4

0.6

0.8

22.13 The following function is patterned on the code for the fourth-order RK method from Fig. 22.8:

PROPRIETARY MATERIAL. The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual
may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the
publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their
individual course preparation. If you are a student using this Manual, you are using it without permission.

This!can!be!plotted!as!follows:!