CH22 Numerical Method
CH22 Numerical Method
CHAPTER 22
22.1 (a) The analytical solution can be derived by the separation of variables,
dy
y
t 3 1.5 dt
t4
ln y 1.5t C
4
Substituting the initial conditions yields C = 0. Substituting this value and taking the exponential gives
t4
1.5t
y e4
t y
0 1
0.25 0.687961
0.5 0.479805
0.75 0.351376
1 0.286505
1.25 0.282339
1.5 0.373673
1.75 0.755577
2 2.718282
t y dy/dt
0 1 -1.5
0.5 0.25 -0.34375
1 0.078125 -0.03906
1.5 0.058594 0.109863
2 0.113525
t y dy/dt
0 1 -1.5
0.25 0.625 -0.92773
0.5 0.393066 -0.54047
0.75 0.25795 -0.2781
1 0.188424 -0.09421
1.25 0.164871 0.074707
1.5 0.183548 0.344153
1.75 0.269586 1.040434
2 0.529695
t y dy/dt tm ym dym/dt
0 1 -1.5 0.25 0.625 -0.92773
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.
2
t y k1 tm ym k2 tm ym k3 te ye k4
0 1.0000 -1.5000 0.25 0.6250 -0.9277 0.25 0.7681 -1.1401 0.5 0.4300 -0.5912 -1.0378
0.5 0.4811 -0.6615 0.75 0.3157 -0.3404 0.75 0.3960 -0.4269 1 0.2676 -0.1338 -0.3883
1 0.2869 -0.1435 1.25 0.2511 0.1138 1.25 0.3154 0.1429 1.5 0.3584 0.6720 0.1736
1.5 0.3738 0.7008 1.75 0.5489 2.1186 1.75 0.9034 3.4866 2 2.1170 13.7607 4.2786
2 2.5131
0
0 0.5 1 1.5 2
22.2 (a) The analytical solution can be derived by the separation of variables,
dy
1 4 x dx
y
2 y x 2 x2 C
Substituting the initial conditions yields C = 2. Substituting this value and rearranging gives
2
2x2 x 2
y
2
x y
0 1
0.25 1.336914
0.5 1.890625
0.75 2.743164
1 4
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.
3
x y dy/dx
0 1 1
0.25 1.25 1.67705
0.5 1.66926 2.584
0.75 2.31526 3.804
1 3.26626 5.42184
Predictor:
k1 (1 2(0)) 1 1
y (0.25) 1 1(0.25) 1.25
k2 (1 2(0.25)) 1.25 1.6771
Corrector:
1 1.6771
y (0.25) 1 0.25 1.33463
2
x y k1 xe ye k2 dy/dx
0 1 1.0000 0.25 1.25 1.6771 1.3385
0.25 1.33463 1.7329 0.5 1.76785 2.6592 2.1961
0.5 1.88364 2.7449 0.75 2.56987 4.0077 3.3763
0.75 2.72772 4.1290 1 3.75996 5.8172 4.9731
1 3.97099
Predictor:
k1 (1 2(0)) 1 1
y (0.1875) 1 1(0.1875) 1.1875
k2 (1 2(0.1875)) 1.1875 1.49837
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.
4
Corrector:
1 2(1.49837)
y (0.25) 1 0.25 1.33306
3
(e) RK4
x y k1 xm ym k2 xm ym k3 xe ye k4
0 1.0000 1 0.125 1.1250 1.32583 0.125 1.1657 1.34961 0.25 1.3374 1.73469 1.3476
0.25 1.3369 1.73436 0.375 1.5537 2.18133 0.375 1.6096 2.2202 0.5 1.8919 2.75096 2.2147
0.5 1.8906 2.74997 0.625 2.2343 3.36322 0.625 2.3110 3.42043 0.75 2.7457 4.14253 3.4100
0.75 2.7431 4.14056 0.875 3.2606 4.96574 0.875 3.3638 5.04368 1 4.0040 6.00299 5.0271
1 3.9998
0
0 0.2 0.4 0.6 0.8 1
RK4 Ralston
Predictor:
k1 2(1) (0) 2 2
y (0.5) 1 (2)(0.5) 0
k2 2(0) 0.52 0.25
Corrector:
2 0.25
y (0.5) 1 0.5 0.5625
2
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.
5
2 (2(0) 0.52 )
yi11 1 0.5 0.5625
2
2 (2(0.5625) 0.52 )
yi21 1 0.5 0.28125
2
2 (2(0.28125) 0.52 )
yi31 1 0.5 0.421875
2
The iterations can be continued until the percent relative error falls below 0.1%. This occurs after 12
iterations with the result that y(0.5) = 0.37491 with a = 0.073%. The remaining values can be computed in
a like fashion to give
t y
0 1.0000000
0.5 0.3749084
1 0.3334045
1.5 0.6526523
2 1.2594796
k1 2(1) (0) 2 2
y (0.25) 1 (2)(0.25) 0.5
k2 2(0.5) 0.252 0.9375
y (0.5) 1 (0.9375)0.5 0.53125
The remainder of the computations can be implemented in a similar fashion as listed below:
t y dy/dt tm ym dym/dt
0 1 -2.0000 0.25 0.5 -0.9375
0.5 0.53125 -0.8125 0.75 0.328125 -0.0938
1 0.48438 0.0313 1.25 0.492188 0.57813
1.5 0.77344 0.7031 1.75 0.949219 1.16406
2 1.35547
k1 2(1) (0) 2 2
y (0.375) 1 ( 2)(0.375) 0.25
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.
6
0.5
0
0 0.5 1 1.5 2
p p0 e
kgt
ln p ln p0 k g t
Therefore, a semi-log plot (ln p versus t) should yield a straight line with a slope of kg. The plot, along with
the linear regression best fit line is shown below. The estimate of the population growth rate is kg =
0.01776/yr.
(b) The ODE can be integrated with the fourth-order RK method with the results tabulated and plotted below:
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.
7
16000
12000
8000
4000
0
1950 1970 1990 2010 2030 2050
22.5 (a) The analytical solution can be used to compute values at times over the range. For example, the
value at t = 1955 can be computed as
12, 000
p 2,560 2,831.54
2,560 (12, 000 2,560)e 0.026(19551950)
Values at the other times can be computed and displayed along with the data in the plot below. The
analytical results are also included as the last column of the table below.
(b) The ODE can be integrated with the fourth-order RK method with the results tabulated and plotted
below:
t p-RK4 k1 pm k2 pm k3 pe k4 p-analytical
1950 2560.00 52.361 2690.9 54.275 2695.7 54.343 2831.7 56.251 54.308 2560
1955 2831.54 56.249 2972.2 58.136 2976.9 58.198 3122.5 60.060 58.163 2831.54
1960 3122.35 60.058 3272.5 61.882 3277.1 61.935 3432.0 63.712 61.901 3122.355
1965 3431.86 63.710 3591.1 65.428 3595.4 65.472 3759.2 67.121 65.439 3431.859
1970 3759.05 67.119 3926.8 68.688 3930.8 68.723 4102.7 70.200 68.690 3759.052
1975 4102.50 70.199 4278.0 71.575 4281.4 71.601 4460.5 72.865 71.569 4102.503
1980 4460.35 72.864 4642.5 74.007 4645.4 74.024 4830.5 75.036 73.994 4460.349
1985 4830.32 75.036 5017.9 75.910 5020.1 75.920 5209.9 76.647 75.890 4830.319
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.
8
1990 5209.77 76.647 5401.4 77.224 5402.8 77.227 5595.9 77.646 77.199 5209.771
1995 5595.77 77.646 5789.9 77.904 5790.5 77.905 5985.3 78.000 77.877 5595.767
2000 5985.15 78.000 6180.2 77.930 6180.0 77.930 6374.8 77.696 77.902 5985.154
2005 6374.66 77.696 6568.9 77.299 6567.9 77.301 6761.2 76.745 77.273 6374.666
2010 6761.03 76.745 6952.9 76.033 6951.1 76.040 7141.2 75.178 76.011 6761.033
2015 7141.09 75.179 7329.0 74.173 7326.5 74.187 7512.0 73.047 74.158 7141.09
2020 7511.88 73.047 7694.5 71.779 7691.3 71.802 7870.9 70.416 71.771 7511.878
2025 7870.73 70.417 8046.8 68.923 8043.0 68.956 8215.5 67.365 68.924 7870.733
2030 8215.35 67.366 8383.8 65.688 8379.6 65.732 8544.0 63.977 65.697 8215.351
2035 8543.84 63.979 8703.8 62.161 8699.2 62.214 8854.9 60.341 62.178 8543.837
2040 8854.73 60.343 9005.6 58.427 9000.8 58.490 9147.2 56.540 58.453 8854.728
2045 9146.99 56.542 9288.3 54.571 9283.4 54.642 9420.2 52.655 54.604 9146.992
2050 9420.01 52.658 9551.7 50.669 9546.7 50.746 9673.7 48.758 50.708 9420.011
10000
8000
6000
4000
2000
0
1950 1970 1990 2010 2030 2050
Thus, the RK4 results are so close to the analytical solution that the two results are indistinguishable
graphically.
dv (6.37 106 ) 2
9.81
dt (6.37 106 x) 2
dx
v
dt
The solution can be obtained with Euler’s method with a step size of 1. Here are the results of the first few
steps.
t v x dv/dt dx/dt
0 1500.000 0 -9.81000 1500.000
1 1490.190 1500.000 -9.80538 1490.190
2 1480.385 2990.190 -9.80080 1480.385
3 1470.584 4470.575 -9.79624 1470.584
4 1460.788 5941.158 -9.79173 1460.788
5 1450.996 7401.946 -9.78724 1450.996
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.
9
160000 2000
x v
120000 1500
80000 1000
40000 500
0 0
0 50 100 150
The maximum height occurs at about 157 s. This result can be made more precise with the following script:
clear,clc
format short g
g=9.81;R=6.37e6;v0=1400;
dt=1/1024;t=0;
v=v0;x=0;
while (1)
if v<=0, break, end
dxdt=v;dvdt=-g*R^2/(R+x)^2;
x=x+dxdt*dt;v=v+dvdt*dt;
t=t+dt;
end
t,x,v
t =
145.75
x =
1.0149e+005
v =
-0.0010876
t y z dy/dt dz/dt
0 2.0000 4.0000 1.00 -16.00
0.1 2.1000 2.4000 0.32 -6.05
0.2 2.1324 1.7952 -0.17 -3.44
0.3 2.1153 1.4516 -0.53 -2.23
0.4 2.0626 1.2287 -0.77 -1.56
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.
10
These slope estimates can then be used to make the prediction for the first step
The remaining steps can be taken in a similar fashion and the results summarized as
t y z
0 2.0000 4.0000
0.1 2.0680 2.8418
0.2 2.0827 2.1938
0.3 2.0576 1.7874
0.4 2.0036 1.5126
5
y-RK z-RK
4 y-Euler z-Euler
3
2
1
0
0 0.1 0.2 0.3 0.4
22.8 The second-order van der Pol equation can be reexpressed as a system of 2 first-order ODEs,
dy
z
dt
dz
(1 y 2 ) z y
dt
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.
11
(a) Euler (h = 0.25). Here are the first few steps. The remainder of the computation would be implemented
in a similar fashion and the results displayed in the plot below.
(b) Euler (h = 0.125). Here are the first few steps. The remainder of the computation would be implemented
in a similar fashion and the results displayed in the plot below.
0
0 2 4 6 8 10
-2
y (h = 0.25) z (h = 0.25)
-4
y (h = 0.125) z (h = 0.125)
22.9 The second-order equation can be reexpressed as a system of two first-order ODEs,
dy
z
dt
dz
4 y
dt
(a) Euler. Here are the first few steps along with the analytical solution. The remainder of the computation
would be implemented in a similar fashion and the results displayed in the plot below.
(b) RK4. Here are the first few steps along with the analytical solution. The remainder of the computation
would be implemented in a similar fashion and the results displayed in the plot below.
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.
12
k1,1 f1 (0,1, 0) z 0
k1,2 f 2 (0,1, 0) 4 y 4(1) 4
y (0.05) 1 0(0.05) 1
z (0.05) 0 4(0.05) 0.2
k2,1 f1 (0.05,1, 0.2) 0.2
k2,2 f 2 (0.05,1, 0.2) 4(1) 4
y (0.05) 1 (0.2)(0.05) 0.990
z (0.05) 0 4(0.05) 0.2
k3,1 f1 (0.05, 0.990, 0.2) 0.2
k3,2 f 2 (0.05, 0.990, 0.2) 4(0.990) 3.96
y (0.1) 1 (0.2)(0.1) 0.980
z (0.1) 0 3.960(0.1) 0.396
k4,1 f1 (0.1, 0.980, 0.396) 0.396
k4,2 f 2 (0.1, 0.980, 0.396) 4(0.980) 3.920
These slope estimates can then be used to make the prediction for the first step
The remaining steps can be taken in a similar fashion and the first few results summarized as
As can be seen, the results agree with the analytical solution closely. A plot of all the values can be
developed and indicates the same close agreement.
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.
13
0
0 1 2 3 4
-2
y-Euler z-Euler
-4
y-RK4 z-RK4
y-anal
-6
22.10 A MATLAB M-file for Heun’s method with iteration can be developed as
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.
14
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;
8000
6000
4000
2000
1950 2000 2050
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
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;
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
8000
6000
4000
2000
1950 2000 2050
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);
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
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.
17
function dy = dydx(x, y)
dy = (1+2*x)*sqrt(y);
1
0 0.2 0.4 0.6 0.8 1
22.13 The following function is patterned on the code for the fourth-order RK method from Fig. 22.8:
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;
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.
18
end
y(1,:) = y0;
for i = 1:n-1
hh = t(i+1) - t(i);
k1 = feval(dydt,t(i),y(i,:))';
y(i+1,:) = y(i,:) + k1*hh;
end
plot(t,y(:,1),t,y(:,2),'--')
This code solves as many ODEs as are specified. Here is the test of the solution of Prob. 22.7. First, a
single M-file holding the differential equations can be written as
function dy = dydtsys(t, y)
dy = [-2*y(1) + 5*y(2)*exp(-t);-y(1)*y(2)^2/2];
>> Prob2213Eulersys_Script
0 2.0000 4.0000
0.1000 3.6000 2.4000
0.2000 3.9658 1.3632
0.3000 3.7307 0.9947
0.4000 3.3530 0.8101
4
3.5
2.5
1.5
0.5
0 0.1 0.2 0.3 0.4
22.14 The following script uses the rk4sys function (Fig. 22.8) to solve the ODEs. The interp1 function is then
used to determine the sum of the squares of the residuals.
clear,clc,clf
tdata=[1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
2006];
Prey=[610 628 639 663 707 733 765 912 1042 1268 1295 1439 1493 1435 1467 1355
1282 1143 1001 1028 910 863 872 932 1038 1115 1192 1268 1335 1397 1216 1313
1590 1879 1770 2422 1163 500 699 750 850 900 1100 900 750 540 450];
Pred=[22 22 23 20 26 28 26 22 22 17 18 20 23 24 31 41 44 34 40 43 50 30 14 23
24 22 20 16 12 12 15 12 12 13 17 16 22 24 14 25 29 19 17 19 29 30 30];
h=0.0625;tspan=[1960:2020];y0=[610 22];
a=0.23;b=0.0133;c=0.4;d=0.0004;
[t y] = rk4sys(@predprey,tspan,y0,h,a,b,c,d);
% create plots
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.
19
subplot(2,2,1);plot(t,y(:,1),tdata,Prey,'o')
title('(a) Prey')
subplot(2,2,3);plot(t,y(:,2),tdata,Pred,'o')
title('(b) Predator')
subplot(2,2,[2 4]);plot(y(:,1),y(:,2))
title('(c) RK4 phase plane plot')
xlabel('Moose'),ylabel('Wolves'),axis square
% determine sum of squares
SSRPred=0;SSRPrey=0;
for i=1:length(tdata)
PreyRK4=interp1(t,y(:,1),tdata(i),'pchip');
SSRPrey=SSRPrey+(Prey(i)-PreyRK4)^2;
end
for i=1:length(tdata)
PredRK4=interp1(t,y(:,2),tdata(i),'pchip');
SSRPred=SSRPred+(Pred(i)-PredRK4)^2;
end
SSRPrey,SSRPred
SSRPrey =
4.5021e+006
SSRPred =
4.8909e+003
(a) Prey
2500
2000
1500
(c) RK4 phase plane plot
1000 35
500 30
0 25
1960 1980 2000 2020
Wolves
20
(b) Predator
50 15
40 10
30 5
500 1000 1500 2000
20 Moose
10
0
1960 1980 2000 2020
function dy = dydtTank(t,y,m,k,c)
dy=[y(2);-(c*y(2)+k*y(1))/m];
end
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.
20
clear,clc,clf
k=20;m=20;
tspan=[0:1/16:15];y0=[1 0];
[t1,y1]=rk4sys(@dydtTank,tspan,y0,1/16,m,k,5);
[t2,y2]=rk4sys(@dydtTank,tspan,y0,1/16,m,k,40);
[t3,y3]=rk4sys(@dydtTank,tspan,y0,1/16,m,k,200);
plot(t1,y1(:,1),t2,y2(:,1),':',t3,y3(:,1),'--')
legend('underdamped','critically damped','overdamped','location','best')
title('Displacements for mass-spring system')
xlabel('t (s)'),ylabel('x (m)')
Output :
0.5
x (m)
-0.5 underdamped
critically damped
overdamped
-1
0 5 10 15
t (s)
dV
CA 2 gH (1)
dt
This equation cannot be solved because it has 2 unknowns: V and H. The volume is related to the depth of
liquid by
H 2 (3r H )
V (2)
3
dV dH
(2 rH H 2 ) (3)
dt dt
This result can be substituted into Eq. (1) to give and equation with 1 unknown,
dH CA 2 gH
(4)
dt 2 rH H 2
A (0.015) 2 0.000707
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.
21
Substituting this value along with the other parameters (C = 0.55, g = 9.81, r = 1.5) into Eq. (4) gives
dH H
0.000548144 (5)
dt 3H H 2
We can solve this equation with an initial condition of H = 2.75 m using the 4th-order RK method with a
step size of 6 s. If this is done, the result can be plotted as shown,
0
0 2000 4000 6000 8000 10000
The results indicate that the tank empties at between t = 7482 and 7488 seconds.
22.17 (a) The temperature of the body can be determined by integrating Newton’s law of cooling to give,
T (t ) To e Kt Ta (1 e Kt )
1 T (t ) Ta
K ln
t To Ta
1 23.5 20
K ln 0.499264 / hr
2 29.5 20
1 T (td ) Ta 1 37 20
td ln ln 1.16556 hr
K To Ta 0.499264 29.5 20
function dy = dTdt(t,T,Ta,K)
dy=-K*(T-Ta);
end
The following script then uses the rk4sys function (Fig. 22.8) to generate the solution and the plot. For
convenience, we have redefined the time of death as t = 0.
clear,clc,clf
[t,y]=rk4sys(@dTdt,[0 4],37,1/16,20,0.499264);
plot(t,y)
xlabel('t (hr)'),ylabel('T (C)')
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.
22
40
35
T (C)
30
25
20
0 0.5 1 1.5 2 2.5 3 3.5 4
t (hr)
22.18 Errata: On the first printing there were several mistakes in the mass balance equations. The
correct equations should be:
dCA1 1
(CA0 CA1 ) kCA1
dt
dCB1 1
CB1 kCA1
dt
dCA2 1
(CA1 CA2 ) kCA2
dt
dCB2 1
(CB1 CB2 ) kCA2
dt
function dc = dCdtReactor(t,C,tau,CA0,k)
dc=[1/tau*(CA0-C(1))-k*C(1); ...
-1/tau*C(2)+k*C(1); ...
1/tau*(C(1)-C(3))-k*C(3); ...
1/tau*(C(2)-C(4))+k*C(3)];
clear,clc,clf
CA0=20;k=0.12;tau=5;
tspan=[0:1/2:10];y0=[0 0 0 0];
[t,C]=rk4sys(@dCdtReactor,tspan,y0,1/16,tau,CA0,k);
plot(t,C(:,1),t,C(:,2),':',t,C(:,3),'--',t,C(:,4),'-.')
legend('CA1','CB1','CA2','CB2','location','best')
title('Reactor concentrations versus time')
xlabel('time (min)'),ylabel('Concentration')
Output :
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.
23
8 CB2
0
0 2 4 6 8 10
time (min)
t C Te
0 1 16
0.0625 0.941257 61.33579
0.125 0.885802 83.19298
0.1875 0.833553 92.53223
0.25 0.784367 95.27129
0.3125 0.738079 94.5932
0.375 0.694529 92.20458
0.4375 0.653556 89.01654
0.5 0.615011 85.51221
0.5625 0.578748 81.94466
0.625 0.544633 78.44362
0.6875 0.512538 75.07279
0.75 0.482341 71.86073
0.8125 0.453932 68.81748
0.875 0.427202 65.94338
0.9375 0.402052 63.23392
1 0.378387 60.68222
100 Te 1
80 C 0.8
60 0.6
40 0.4
20 0.2
0 0
0 1 2 3 4
dy
w
dz
dw f
( L z )2
dz 2 EI
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.
24
z y w dy/dz dw/dz
0 0 0 0 0.004154
1 0 0.004154 0.004154 0.003882
2 0.004154 0.008035 0.008035 0.003618
3 0.012189 0.011654 0.011654 0.003365
4 0.023843 0.015018 0.015018 0.00312
5 0.038862 0.018138 0.018138 0.002885
26 0.78 0.0435 0.0435 7.38E-05
27 0.8235 0.043574 0.043574 4.15E-05
28 0.867074 0.043615 0.043615 1.85E-05
29 0.910689 0.043634 0.043634 4.62E-06
30 0.954323 0.043638 0.043638 0
30 z
20
10
y
0
-1 0 1
22.21 This problem can be approached in a number of ways. The simplest way is to fit the area-depth data
with polynomial regression. A fifth-order polynomial with a zero intercept yields a perfect fit:
This polynomial can then be substituted into the differential equation to yield
dh d2
2 g ( h e)
dt 4(10.0054h5 + 117.8991h 4 389.2741h3 + 275.0587h 2 + 1814.1224h)
This equation can then be integrated numerically. This is a little tricky because a singularity occurs as the
lake’s depth approaches zero. Therefore, the software to solve this problem should be designed to terminate
just prior to this occurring. For example, the software can be designed to terminate when a negative area is
detected. As displayed below, the results indicate that the reservoir will empty in a little over 67,300 s.
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.
25
0
0 10000 20000 30000 40000 50000 60000 70000
function dy = ODEquake(t,y,m1,m2,m3,k1,k2,k3)
dy=[y(4);y(5);y(6);-k1/m1*y(1)+k2/m1*(y(2)-y(1)); ...
k2/m2*(y(1)-y(2))+k3/m2*(y(3)-y(2)); ...
k3/m3*(y(2)-y(3))];
clear,clc,clf
m1=12000;m2=10000;m3=8000;
k1=3000;k2=2400;k3=1800;
tspan=[0:1/32:20];y0=[0 0 0 1 0 0];
[t,y]=rk4sys(@ODEquake,tspan,y0,1/32,m1,m2,m3,k1,k2,k3);
subplot(2,1,1)
plot(t,y(:,1),t,y(:,2),':',t,y(:,3),'--')
legend('x1','x2','x3','location','best')
title('Displacements (m)')
xlabel('time (s)'),ylabel('displacement (m)')
subplot(2,1,2)
plot(t,y(:,4),t,y(:,5),':',t,y(:,6),'--')
legend('dx1/dt','dx2/dt','dx3/dt','location','best')
title('Velocities (m/s)')
xlabel('time (s)'),ylabel('velocity (m/s)')
Output :
Displacements (m)
3
x1
2
displacement (m)
x2
1 x3
-1
-2
0 5 10 15 20
time (s)
Velocities (m/s)
1
0.5
velocity (m/s)
-0.5 dx1/dt
-1 dx2/dt
dx3/dt
-1.5
0 5 10 15 20
time (s)
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.
26
function yp=lorenz(t,y,sigma,b,r)
yp=[-sigma*y(1)+sigma*y(2);r*y(1)-y(2)-y(1)*y(3);-b*y(3)+y(1)*y(2)];
The following script solves the ODEs and generates the plots:
clear,clc,clf
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.
27
10
-10
-20
0 5 10 15 20
0
y
-50 0 0
-20 0 20 -20 0 20 -50 0 50
x x y
40
20
0
z
-20
-40
50
20
0 10
0
-10
y -50 -20 x
22.24 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.
28
clear,clc,clf
tspan=[0 20];y0=[5 5 5];
sigma=10;b=8/3;
[t y] = rk4sys(@lorenz,tspan,y0,0.03125,sigma,b,28);
[t1 y1] = rk4sys(@lorenz,tspan,y0,0.03125,sigma,b,99.96);
subplot(2,2,[1 2])
plot(t,y(:,1),t1,y1(:,1),'--')
title('Lorenz model x versus t');
xlabel('x');ylabel('y')
legend('r = 28','r = 99.96')
subplot(2,2,3)
plot3(y(:,1),y(:,2),y(:,2))
title('r = 28');
xlabel('x');ylabel('y');zlabel('z');grid
subplot(2,2,4)
plot3(y1(:,1),y1(:,2),y1(:,2))
title('r = 99.96');
xlabel('x');ylabel('y');zlabel('z');grid
20
y
-20
-40
0 2 4 6 8 10 12 14 16 18 20
x
r = 28 r = 99.96
50 100
0 0
z
-50 -100
50 100
20 50
0 0
0 0
y -50 -20 x y -100 -50 x
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.