0% found this document useful (0 votes)
65 views25 pages

CH23 Numerical Method

The document discusses numerical methods for solving ordinary differential equations (ODEs). It presents code to solve an ODE modeling a siphon using the ode23, ode23s, and ode113 solvers. It also provides code to model an epidemic using ODEs and the ode23s solver, comparing results with and without population re-susceptibility. Additional sections discuss implementing numerical methods like the Runge-Kutta method and Heun's method to solve example ODEs and analyze the stability and accuracy of solutions.

Uploaded by

배주한
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views25 pages

CH23 Numerical Method

The document discusses numerical methods for solving ordinary differential equations (ODEs). It presents code to solve an ODE modeling a siphon using the ode23, ode23s, and ode113 solvers. It also provides code to model an epidemic using ODEs and the ode23s solver, comparing results with and without population re-susceptibility. Additional sections discuss implementing numerical methods like the Runge-Kutta method and Heun's method to solve example ODEs and analyze the stability and accuracy of solutions.

Uploaded by

배주한
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

1

CHAPTER 23
23.1 Here is a script to implement the computations and create the plot:

global siphon
siphon = 0;
tspan = [0 100]; y0 = 0;
[tp1,yp1]=ode23(@Plinyode,tspan,y0);
subplot(3,1,1),plot(tp1,yp1)
xlabel('time, (s)')
ylabel('water level, (m)')
title('(a) ode23'),grid
[tp1,yp1]=ode23s(@Plinyode,tspan,y0);
subplot(3,1,2),plot(tp1,yp1)
xlabel('time, (s)')
ylabel('water level, (m)')
title('(b) ode23s'),grid
[tp1,yp1]=ode113(@Plinyode,tspan,y0);
subplot(3,1,3),plot(tp1,yp1)
xlabel('time, (s)')
ylabel('water level, (m)')
title('(c) ode113'),grid

23.2 The equations can first be written to account for the fact that recovered individuals can become
susceptible:

dS dI dR
 aSI   R  aSI  rI  rI   R
dt dt dt

Then, we can first develop an M-file to hold these ODEs:

function dy=epidemic(t,y,a,r,rho)
dy=[-a*y(1)*y(2)+rho*y(3);a*y(1)*y(2)-r*y(2);r*y(2)-rho*y(3)];

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

Here is a script to implement the computations and create the plots:

tspan = [0 50]; y0 = [10000 1 0];


[t,y]=ode23s(@epidemic,tspan,y0,[],0.002/7,0.15,0);
subplot(1,2,1),plot(t,y)
xlabel('time, (s)')
ylabel('individuals')
title('(a) time series plot'),grid
legend('susceptible','infected','recovered')
subplot(1,2,2),plot3(y(:,3),y(:,2),y(:,1))
xlabel('infected'),ylabel('recovered'),zlabel('susceptible')
title('(b) phase plane plot'),grid
pause
[t,y]=ode23s(@epidemic,tspan,y0,[],0.002/7,0.15,0.03);
subplot(1,2,1),plot(t,y)
xlabel('time, (s)')
ylabel('individuals')
title('(a) time series plot'),grid
legend('susceptible','infected','recovered')
subplot(1,2,2),plot3(y(:,3),y(:,2),y(:,1))
xlabel('infected'),ylabel('recovered'),zlabel('susceptible')
title('(b) phase plane plot'),grid

Here are the results for the first case where there is no re-susceptibility of the recovered individuals. Notice
how after about 50 days the epidemic has burnt out.

In contrast, when the recovered become susceptible, there is a constant significant level of infected
individuals:

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

23.3 First step:

Predictor:
y10 = 5.222138+[0.5(4.143883)+e2]1 = 3.285532

Corrector:
0.5(4.143883)  e 2  0.5(3.285532)  e 2.5
y11  4.143883  0.5  3.269562
2

The corrector can be iterated to yield

j yi+1j |a|, %
1 3.269562
2 3.271558 0.061

Second step:

Predictor:
y20 = 4.143883+[0.5(3.271558)+e2.5]1 = 2.590189

Predictor Modifier:
y20 = 2.590189+4/5(3.271558-3.285532) = 2.579010

Corrector:
0.5(3.271558)  e 2.5  0.5(2.579010)  e 3
y21  3.271558  0.5  2.573205
2

The corrector can be iterated to yield

j yi+1j |a|, %
1 2.573205
2 2.573931 0.0282

23.4 Before solving, for comparative purposes, we can develop the analytical solution 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.
4

t3
t
y e3

Thus, the true values being simulated in this problem are

t y
0 1
0.25 0.782868
0.5 0.632337

The first step is taken with the fourth-order RK:

k1  f (0,1)  1(0) 2  1  1
y (0.125)  1  1(0.125)  0.875
k2  f (0.125, 0.875)  0.861328
y (0.125)  1  0.861328(0.125)  0.89233
k3  f (0.125, 0.89233)  0.87839
y (0.25)  1  0.87839(0.25)  0.78040
k4  f (0.25, 0.78040)  0.73163
1  2(0.861328  0.87839)  0.73163
  0.86851
6
y (0.25)  1  0.86851(0.25)  0.7828723

This result compares favorably with the analytical solution. The second step can then be implemented with
the non-self-starting Heun method:

Predictor:
y (0.5)  1  (0.7828723(0.25) 2  0.7828723)0.5  0.633028629

Corrector: (First iteration):


0.7339  (0.633028629(0.5) 2  0.633028629)
y (0.5)  0.7828723  0.25  0.63178298
2

Corrector: (Second iteration):


0.7339  (0.63178298(0.5) 2  0.63178298)
y (0.5)  0.7828723  0.25  0.63189976
2

The iterative process can be continued with the final result converging on 0.63188975.

23.5 (a) h < 2/100,000 = 2105.

(b) The implicit Euler can be written for this problem as

 
yi 1  yi  100, 000 yi 1  99,999e ti1 h

which can be solved for

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

yi  99,999eti1 h
yi 1 
1  100, 000h

The results of applying this formula for the first few steps are shown below. A plot of the entire solution is
also displayed

t y
0 0
0.1 1.904638
0.2 1.818731
0.3 1.740819
0.4 1.67032
0.5 1.606531

0
0 1 2

23.6 The implicit Euler can be written for this problem as

yi 1  yi   30(sin ti 1  yi 1 )  3cos ti 1  h

which can be solved for

yi  30sin ti 1h  3cos ti 1h


yi 1 
1  30h

The results of applying this formula are tabulated and graphed below.

t y t y t y t y
0 0 1.2 0.952306 2.4 0.622925 3.6 -0.50089
0.4 0.444484 1.6 0.993242 2.8 0.270163 4 -0.79745
0.8 0.760677 2 0.877341 3.2 -0.12525

1.5

0.5

0
0 1 2 3 4
-0.5

-1

23.7 (a) The explicit Euler can be written for this problem 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.
6

x1,i 1  x1,i   999 x1,i  1999 x2,i  h


x2,i 1  x2,i   1000 x1,i  2000 x2,i  h

Because the step-size is much too large for the stability requirements, the solution is unstable,

t x1 x2 dx1/dt dx2/dt
0 1 1 2998 -3000
0.05 150.9 -149 -147102 147100
0.1 -7204.2 7206 7207803 -7207805
0.15 353186 -353184 -3.5E+08 3.53E+08
0.2 -1.7E+07 17305943 1.73E+10 -1.7E+10

(b) The implicit Euler can be written for this problem as

x1,i 1  x1,i   999 x1,i 1  1999 x2,i 1  h


x2,i 1  x2,i   1000 x1,i 1  2000 x2,i 1  h

or collecting terms

(1  999h) x1,i 1  1999hx2,i 1  x1,i


1000hx1,i 1  (1  2000h) x2,i 1  x2,i

or substituting h = 0.05 and expressing in matrix format

 48.95 99.95  x1,i 1   x1,i 


 50   
 101   x2,i 1   x2,i 

Thus, to solve for the first time step, we substitute the initial conditions for the right-hand side and solve the
2x2 system of equations. The best way to do this is with LU decomposition since we will have to solve the
system repeatedly. For the present case, because its easier to display, we will use the matrix inverse to
obtain the solution. Thus, if the matrix is inverted, the solution for the first step amounts to the matrix
multiplication,

 x1,i 1  1.886088 1.86648  1 3.752568


      
 x2,i 1   0.93371 0.9141 1 1.84781

For the second step (from x = 0.05 to 0.1),

 x1,i 1  1.886088 1.86648  3.752568  3.62878 


    
 x2,i 1   0.93371 0.9141 1.84781 1.81472 

The remaining steps can be implemented in a similar fashion to give

t x1 x2
0 1 1
0.05 3.752568 -1.84781
0.1 3.62878 -1.81472
0.15 3.457057 -1.72938

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

0.2 3.292457 -1.64705

The results are plotted below, along with a solution with the explicit Euler using a step of 0.0005.

4 x1

0
0 0.1 0.2
-2 x2

23.8 (a) The exact solution is

y  Ae5t  t 2  0.4t  0.08

If the initial condition at t = 0 is 0.8, A = 0,

y  t 2  0.4t  0.08

Note that even though the choice of the initial condition removes the positive exponential terms, it still
lurks in the background. Very tiny round off errors in the numerical solutions bring it to the fore. Hence all
of the following solutions eventually diverge from the analytical solution.

(b) 4th order RK. The plot shows the numerical solution (bold line) along with the exact solution (fine line).
15

10

-5
0 1 2 3 4
-10

(c)
function yp = dy(t,y)
yp = 5*(y-t^2);
>> tspan = [0,5];
>> y0 = 0.08;
>> [t,y] = ode45(@dy1,tspan,y0);

(d)
>> [t,y] = ode23s(@dy1,tspan,y0);

(e)
>> [t,y] = ode23tb(@dy1,tspan,y0);

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

30
20
10
0
-10 0 1 2 3 4 5
-20
-30

RK4 Analytical ODE45


ODE23s ODE23tb

23.9 (a) As in Example 19.5, the humps function can be integrated with the quad function as in

>> format long


>> quad(@humps,0,1)

ans =
29.85832612842764

(b) Using ode45 is based on recognizing that the evaluation of the definite integral

b
I  a
f ( x) dx

is equivalent to solving the differential equation

dy
 f ( x)
dx

for y(b) given the initial condition y(a) = 0. Thus, we must solve the following initial-value problem:

dy 1 1
  6
dx ( x  0.3)  0.01 ( x  0.9) 2  0.04
2

where y(0) = 0. To do this with ode45, we must first set up an M-file to evaluate the right-hand side of the
differential equation,

function dy = humpsODE(x,y)
dy = 1./((x-0.3).^2 + 0.01) + 1./((x-0.9).^2+0.04) - 6;

Then, the integral can be evaluated as

>> [x,y] = ode45(@humpsODE,[0 0.5 1],0);


>> disp([x,y])
0 0
0.50000000000000 21.78356481821654
1.00000000000000 29.85525185285369

Thus, the integral estimate is within 0.01% of the estimate obtained with the quad function. Note that a
better estimate can be obtained by using the odeset function to set a smaller relative tolerance as in

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

>> options = odeset('RelTol',1e-8);


>> [x,y] = ode45(@humpsODE,[0 0.5 1],0,options);
>> disp([x,y])
0 0
0.50000000000000 21.78683736423308
1.00000000000000 29.85832514287622

23.10 The nonlinear model can be expressed as the following set of ODEs,

d dv g
v   sin 
dt dt l

where v = the angular velocity. A function can be developed to compute the right-hand-side of this pair of
ODEs for the case where g = 9.81 and l = 0.6 m,

function dy = dpnon(t, y)
dy = [y(2);-9.81/0.6*sin(y(1))];

The linear model can be expressed as the following set of ODEs,

d dv g
v  
dt dt l

A function can be developed as,

function dy = dplin(t, y)
dy = [y(2);-9.81/0.6*y(1)];

Then, the solution and plot can be obtained for the case where (0) = /8. Note that we only depict the
displacement ( or y(1)) in the plot

>> [tn yn] = ode45(@dpnon,[0 10],[pi/8 0]);


>> [tl yl] = ode45(@dplin,[0 10],[pi/8 0]);
>> plot(tn,yn(:,1),tl,yl(:,1),'--')
>> legend('nonlinear','linear')

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

You should notice two aspects of this plot. First, because the displacement is small, the linear solution
provides a decent approximation of the more physically realistic nonlinear case. Second, the two solutions
diverge as the computation progresses.

For the larger initial displacement ((0) = /8), the solution and plot can be obtained as,

>> [tn yn] = ode45(@dpnon,[0 10],[pi/2 0]);


>> [tl yl] = ode45(@dplin,[0 10],[pi/2 0]);
>> plot(tn,yn(:,1),tl,yl(:,1),'--')
>> legend('nonlinear','linear')

Because the linear approximation is only valid at small displacements, there are now clear and significant
discrepancies between the nonlinear and linear cases that are exacerbated as the solution progresses.

23.11 Script:

clear,clc,clf
format compact
opts=odeset('events',@linpendevent);
[ta,ya,tea,yea]=ode45(@linpend,[0 inf],[pi/8 0],opts,1);
tea,yea
[tb,yb,teb,yeb]=ode45(@linpend,[0 inf],[pi/4 0],opts,1);
teb,yeb
[tc,yc,tec,yec]=ode45(@linpend,[0 inf],[pi/2 0],opts,1);
tec,yec
Tpa=4*tea
Tpb=4*teb
Tpc=4*tec
subplot(2,1,1)
plot(ta,ya(:,1),'-',tb,yb(:,1),'--',tc,yc(:,1),':','LineWidth',2)
legend('theta(0)=pi/16','theta(0)=pi/8','theta(0)=pi/4','Location','Best')
xlabel('time (s)');ylabel('theta (rad) and v (m/s)')
subplot(2,1,2)
plot(ta,ya(:,2),'-',tb,yb(:,2),'--',tc,yc(:,2),':','LineWidth',2)
legend('theta(0)=pi/16','theta(0)=pi/8','theta(0)=pi/4','Location','Best')
xlabel('time (s)');ylabel('dtheta/dt (rad/s)')

Supporting functions:

function [detect,stopint,direction]=linpendevent(t,y,varargin)
% Locate the time when height passes through zero

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

% and stop integration.


detect=y(1); % Detect height = 0
stopint=1; % Stop the integration
direction=0; % Direction does not matter

function dydt=linpend(t,y,l)
% y(1) = theta and y(2) = dtheta/dt
grav=9.81;
dydt=[y(2);-grav/l*y(1)];

Results:

tea =
0.5015
yea =
0 -1.2301
teb =
0.5015
yeb =
-0.0000 -2.4602
tec =
0.5015
yec =
-0.0000 -4.9198
Tpa =
2.0059
Tpb =
2.0060
Tpc =
2.0059

2
theta (rad) and v (m/s)

theta(0)=pi/16
theta(0)=pi/8
1
theta(0)=pi/4

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
time (s)

0
dtheta/dt (rad/s)

-2

theta(0)=pi/16
-4
theta(0)=pi/8
theta(0)=pi/4
-6
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
time (s)

23.12 Script:

clear,clc,clf
format compact
opts=odeset('events',@nlinpendevent);
[ta,ya,tea,yea]=ode45(@nlinpend,[0 inf],[pi/8 0],opts,1);
tea,yea
[tb,yb,teb,yeb]=ode45(@nlinpend,[0 inf],[pi/4 0],opts,1);
teb,yeb
[tc,yc,tec,yec]=ode45(@nlinpend,[0 inf],[pi/2 0],opts,1);
tec,yec

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

Tpa=4*tea
Tpb=4*teb
Tpc=4*tec
subplot(2,1,1)
plot(ta,ya(:,1),'-',tb,yb(:,1),'--',tc,yc(:,1),':','LineWidth',2)
legend('theta(0)=pi/16','theta(0)=pi/8','theta(0)=pi/4','Location','Best')
xlabel('time (s)');ylabel('theta (rad) and v (m/s)')
subplot(2,1,2)
plot(ta,ya(:,2),'-',tb,yb(:,2),'--',tc,yc(:,2),':','LineWidth',2)
legend('theta(0)=pi/16','theta(0)=pi/8','theta(0)=pi/4','Location','Best')
xlabel('time (s)');ylabel('dtheta/dt (rad/s)')

Supporting functions:

function [detect,stopint,direction]=nlinpendevent(t,y,varargin)
% Locate the time when height passes through zero
% and stop integration.
detect=y(1); % Detect height = 0
stopint=1; % Stop the integration
direction=0; % Direction does not matter

function dydt=nlinpend(t,y,l)
% y(1) = theta and y(2) = dtheta/dt
grav=9.81;
dydt=[y(2);-grav/l*sin(y(1))];

Results:

tea =
0.5063
yea =
-0.0000 -1.2222
teb =
0.5215
yeb =
-0.0000 -2.3981
tec =
0.5919
yec =
0 -4.4300
Tpa =
2.0254
Tpb =
2.0861
Tpc =
2.3678

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

theta (rad) and v (m/s)


theta(0)=pi/16
theta(0)=pi/8
1 theta(0)=pi/4

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
time (s)

0
dtheta/dt (rad/s)

-2

-4 theta(0)=pi/16
theta(0)=pi/8
theta(0)=pi/4
-6
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
time (s)

23.13 In MATLAB, the first step is to set up a function to hold the differential equations:

function dc = dcdtstiff(t, c)
dc = [-0.013*c(1)-1000*c(1)*c(3);-2500*c(2)*c(3);-0.013*c(1)-1000*c(1)*c(3)-
2500*c(2)*c(3)];

Then, an ODE solver like the function ode45 can be implemented as in

>> tspan=[0,50];
>> y0=[1,1,0];
>> [t,y]=ode45(@dcdtstiff,tspan,y0);

If this is done, the solution will take a relatively long time to compute the results. In contrast, because it is
expressly designed to handle stiff systems, a function like ode23s yields results almost instantaneously.

>> [t,y]=ode23s(@dcdtstiff,tspan,y0);

In either case, a plot of the results can be developed as

>> plot(t,y)

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

23.14 (a) Analytic solution:

y
1
999

1000e  x  e1000 x 
(b) The second-order differential equation can be expressed as the following pair of first-order ODEs,

dy
w
dx
dw
 1000 y  1001w
dx

where w = y. Using the same approach as described in Sec. 26.1, the following simultaneous equations
need to be solved to advance each time step,

yi 1  hwi 1  yi
1000hyi 1  1001hwi 1  wi

If these are implemented with a step size of 0.5, the following values are simulated

x y w
0 1 0
0.5 0.667332 -0.66534
1 0.444889 -0.44489
1.5 0.296593 -0.29659
2 0.197729 -0.19773
2.5 0.131819 -0.13182
3 0.087879 -0.08788
3.5 0.058586 -0.05859
4 0.039057 -0.03906
4.5 0.026038 -0.02604
5 0.017359 -0.01736

The results for y along with the analytical solution are displayed below:

1.2
1
0.8
Implicit numerical
0.6
0.4
0.2 Analytical
0
0 1 2 3 4 5

Note that because we are using an implicit method the results are stable. However, also notice that the
results are somewhat inaccurate. This is due to the large step size. If we use a smaller step size, the results
will converge on the analytical solution. For example, if we use h = 0.125, the results are:

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

1.2
1
0.8
Implicit numerical
0.6
0.4
0.2 Analytical
0
0 1 2 3 4 5

Finally, we can also solve this problem using one of the MATLAB routines expressly designed for stiff
systems. To do this, we first develop a function to hold the pair of ODEs,

function dy = dydx(x, y)
dy = [y(2);-1000*y(1)-1001*y(2)];

Then the following session generates a plot of both the analytical and numerical solutions. As can be seen,
the results are indistinguishable.

x=[0:.1:5];
y=1/999*(1000*exp(-x)-exp(-1000*x));
xspan=[0 5];
x0=[1 0];
[xx,yy]=ode23s(@dydx,xspan,x0);
plot(x,y,xx,yy(:,1),'o')
grid
xlabel('x')
ylabel('y')

23.15 The second-order equation can be composed into a pair of first-order equations as

d dx g
x  
dt dt l

We can use MATLAB to solve this system of equations.

tspan=[0,5]';
x0=[0,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.
16

[t,x]=ode45('dxdt',tspan,x0);
plot(t,x(:,1),t,x(:,2),'--')
grid
title('Angle Theta and Angular Velocity Versus Time')
xlabel('Time, t')
ylabel('Theta (Solid) and Angular Velocity (Dashed)')
axis([0 2 0 10])
zoom

function dx=dxdt(t,x)
dx=[x(2);(9.81/0.5)*x(1)];

23.16 Analytic solution: Take Laplace transform

1000
sX  x (0)  700 X 
s 1
1000
sX  700 X  x(0) 
s 1
x(0) 1000
X  
s  700 ( s  1)( s  700)

Substituting the initial condition and expanding the last term with partial fractions gives,

4 1.430615 1.430615
X   
s  700 s 1 s  700

Taking inverse transforms yields

x  5.430615e700t  1.430615et

MATLAB can be used to plot both the fast transient and the slow phases of the analytical solution.

t=[0:.001:.02];
x=5.430615*exp(-700*t)-1.430615*exp(-t);
plot(t,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.
17

grid
xlabel('t')
ylabel('x')
title('Analytic Solution:Fast Transient')

t=[0.02:.01:5];
x=6.004*exp(-500*t)-2.004*exp(-t);
plot(t,x)
grid
xlabel('t')
ylabel('x')
title('Analytic Solution: Slow Transition')
gtext('6.004e^-500t-2.004 e^-t')

Numerical solution: First set up the function holding the differential equation:

function dx=dxdt(t,x)
dx=-700*x-1000*exp(-t);

The following MATLAB code then generates the solutions and 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.
18

tspan=[0 5];
x0=[4];
[t,x]=ode23s(@dxdt,tspan,x0);
plot(t,x)
grid
xlabel('t')
ylabel('x')
title('Numerical Solution: Fast Transient')
axis([0 .02 -2 4])

tspan=[0 5];
x0=[4];
[t,x]=ode23s(@dxdt,tspan,x0);
plot(t,x)
grid
xlabel('t')
ylabel('x')
title('Numerical Solution: Slow Transition')
%axis([0.02 5 -2 0])

23.17 (a) Analytic solution:

y  1e 10t

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

(b) Explicit Euler

yi 1  yi  (10 yi )h

Here are the results for h = 0.2. Notice that the solution oscillates:

t y dy/dt
0 1 -10
0.2 -1 10
0.4 1 -10
0.6 -1 10
0.8 1 -10
1 -1 10

For h = 0.1, the solution plunges abruptly to 0 at t = 0.1 and then stays at zero thereafter. Both results are
displayed along with the analytical solution below:

Explicit Euler
1.5
h = 0.1 h = 0.2
1
analytical
0.5

0
0 0.2 0.4 0.6 0.8 1
-0.5

-1

-1.5

(c) Implicit Euler

yi
yi 1 
1  10h

Here are the results for h = 0.2. Notice that although the solution is not very accurate, it is stable and
declines monotonically in a similar fashion to the analytical solution.

t y
0 1
0.2 0.333333333
0.4 0.111111111
0.6 0.037037037
0.8 0.012345679
1 0.004115226

For h = 0.1, the solution is also stable and tracks closer to the analytical solution. Both results are displayed
along with the analytical solution 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.
20

1.2
Implicit Euler
1
h = 0.1
0.8
0.6 h = 0.2
0.4
0.2
analytical
0
0 0.2 0.4 0.6 0.8 1

23.18 We can develop an M-file to compute the ODEs,

function yp = predpreylog(t,y,a,b,c,d,K)
yp = [a*(1-y(1)/K)*y(1)-b*y(1)*y(2);-c*y(2)+d*y(1)*y(2)];

The following script employs the ode45 function to generate the solution.

a=1.2;b=0.6;c=0.8;d=0.3;
[t y] = ode45(@predpreylog,[0 40],[2 1],[],a,b,c,d,1e8);
subplot(2,2,1);plot(t,y(:,1),t,y(:,2),'--')
title('(a) time series plot (K = 1e8)')
subplot(2,2,3);plot(y(:,1),y(:,2))
title('(b) phase plane plot (K = 1e8)')
[t y] = ode45(@predpreylog,tspan,y0,[],a,b,c,d,200);
subplot(2,2,2);plot(t,y(:,1),t,y(:,2),'--')
title('(c) time series plot (K = 200)')
subplot(2,2,4);plot(y(:,1),y(:,2))
title('(d) phase plane plot (K = 200)')

Two things are indicated by these 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.
21

1. The period of the oscillations seems to be unaffected by the introduction of a carrying capcity effect.
2. The amplitudes of the oscillations decrease with time when a meaningful carrying capacity is imposed..

The second result might suggest a further question of whether or not the oscillations would eventually
stabilize. This can be investigated by extending the integration interval as in the following script:

a=1.2;b=0.6;c=0.8;d=0.3;
[t y] = ode45(@predpreylog,[0 400],[2 1],[],a,b,c,d,200);
subplot(2,1,1);plot(t,y(:,1),t,y(:,2),'--')
title('(a) time series plot (K = 200)')
subplot(2,1,2);plot(y(:,1),y(:,2))
title('(b) phase plane plot (K = 200)')

The resulting plot indicates that the solution does not have stable oscillations but seems to be converging on
stable constant populations.

23.19 The second-order equations can be expressed as the following system of first-order ODEs,

dx1 dx2
 x3  x4
dt dt
dx3 k k
  1  x1  L1   2 ( x2  x1  w1  L2 )
dt m1 m1
dx4 k2
2
  ( x2  x1  w1  L2 )
dt m2

We can develop an M-file to compute them,

function dx = dxdtProb2117(t,x,k1,k2,m1,m2,w1,w2,L1,L2)
dx = [x(3);x(4);-k1/m1*(x(1)-L1)+k2/m1*(x(2)-x(1)-w1-L2); ...
-k2/m2*(x(2)-x(1)-w1-L2)];

The following script employs the ode45 function to generate the solution.

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

k1=5;k2=5;m1=2;m2=2;w1=5;w2=5;L1=2;L2=2;
[t,x]=ode45(@dxdtProb2117,[0 20],[2 15 0 0],[],k1,k2,m1,m2,w1,w2,L1,L2);
subplot(2,1,1);plot(t,x(:,1),t,x(:,2),'--')
grid;title('displacements');legend('x1','x2')
subplot(2,1,2);plot(t,x(:,3),t,x(:,4),'--')
grid;title('velocities');legend('v1','v2')
pause
subplot(1,1,1),plot(x(:,2),x(:,1))
xlabel('x2');ylabel('x1')

23.20 The following function can be used to hold the ODEs:

function dx = dxdtProb2320(t,x,k1,k2,m1,m2,w1,w2,L1,L2)
dx = [x(3);x(4);-k1/m1*(x(1)-L1)+k2/m1*(x(2)-x(1)-w1-L2); ...

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

-k2/m2*(x(2)-x(1)-w1-L2)];

The following script employs the ode45 function to generate the solution and then generates the power
spectrum for the first mass’s displacement:

clear,clf,clc
k1=5;k2=5;m1=2;m2=2;w1=5;w2=5;L1=2;L2=2;
[t,x]=ode45(@dxdtProb2320,[0:1/16:32],[2 15 0 0],[],k1,k2,m1,m2,w1,w2,L1,L2);
subplot(2,1,1);plot(t,x(:,1))
grid;title('(a) Displacement of first mass')
n=length(t)-1; dt=(max(t)-min(t))/n;fs=1/dt;nyquist=fs/2;
Y=fft(x(:,1))/n;
Y(1)=[];
% compute and display the power spectrum
f = (1:n/2)/(n/2)*nyquist;
Pyy = abs(Y(1:n/2)).^2;
subplot(2,1,2);
bar(f,Pyy)
title('(b) Power spectrum')
xlabel('Frequency (Hz)');

(a) Displacement of first mass


displacement (m)

10

-5
0 5 10 15 20 25 30 35
time (s)
(b) Power spectrum
2

0
0 2 4 6 8 10
Frequency (Hz)

The power spectrum indicates peaks at 0.1563 and 0.4063 Hz. This result can be validated by determining
the systems eigenvalues:

A=[k1/m1+k2/m1 -k2/m1;-k2/m2 k2/m2];


lambda=eig(A);
freq=sqrt(lambda)/(2*pi)

freq =
0.1555
0.4072

23.21 The following function can be used to hold the ODEs:

function dx = dxdtProb2321(t,x,m1,m2,m3,k1,k2,k3)
dx=[x(4);x(5);x(6);-k1/m1*x(1)+k2/m1*(x(2)-x(1)); ...
k2/m2*(x(1)-x(2))+k3/m2*(x(3)-x(2)); ...
k3/m3*(x(2)-x(3))];

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

The following script employs the ode45 function to generate the solution and then generates the power
spectrum for the floor’s displacement:

clear,clf,clc
m1=12000;m2=10000;m3=8000;
k1=3000;k2=2400;k3=1800;
tspan=[0:1/8:128];y0=[0 0 0 1 0 0];
[t,x]=ode45(@dxdtProb2321,tspan,y0,[],m1,m2,m3,k1,k2,k3);
subplot(2,1,1);plot(t,x(:,1))
xlabel('time (s)');ylabel('displacement (m)');
grid;title('(a) Displacement of first floor')
n=length(t)-1; dt=(max(t)-min(t))/n;fs=1/dt;nyquist=fs/2;
Y=fft(x(:,1))/n;
Y(1)=[];
% compute and display the power spectrum
f = (1:n/2)/(n/2)*nyquist;
Pyy = abs(Y(1:n/2)).^2;
subplot(2,1,2);
bar(f(1:n/32),Pyy(1:n/32))
title('(b) Power spectrum')
xlabel('Frequency (Hz)');
% Use eigenvalues to determine fundamental frequencies

(a) Displacement of first floor


displacement (m)

-2
0 20 40 60 80 100 120 140
time (s)
(b) Power spectrum
0.2

0.1

0
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35
Frequency (Hz)
The power spectrum indicates peaks at 0.0390625, 0.09375, and 0.1328125 Hz. This result can be validated
by determining the systems eigenvalues:

A=[(k1+k2)/m1 -k2/m1 0; ...


-k2/m2 (k2+k3)/m2 -k3/m2; ...
0 -k3/m3 k3/m3];
lambda=eig(A);
freq=sqrt(lambda)/(2*pi)

freq =
0.1330
0.0927
0.0380

23.22 Script:

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.
25

opts=odeset('events',@endevent2322);
y0=[-200 -20];
[t,y,te,ye]=ode45(@freefall,[0 inf],y0,opts,0.25,68.1);
te,ye
subplot(2,1,1)
plot(t,-y(:,1),'LineWidth',2)
title('height (m)')
ylabel('x (m)'),grid
subplot(2,1,2)
plot(t,-y(:,2),'LineWidth',2)
title('velocity (m/s)')
xlabel('time (s)');ylabel('v (m/s)'),grid

Supporting functions:

function dydt=freefall(t,y,cd,m)
% y(1) = x and y(2) = v
grav=9.81;
dydt=[y(2);grav-cd/m*y(2)*abs(y(2))];

function [detect,stopint,direction]=endevent2318(t,y,varargin)
% Locate the time when velocity is zero at maximum
% and stop integration.
detect=y(2); % Detect height = 0
stopint=1; % Stop the integration
direction=0; % Direction does not matter

Results:

te =
1.9456
ye =
-218.9975 0.0000

(a) height (m)


220
x (m)

210

200
0 0.5 1 1.5 2
(b) velocity (m/s)
20

10
v (m/s)

-10
0 0.5 1 1.5 2
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.

You might also like