0% found this document useful (0 votes)
118 views

Chapter 14 Differential Equations - 2009 - A Guide To Microsoft Excel 2007 For Scientists and Engineers

This document discusses numerical methods for solving differential equations, including: 1) Euler's method, which approximates the solution of a differential equation by breaking it into small steps and approximating the curve as a series of straight lines. 2) An example applying Euler's method to solve the differential equation dy/dx = xy with initial value y(0) = 1, comparing the approximate solution to the exact solution. 3) How decreasing the step size improves the accuracy of the approximation in Euler's method.

Uploaded by

yonaye
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)
118 views

Chapter 14 Differential Equations - 2009 - A Guide To Microsoft Excel 2007 For Scientists and Engineers

This document discusses numerical methods for solving differential equations, including: 1) Euler's method, which approximates the solution of a differential equation by breaking it into small steps and approximating the curve as a series of straight lines. 2) An example applying Euler's method to solve the differential equation dy/dx = xy with initial value y(0) = 1, comparing the approximate solution to the exact solution. 3) How decreasing the step size improves the accuracy of the approximation in Euler's method.

Uploaded by

yonaye
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/ 14

14

Differential Equations
Differential equations occur in many physical problems. Let us
look at some simple examples.

(i) A body falling through the air is subjected to two forces:


gravity acting downward and air resistance acting upward.
The first force is constant, but the second is proportional to
the body's velocity. This gives rise to the first-order
differential equation
dv 2
m-=g-kv (14.1)
dt
(ii) Consider the chemical reaction A + B --+ C where the rate of
reaction is proportional to the concentration of A and to the
concentration of B. Letx be the amount of A and B reacted at
time t, and let the initial concentration of A and B be a and b,
respectively. These quantities will be related by
dx
dt =k2 ( a - x ) ( b - x ) (14.2)

(iii) The equation of motion for a harmonic oscillator is


d 2x
-2-+CVX= 0 (14.3)
dt
Equations 14.1 and 14.2 are examples of first-order differential
equations, while Equation 14.3 is of second order. The equations
in these examples may readily be solved by analytical means. The
next equation is also first order, but its solution is rather more
difficult to arrive at:
dy = x+ Y
(14.4)
dx x-y

When a differential equation is difficult or impossible to solve


analytically, we may use numerical methods to find approximate
solutions.

Consider the simple equation dvIdt =9 for a falling body when air
resistance is ignored. This integrates to give v =gt + c where cis
the integration constant and 9 is a constant of known value. Thus
we do not have a unique solution, since any value of c will satisfy
248 A Guide to Microsoft Excel 2007 for Scientists and Engineers

the differential equation. By inspection of the solution we see that


c is the value ofv when t equals zero. We need to know this value
in order to uniquely solve the equation. In general, to solve dy/d»
=j(x,y) over the x range [a, b], we need to know the value ofy(a),
which is called the initial value. Problems of this type are called
initial value problems. With second-order differential equations
two integration constants arise. For an initial value problem we
need to know the initial value of the two values of the dependent
variables. Alternatively, the problem may be defined by specifying
some conditions at one value of x and others at another value of x.
Such problems are called boundary value problems.

Exercise 1: Euler's Euler developed a method for finding the approximate solution to
initial value problems. Let the differential equation to be solved
Method have the form of Equation 14.5 and letthe initial value ofy beyo'

: = y' = !(x,y) (14.5)

Letthe solution (i.e., the integral of Equation 14.5) have the form
of Equation 14.6.
Y = g(x,Y) (14.6)

Consider the two curves in Figure 14.1 where f(x) is the


differential to be solved and g(x) is the integrated solution.

f(x)
Y'o

Yo
x,
xc .......:..:..:.------ xo Xl

Figure 14.1
From Equation 14.5, we may calculate any value y'I'y'Z' ...,y'n' We
already know the value ofYo - the initial value. Our task is to find
values for YI'Yz, ..·,Yn· Integration of Equation 14.5 from Xo to Xl
yields Equation 14.7.
Xl

Yl = Yo + J !(xo,yo)dx (14.7)
Differential Equations 249

The second term on the right is the area under the curve f(xJl)
between the two x-values. Euler approximated this to the area of
the rectangle defined by y'O,y'l' x o, and Xl' The approximate value
ofy, is then given by Equation 14.8, or by Equation 14.9 when the
x increment is represented by h.
YI = Yo + (Xl - xo)f(xo, Yo) (14.8)
Yl = Yo + hf(xo,Yo) (14.9)

Having found an approximation for Yl' we may now find an


approximation for Yz
Y 2 = Y1 + hf(xpY1 ) (14.10)

In general, the value of the approximation at one point is found


from the previous one using
Yn+l = Yn + hf(xn,yJ (14.11)

In this Exercise, Euler's method is used to find an approximate


solution of the differential equation dy/d» = xy, with the initial
value yeO) = 1. The approximation is compared to the analytical
solutiony = exp(x2j2).

Figure 14.2

(a) Open a new workbook. Enter the text shown on A1:ES of


Figure 14.2. Enter the values in A6:A11.

(b) In C2 enter 0.1 for the value of h. Narne the cell as h.

(c) In B6 enter 0 , the initial value of x. In C6 enter 1.0 for the


initial value ofy from the conditiony(O) = 1; this corresponds
to the first term on the right of Equation 14.11 when n = O.
250 A Guide to Microsoft Exce/2007 for Scientists and Engineers

(d) InD6 enter =h*(B6*C6). This corresponds to the second term


on the right in Equation 14.11 when n = O. The parentheses
are not essential here but are used to make it clear that we
are computing the value h*function.

(e) In B7 enter =B6 + h to incrementx. In C7we compute the first


approximation of y with =C6 + D6. This corresponds to the
left-hand side of Equation 14.11. Copy D6 down to D7.

(f) Copy the cells B7:D7 down to row 11. This computes the
successive y approximations. Since we shall not be using the
last value of h*f(x,y), delete Dl1.

(g) So that we can compare our approximations in the Ccolumn


with the exact solution, in E6 enter =EXP(B6"'2 / 2) and copy
this down to Ell. Save the workbook as Chap14.xlsm.

1.14

1.12

1.10

1.08

1.06

1.04

1.02

1.00

0.98
0 0.1 0.2 0.3 0.4 0.5

Figure 14.3

Clearly, our answer in the Ccolumn is not in very good agreement


with the exactvalues in the E column. Abetter approximation may
be obtained by reducing the size of h, the increment for the
x-values. Figure 14.3 graphs the exact solution, the
approximations with h = 0.1 and those with h = 0.05. This shows
that (i) the deviation from the exact values increases with each
iteration of Equation 14.11 as expected, and (ii) decreasing the
size of h significantly improves the solution values.

(h) Modify your worksheet to find the approximations of this


differential equation for x-values from 0 to 0.5 with steps of
0.025. Save the workbook.
Differential Equations 251

In this example we have used the "crude" Euler method in which


the integral in Equation 14.7 was approximated to the area of a
rectangle. In the modified Euler method it is approximated to a
trapezoid. Compared to the original Euler method, this requires
fewer calculations for comparable accuracy. We shall not examine
the modified method. The next Exercise uses a more modern
method.

Exercise 2: The Like the Euler method, the Runge-Kutta methods finds that an
approximation for y is based on the previous value. These
Runge-Kutta Method mathematicians developed a number of algorithms to solve
differential equations. We shall use the fourth-order Runge-Kutta
The fourth-order method is method, the derivation of which is beyond the scope of this book.
sometimes called the The iterative formula is given in Equation 14.12.
Kutta-Simpson formula since, Y n-] = Y n +i(k] +2k2 +2k3 +k4 )
when the right-hand side of k. = hf(xn,yJ
the differential equation is a
k 2 = hf t x; +fh,Yn +fk]) (14.12)
function of xalone, it reduces
to Simpson's 113 rule. k 3 = hf t x; +fh,Yn +fk2 )
k 4 =hf(xn +h'Yn +k3 )
In the next chapter, a
user-defined function is This may look somewhat formidable so let us see how we can put
developed to perform the it into a worksheet. We have seen in Exercise 1 how to evaluate
Runge-Kutta computation. the equivalentto k 1; this is the value of the differential function for
various x- andy-values. The second parameter, k 2, is similar except
that the x-value is incremented by h while the y-value is
incremented by k 1 . Each parameter increments y by a multiple of
the parameter preceding it.

In the previous Exercise we solved dyjdx =xy with the initial value
yeO) = 1 using Euler's method. Here we solve the same problem
using the Runge-Kutta method so that we may compare the
results.

(a) Open the workbook Chap14.xlsm and make Sheet2 active.


Enter the text shown on A1:HS of Figure 14.4.

(b) Name the cell C4 as h.

(c) Enter the series of values in A6:A11 and B6:B11.

(d) In C6 enter the value 1.0. This is the initial conditiony(O) = 1


and corresponds to the first term to the right in Equation
14.12 for n = O.
252 A Guide to Microsoft Exce/2007 for Scientists and Engineers

(e) Enter these formulas to compute the k parameters:


D6: =h*(B6 * C6)
E6: =h*((B6 + h/2) * (C6 + D6/2))
F6: =h*((B6 + h/2) * (C6 + E6/2))
G6: =h*((B6 + h) * (C6 + F6))

We have shown that for the


equation dy/dx = xy, the
Runge-Kutta method is clearly
far superior to Euler's
method. It may be shown that
this is true for all equations.
Figure 14.4

(f) In C7 enter =C6 + (l/6)*(D6 + 2*E6 + 2*F6 + G6) to compute


the first approximation. Compare this formula with Equation
14.12.

(g) Copy the cells D6:G6 to D7:G7.Then copy C7:G7down to row


10 and copy Cl0 to Cl1. This computes the successive y
approximations.

(h) To compute the error in our approximations enter in H6 =C6


-EXP(B6"2 / 2), format as shown and copy down to Hll. Save
the workbook Chap14.xlsm.

Exercise 3: Solving In the previous Exercise we solved dy/dx =xy. We would need to
make many edits to the worksheet to solve for another equation
with a User-Defined dy/dx =j(x,y). If we put the function jlxy) in a module, we need
Function edit only the module (and the initial value) to change our
worksheet.

In this Exercise we find the values ofy that satisfy the equation
dy/dx = l/(x + y) with the initial valuey(O) = 2. We use x-values
from 0 to 1.0 in increments of 0.2.
Differential Equations 253

The completed worksheet


(Figure 14.5) is very similar to
that in the previous exercise.
To save time, you may wish to
copy that worksheet to a new
sheet and edit it to agree
with the instructions below.
Any of the Copy and Paste
methods may be used to
duplicate the workbook.
Alternatively, hold down [Ctrl]
and drag the tag of the sheet
to be copied to the right. Figure 14.5
When [Ctrl] is released, a copy
of the sheet is inserted into
the workbook. Right click the (a) Open the VBE and insert a module on the Chap14 project
tag and rename it. Enter this function on the module sheet:
Function RKfunc(x, y)
RKfunc = 1 / (x + y)
End Function

(b) On Sheet3 enter the text and values shown inA1:G7 of Figure
14.5. Name the cells C4:C6 using the text in B4:B6.

(c) Enter the series of values in A8:A13.


Enter these formulas:
B8: =xO The initial x value
C8: =yO The initialy value
D8: =h*rkfunc(B8, C8) The k parameters
E8: =h*rkfunc ((B8 + h/2), (C8 + D8/2))
F8: =h*rkfunc ((B8 + h/2), (C8 + E8/2))
G8: =h*rkfunc ((B8 + h), (C8 + F8))

(d) In B9 enter =B8+h to incrementx.

(e) In C9 enter =C8 + (l/6)*(D8 + 2*E8 + 2*F8 + G8) to compute


the first approximation for y.

(f) Copy D8:G8 to line 9. In row 9, we have the secondy-value,


and the k values needed to compute the thirdy-value.

(g) Copy B9:G9 down to line 13 to compute the successive y-


values. Save the workbook.
254 A Guide to Microsoft Exce/2007 for Scientists and Engineers

If your values do not agree with Figure 14.5 you need to check the
function in the module and the formulas on the worksheet
Remember that formulas can be displayed with [Ctrll+[J. To check
the function, move to a blank cell such as A20 and enter
=rkfunc(3,l). This should return the value 0.25.

(h) Now that you have solved one equation, modify the module
sheet and the values in the named cells of the worksheet to
solve the equation dy/d» =x 2 +Y with the initial valuey(l) =
1. Find the value ofy when x = 1.5 using first h = 0.1, then h =
0.01. You will need to extend the worksheet in the second
case. The analytical resultto eight decimal places isy(1.5) =
2.64232762.

Simultaneous and Consider a pair of simultaneous equations having the form:


y' = g(x,y,z)
Second-Order u' = f(x,y,z)
(14.13)
Differential Equations The Runge-Kutta formulas for these equations are given in
Equation 14.14.

Yn-j = Y n +i(kj +2k2 +2k3 +k4)


un_ j = un + i(qj + 2q2 + 2q3 + q4)
k, = hg(xn,Yn,uJ
qj = hf(xn,Yn, uJ
k 2 =hg(xn +fh,Yn +fkl' un +fqj)
q2 = hf t x; +fh,Yn +fkj,un +fqj) (14.14)
k 3 =hg(xn +fh,Yn +fk2,un +fq2)
q3 = hf t x; + fh,Yn + f k2' Un + fq2)
k, = hgt x; + n.», + k 3,Un + q3)
q4 =hf(xn +h'Yn +k3,un +q3)

Equations of order greater than one may be solved by


transforming them into sets of simultaneous equations. For
example, to solve y" =ay' + by + c, we make the substitutiony' =u.
The introduction of the auxiliary variable u allows us to write the
second-order equations as two simultaneous equations:
y'=u
(14.15)
u' = au+by+c
Differential Equations 255

Comparing Equations 14.14 and 14.15, we see thatg is a function


only of u and is a very simple function: it has the value of u. This
simplifies the k terms in Equation 14.14:
k, =h(uJ
k 2 =h(un ++qj)
k 3 = htu; + +q2) (14.16)
k4 = htu; + q3)

Exercise 4: Solving a In this Exercise we apply the equations developed above to solve:
y" =y' + y =sin(x)
Second-Order with boundary conditionsy(O) = 0 andy'(O) = 0
Equation
Our task is to obtain approximate values ofy andy' when x = 1.
With the substitutiony' = u, we get a pair of equations:
y' = u initial value yeO) = 0
u' =sin(x) - y - u initial value u(O) = 0

Comparing these with Equation 14.13, we see thatg =u, so we will


use the simplified kvalues of Equation 14.16. We also see thatf =
sin(x) - y - u.

The function f is referenced in each of the q terms, so it will be


more convenient to use a module function. Furthermore, by
changing the module you will be able to use the same worksheet
for another function.

Figure 14.6

(a) With Chap14.xlsm open, go to the VBE and insert a new


module. For this exercise, code the function:
Function f(x, y, u)
f = Sin(x) - y - u
End Function
256 A Guide to Microsoft Exce/2007 for Scientists and Engineers

(b) Move to Sheet4 and enter the text and values shown in Al :K6
of Figure 14.6.

(c) SelectA3:D4 and name the cells in row 4.

(d) The formulas in row 7 are as follows.


A7: =xinit
B7: =yinit
C7: =uinit
D7: =h*C7
E7: =h*f(A7,B7,C7)
F7: =h*(C7+E7/2)
G7: =h*f(A7+h/2,B7+D7/2,C7+E7/2)
H7: =h*(C7+G7/2)
17: =h*f(A7+h/2,B7+F7/2,C7+G7/2)
J7: =h*(C7+17)
K7: =h*f(A7+h,B7+H7,C7+17)

(e) The formulas in row A8:C8 are shown here. Those in columns
D to K may be copied from the row above.
A8: =A7+h
B8: =B7+(D7+2*F7+2*H7+J7)/6
C8: =C7+(E7+2*G7+2*I7+K7)/6
D8: =h*C8

(f) Copy row 8 down to row 12 to geta final value ofx = 1.0. The
results should bey(1.0) = 0.119394 andy'(1.0) = 0.307960.

(g) Try other values of h such as 0.1 and 0.05 to see if the
approximations converge. You will need to expand the table
to have x = 1.0 in the final row.

Exercise 5: The Simple The equation of motion for


28
a simple pendulum oflength L is
d g. (8) =0
Pendulum ----SIn
dt' L
Most textbooks consider a pendulum that starts with a small
displacement and use the approximation sin(8);::; 8. Our
approximation will be to use the Runge-Kutta method to solve this
second-order differential equation to show how the angle and
angular velocity change with time. We will model a 0.75-meter
pendulum which is started with a displacement of 0.8 radians
from the perpendicular.
Differential Equations 257

As before, we start with the substitution de/dt = u, giving:


8'=u 8(t=0)=0.8
u'= -(g / L) sin (8) u (t= 0) = 0

(a) On the same module used for Exercise 4, code the Pend
function
Function Pend(L, angle)
9 = 9.8
Pend = (-giL) * Sin(angle)
End Function
The parentheses aroundg/L help in reading the formula.

(b) On SheetS enter the text and values shown in Al :K6 of


Figure 14.7.

Figure 14.7

(c) The formulas needed in row 7 start with:


A7: =InitTime
B7: =InitAngle
C7: =InitVel
D7: =h*C7
E7: =h*Pend(Length, B7)
F7: =h*(C7+E7/2)
G7: =h*Pend(Length, B7+D7/2)

(d) Using what you learned in Exercise 4, complete the formulas


in rows 7 and 8. Copy row 8 down to row 37.

(e) Make a chart showing how the angle and the velocity vary
with time.
258 A Guide to Microsoft Exce/2007 for Scientists and Engineers

Problems 1. Use Euler's method to solve the differential equation


dy = x + Y
dx
Subject to the initial condition x(O) = O,y(O) = O. Use steps of
0.1 and 0.05 up to x = 1. Compare your results with the
analytical solution y = e" - x-I.

2. *Use Euler's method with h = 0.05 to solve the differential


equation y' = -2xy with yeO) = 1 for a ~ x ~ 2. The exact
solution is y = exp (_x2 ) , but be careful how you make the Excel
formula; remember, negation has higher priority than
exponentiation.

3. RepeatProblem 2 using the Runge-Kutta method. Graphically


compare (Figure 14.8) the errors in each method with h =
0.05.
2.0E-02

L5E..(]2

1.0E..(]2
>
.5
~ 5.0E-Q3

.:;
O.OE+OO

-S.OE-03

-1.0E-<l2 +---~~-~-~

0.0 0.5 1.0 1.5 2.0


X

Figure 14.8

4. Use the Runge-Kutta method with h = 0.1 to solve the


differential equationy' = -2x - Y withy(O) = -1 for a ~ x ~ 2.

5. Water flows from an inverted conical tank with a circular


orifice at the rate

x'(t) = -O.6Jrr2 ~
.J2i A(x)
where ris the radius of the orifice, xis the height of the water
level from the vertex of the cone, and A (x) is the area of cross
section of the tank x units above the orifice. Find the time
when the tank is empty if r = 0.1 feet,g = 32 feet/sec", and the
tank was initially filled with 51211/3 cubic feet of water to a
level of 8 feet
1 M. L. James et aI., Applied
Numerical Methods for 6. *The circuit' shown in the following figure contains a battery
Digital Computation, Harper (E), an inductance (L), and a resistor (R) whose magnitude
& Row, New York, 1977 varies with its temperature and hence with the current
(page 406). passing through it Its resistance can be expressed by R = a +
Differential Equations 259

bi, where a and b are constants and i is the current The


switch (S) is closed at time t = a and the resulting current can
be described by the differential equation:

di
-=---[
E b' 3 a.
--I
dt L L L

R=a+bi 2

Using Exercise 3 as a model, compute the current from t = a


to t = 0.8 seconds in increments of 0.001 for the case E = 200
volts, L = 3 henrys, a = 100 ohms, and b = 50 ohms/amp".
Since the independent variable tdoes not appear explicitly in
the differential, the terms for the Runge-Kutta k's will involve
only the current i.

7. *If air resistance is proportional to the square of the


instantaneous velocity v, then the velocity of a mass m
2D. G. Zill, Differential dropped from a height h is determined by 2
Equations with Computer Lab dv 2
m-=mg-kv
Experiments, PWS dt
Publishing Co., Boston, 1995
(page 86). Use the Runge-Kutta method with increments of 0.5 to find
an approximate velocity after 5 seconds for a mass of 5 slugs.
Let 9 = 32 and k = 0.125. Compare your result with the
analytical solution:
3 Hint: Imagine the outflow

can be paused. Let 10 gals


flow in; what is the new
v(l) ~ ~7 tanh~~1
concentration? Let the 10gals
escape in a flash. How much 8. A 500-gal tank is filled with water with 20 lbs of dissolved
salt remains? Do this for salt Fresh water flows in at 10 gal/min. How long will it take
successive minutes. Can a
until there are just 5 lbs of salt in the tank? Assume perfect
mixing. Solve" this without writing down a differential
single formula be used to find
equation; see Figure 14.9. Can you write a formula to
the amount after n minutes?
compute twhen Salt = 5? As Mgets smaller, your answer will
Repeat the solution with converge to a more accurate value.
smaller time intervals.
260 A Guide to Microsoft Excel 2007 for Scientists and Engineers

Figure 14.9

9. *Write a differential equation for Problem 8. Integrate this


analytically. Make a plot of Salt against time from the data in
Problem 8 and compare its trendline to that predicted by
your analytical solution.

10. Suppose a ship moving at speed 6 m/s suffers a sudden loss


4J. R. Hanly, Essential C++ of power. We will assume the distance s (meters) it moves in
for Engineers and time t (seconds) is governed by the differential equation"
Scientists, Addison- Wesley, ds = v exp(- ktl)
Reading, MA, 1977 (page dt 0 1m
362).
with initial condition s(O) = O. Use the Runge-Kutta method
to find how far it will move in the first 60 seconds if k = 44 X
10 3 kg/s and m = 2.55 x 10 6 kg. For your first attempt use
steps of h =10 seconds. Then repeat using steps of h = 5 and
h = 1. Compare your approximations with the exact values
computed with s(t) = v~m (1- expf " k~).
11. Getting your information from a textbook and/or the
Internet, repeat Problem 4 using (i) integration using the
Taylor series method and (ii) the Runge-Kutta-Fehlberg
method. The second method is used in programs such as
MathLab and Maple for their OED routines.

You might also like