mathematical methods (10/24.539) i. first order ... · pdf filefirst order differential...

43
Mathematical Methods (10/24.539) I. First Order Differential Equations Introduction The goal of this course is to provide the student a solid foundation in engineering mathematics and to give him or her a set of practical tools that can be used in solving a variety of engineering design and analysis problems. The course is geared towards the senior or first-year graduate level and it reviews, enhances, extends, and integrates much of the engineering mathematics that is treated in a variety of undergraduate math and engineering science courses. The course is fast paced and it emphasizes both analytical procedures and practical numerical applications. Since most physical systems can be described in mathematical terms via differential equations (using physical conservation laws), much of the emphasis of this course deals either directly or indirectly with the development and solution of both ordinary and partial differential equations. The topics covered gradually build a mathematical framework that gives the student the tools needed to identify and solve a variety of differential equations common to many engineering disciplines. In building a foundation, one should always start with a strong base of familiar topics. With this in mind, this first section of notes briefly reviews the basic concepts and strategies (both analytical and numerical) for solving First Order Ordinary Differential Equations (ODEs). The most common and most useful general solution techniques are first reviewed and then several examples illustrate how to apply the various methods. This presentation model, which includes an overview of the solution algorithms (with minimal theoretical background) and specific example applications, is followed throughout these course notes. The material here and in the next few sections is typically treated in some detail in the standard undergraduate Differential Equations course taken by all engineering students. As such, these initial sections will not be treated in detail in this course! The material is included here primarily as a place to gather much of the pertinent subject matter needed as a prerequisite for this course. Also, of course, it is intended that the student will use this material for general review, to become familiar with my particular style and notation, and as an introduction to using Matlab in a variety of mathematically oriented problems and applications. The goal here is to make sure that everyone has a common base for more advanced material!!! A note about Matlab: Matlab is used as the primary problem-solving tool in this course. In fact, I use Matlab quite extensively in most of my courses and, over the years, I have put together a large number of Matlab script and function files to illustrate a variety of Matlab’s basic syntax and functional capability and to solve a wide range of illustrative engineering applications. In particular, many of the sample files for my undergraduate Differential Equations course and my Applied Problem Solving with Matlab course may be of interest for getting started with Matlab in this course -- especially if you are a new or relatively novice user! These sample Matlab programs can be accessed via my websites for the individual courses -- via the following URL: www.profjrwhite.com/courses.htm . In this first section, our summary review of analytical and numerical solution schemes for first order ODEs is broken into several subsections, as follows:

Upload: lamdieu

Post on 30-Jan-2018

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Mathematical Methods (10/24.539)

I. First Order Differential Equations

Introduction

The goal of this course is to provide the student a solid foundation in engineering mathematics and to give him or her a set of practical tools that can be used in solving a variety of engineering design and analysis problems. The course is geared towards the senior or first-year graduate level and it reviews, enhances, extends, and integrates much of the engineering mathematics that is treated in a variety of undergraduate math and engineering science courses. The course is fast paced and it emphasizes both analytical procedures and practical numerical applications.

Since most physical systems can be described in mathematical terms via differential equations (using physical conservation laws), much of the emphasis of this course deals either directly or indirectly with the development and solution of both ordinary and partial differential equations. The topics covered gradually build a mathematical framework that gives the student the tools needed to identify and solve a variety of differential equations common to many engineering disciplines.

In building a foundation, one should always start with a strong base of familiar topics. With this in mind, this first section of notes briefly reviews the basic concepts and strategies (both analytical and numerical) for solving First Order Ordinary Differential Equations (ODEs). The most common and most useful general solution techniques are first reviewed and then several examples illustrate how to apply the various methods. This presentation model, which includes an overview of the solution algorithms (with minimal theoretical background) and specific example applications, is followed throughout these course notes.

The material here and in the next few sections is typically treated in some detail in the standard undergraduate Differential Equations course taken by all engineering students. As such, these initial sections will not be treated in detail in this course! The material is included here primarily as a place to gather much of the pertinent subject matter needed as a prerequisite for this course. Also, of course, it is intended that the student will use this material for general review, to become familiar with my particular style and notation, and as an introduction to using Matlab in a variety of mathematically oriented problems and applications. The goal here is to make sure that everyone has a common base for more advanced material!!!

A note about Matlab: Matlab is used as the primary problem-solving tool in this course. In fact, I use Matlab quite extensively in most of my courses and, over the years, I have put together a large number of Matlab script and function files to illustrate a variety of Matlab’s basic syntax and functional capability and to solve a wide range of illustrative engineering applications. In particular, many of the sample files for my undergraduate Differential Equations course and my Applied Problem Solving with Matlab course may be of interest for getting started with Matlab in this course -- especially if you are a new or relatively novice user! These sample Matlab programs can be accessed via my websites for the individual courses -- via the following URL: www.profjrwhite.com/courses.htm.

In this first section, our summary review of analytical and numerical solution schemes for first order ODEs is broken into several subsections, as follows:

Page 2: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 2

Overview of Analytical Solution Methods

• Various Forms

• Separable Equations (Method I)

• Reduction to Separable Form (Method II)

• Exact Equations (Method III)

• Reduction to Exact Form (Method IV)

• Linear Equations (Method V)

• Reduction to Linear Form (Method VI)

Examples of Analytical Solution Methods

• Example 1.1 - Solve 2

2dy 3xy 2ydx 2x y x

+= −

+ using Method IV

• Example 1.2 - Solve 2 2dy x 3y

dx 2xy+

= using Method II

• Example 1.3 - Solve same equation as in Example 1.2 using Method VI

• Example 1.4 - Solve using Method VI 2 2xy ' y x y y(0.5) 0.5 with + = =

• Example 1.5 - Chain Sliding Off Frictionless Table

• Example 1.6 - Orthogonal Trajectories

Approximate Solutions via Picard’s Iteration

Numerical Solution Methods

• Overview of One-Step and Multi-Step Methods

• Euler’s and Modified Euler’s Methods

• Runge-Kutta Methods

• The Adams-Bashford-Moulton Method

• Adaptive Control of the Integration Step

• ODE Solvers in Matlab

Examples of Numerical Solutions

• Example 1.7 - Revisit Example 1.1

• Example 1.8 - Revisit Examples 1.2 and 1.3

• Example 1.9 - Flow from a Damaged Oil Tank

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 3: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 3

The reader should also see the various additional examples of first-order systems under the Illustrative Applications link at my website for my undergraduate Differential Equation Course at UMass-Lowell (see www.profjrwhite.com/courses.htm). These examples use a mix of analytical and numerical techniques and some include simulation and plotting of the solutions within Matlab. These extra examples simply provide a larger inventory of worked problems and they also illustrate the wide range of applications for the methods reviewed in Section I of these notes.

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 4: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 4

Overview of Analytical Solution Methods

This subsection briefly classifies and summarizes the various analytical solution schemes for first order ODEs.

Various Forms of First Order Equations

dy dy M(x, y) dy f (x)F(x, y)dx dx N(x, y) dx g(y)

= = − =

+

=

(1.1)

Method I - Separable Equations

Given , integrate directly to give g(y)dy f (x)dx=

(1.2) g(y)dy f (x)dx c=∫ ∫where c is an integration constant.

Method II - Reduction to Separable Form

Certain first order equations that are not separable can be made separable by a simple change of variables. In particular, homogeneous equations can often be reduced to separable form by the substitution of y = ux.

Note: Given , if the substitution M(x, y)dx N(x, y)dy 0+ xλ for x and yλ for y gives an expression of the form [ ]n M(x, y)dx N(x, y)dy 0λ + , then the system is said to be homogeneous. Example 1.2 illustrates this solution method.

=

=

Method III - Exact Equations:

The first order equation is said to be exact if the left hand side is an exact differential of the form

M(x, y)dx N(x, y)dy 0+

u udu dx dy 0x y

∂ ∂= +

∂ ∂=

=

(1.3)

Then by integration, one has

(1.4) du u(x, y) c 0 or u(x, y) c= + =∫Note also that from eqn. (1.3), we have

uM(x, y) and N(x, y)x y

∂=

∂ ∂u∂

= (1.5)

and then

2 2M u Nand

y y x x x∂ ∂ ∂ ∂

=∂ ∂ ∂ ∂ ∂ ∂

uy

= (1.6)

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 5: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 5

Therefore, since 2 2u

y x x y∂ ∂

=∂ ∂ ∂ ∂

u , we have

My x

∂ ∂=

∂ ∂N (1.7)

as a necessary and sufficient condition for Mdx + Ndy to be a total differential.

Method IV - Reduction to Exact Form

Some equations can be made exact by multiplication by some function, , which is usually called the Integrating Factor. Given

g(x, y) 0≠0M(x, y)dx N(x, y)dy+ = , if g(x, y) is an

integrating factor, then is exact. If this is true, then the following expression is valid:

gMdx gNdy 0+ =

(gM) (gN)y x∂ ∂

=∂ ∂

(1.8)

This is rather complicated for the general function g(x,y). But, if g(x,y) is only a function of one variable [for example, if we let g(x,y) = g(x)], eqn. (1.8) becomes

M N dg g Ny x

∂ ∂= +

∂ ∂g

dx

Dividing by gN and reordering gives

1 dg 1 M Ng dx N y x

∂ ∂= − ∂ ∂

(1.9)

Solution of this equation gives the desired integrating factor, g(x). Example 1.1 further illustrates this solution method. Note that one could also let g(x,y) = g(y), and a similar, but different expression for eqn. (1.9) results. This alternate formulation is sometimes useful!

Method V - Linear First Order Equations

Given a linear equation of the form

(1.10) y '(x) p(x)y(x) q(x)+ =

we first rewrite this as . Then, when multiplied by an integrating factor, g(x), this equation can be written as

(py q)dx dy 0− + =gMdx gNdy 0+ = , where M(x, y) py q= − , and

g(x) is defined through eqn. (1.9), or N(x, y) 1=

( )1 dg 1 M N 1 p(x) 0 p(x)g dx N y x 1

∂ ∂= − = − = ∂ ∂

Therefore

p(x)dxdg pdx and ln g p(x)dx or g(x) eg

∫= = ∫ = (1.11)

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 6: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 6

Now with this integrating factor, the original first order linear equation becomes

p(x)dx p(x)dx p(x)dxe y '(x) e p(x)y(x) e q(x)∫ ∫ ∫+ =

But ( ) (p(x)dx p(x)dx p(x)dx p(x)dxd d d(e y) e y e p(x)dx y e y ' pydx dx dx

∫ ∫ ∫ ∫= + =∫ )+

Therefore d , which gives ep(x)dx p(x)dx(e y) e qdx∫ ∫= p(x)dx p(x)dxy e qdx c∫ ∫= +∫ , or

p(x)dx p(x)dxx) e e q(x−∫ ∫y( )dx c = + ∫ (1.12)

In practical applications, the steps leading up to eqn. (1.12) should be performed for the explicit equation of interest. Along the way, one should always check to make sure that, with the computed integrating factor, the left hand side of the original ODE is indeed an exact differential. This simply verifies that the integrating factor has been properly determined.

Method VI - Reduction to Linear Form (Bernoulli Equation)

Some nonlinear equations can be reduced to linear form via a specific substitution. Probably the most important equation of this type is the Bernoulli equation:

(1.13) ay '(x) p(x)y(x) g(x)y(x)+ =

(note: if a = 0 or 1 then the system is linear or separable).

To linearize this system, let 1 au(x) y(x) −= (1.14)

Then

a a a 1 adu dy(1 a)y (1 a)y (gy py) (1 a)(g py ) (1 a)(g pu)dx dx

− − −= − = − − = − − = − −

Therefore,

(1.15) u ' (1 a)pu (1 a)g+ − = −

which is a first order linear system for u(x). One then simply solves this expression for u(x), which effectively gives the desired result for y(x) via the substitution in eqn. (1.14). Example 1.3 and Example 1.4 illustrate the use of Method VI.

Note that Methods I, III, and V are implicitly included as part of Methods II, IV, and VI, respectively. Thus, illustration of these techniques is also handled via Examples 1.1 - 1.4.

Example 1.5 presents a physical situation involving a chain sliding off a table (and it also illustrates the use of the Matlab program for plotting solution results). This example is our first case that involves the development of the mathematical model for a given physical system. Many additional illustrations of this type are integrated throughout these notes.

Finally, Example 1.6, a study of oblique and orthogonal trajectories, provides further application of first order ODE solution methods and implementation in Matlab. This example is useful for those unfamiliar with Matlab as a plotting and visualization tool.

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 7: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 7

Example 1.1 -- Analytical Solution Methods (Method IV)

Problem Description:

Solve the following equation using Method IV (Reduction to Exact Form):

2

2dy 3xy 2ydx 2x y x

+= −

+

Problem Solution:

This expression is not homogeneous, and it is not separable. It is also not linear nor is it easily written as a Bernoulli equation. Therefore, let’s see if it is exact or if it can be reduced to exact form by finding an integrating factor.

1. Rewrite the base equation as Mdx Ndy 0+ =

( ) ( )2 23xy 2y dx 2x y x dy 0+ + + =

2. Check exactness ?M N

y x∂ ∂

=∂ ∂

M N6xy 2 and 4xy 1y x

∂ ∂= + = +

∂ ∂

Therefore, the equation is not exact.

3. Compute integrating factor, g(x):

( ) ( )21 dg 1 M N 1 2xy 1 16xy 2 4xy 1g dx N y x x(2xy 1) x2x y x

∂ ∂ += − = + − + = ∂ ∂ ++

=

Therefore,

dg dx or ln g ln x or g(x) xg x

= = =

=

4. Check exactness with the integrating factor:

( ) ( )2 2 3 23x y 2xy dx 2x y x dy 0+ + +

2 2M N6x y 2x and 6x y 2xy x

∂ ∂= + = +

∂ ∂

Therefore My x

∂ ∂=

∂ ∂N and the system is exact (see Method III).

5. Now that the system is exact we can simply integrate as follows:

u udu dx dy 0x y

∂ ∂= +

∂ ∂=

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 8: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 8

Therefore,

2 2uM 3x y 2x

∂= = +

∂xy

Integrating this with respect to x gives

( ) ( ) ( )2 2 3 2 2u x, y 3x y 2xy dx x y x y f y= + = + +∫Note that the integration constant, f(y), in general, can be a function of y since y is considered to be a constant when performing partial integration with respect to x.

6. Now to evaluate the arbitrary function f(y), one has

3 2uN 2x yy

∂= = +

∂x

)

But from , we have ( ) (3 2 2u x, y x y x y f y= + + 3 2u 2x y xy d

dfy

∂= + +

∂ and comparing these two

expressions for u y∂∂ shows that ( )0 and f y k dfdy

= = .

7. Therefore, the final solution is

( ) 3 2 2u x, y x y x y k 0= + + =

or

3 2 2x y x y c where c k+ = = −

This is referred to as an implicit solution. Equations of this type give an implied relationship between x and y but, quite often, an explicit function, y(x) = F(x), cannot be derived.

Example 1.1 is reconsidered using a numerical solution method in Example 1.7.

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 9: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 9

Example 1.2 -- Analytical Solution Methods (Method II)

Problem Description:

Solve the following equation using Method II (Reduction to Separable Form):

2 2dy x 3y

dx 2xy+

=

Problem Solution:

1. Rewrite the base equation as ( ) ( )2 2x 3y dx 2xy dy 0+ − = .

2. Note that this is homogeneous, since and give x → λx y y→ λ

( ) ( )2 2 2 2x 3y dx 2xy dy 0λ + − λ =

3. Therefore, letting , gives y ux=

dy duu xdx dx

= +

4. Substitution into the original equation gives

( )2 2

2

x 1 3uduu xdx x (2u)

++ =

and doing some arithmetic gives

2 2 2du 1 3u 1 3u 2u 1 ux u

dx 2u 2u 2u+ + −

= − = =2+

and finally we have

2dx 2udux 1 u

=+

which is separable (see Method I).

5. Integrating both sides gives (let 2z 1 u= + )

( )2ln x ln z c ln 1 u c= + = + +

2xln c

1 u = +

or c2

x e k1 u

= =+

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 10: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 10

and finally 2x ky1x

= +

6. Simplify this expression as desired, or

3

2 2 2x x k

x yy1x

= =+ +

( )3 2x k x y= + 2

2 2 3y cx x= −

and finally the simplest form of the general solution is

( )2 2y x cx 1= −

Note that Example 1.3 uses Method VI to achieve the same results. A numerical solution is also formulated in Example 1.8 using Matlab's built-in ODE solver (as discussed later).

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 11: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 11

Example 1.3 -- Analytical Solution Methods (Method VI)

Problem Description:

Solve the following equation using Method VI (Reduction to Linear Form). First write the expression as a Bernoulli equation and solve the resultant relationship as a first order linear equation:

2 2dy x 3y

dx 2xy+

=

Note that this expression is homogeneous so Method II also applies (see Example 1.2).

Problem Solution:

1. Rewrite base equation as

13 1y ' y xy2x 2

−= +

or 13 1y ' y xy2x 2

−− =

2. Recognize the above equation as a specific form of Bernoulli’s equation,

ay ' p(x)y g(x)y+ =

where a = -1. Therefore, let . With this substitution we have 1 a 2u(x) y y−= =

2 du dyu y and 2ydx dx

= =

To simplify the algebra we first multiply the original equation by 2y, giving

2dy 32y x ydx x

= +

Now, substitution of the expressions for u and du/dx gives

du 3x udx x

= −

or 3u ' u xx

− =

and this is a linear first order differential equation.

3. Now solve the linear first order ODE using the integrating factor approach (see Method V), where the integrating factor, g(x), is given by (note that this g(x) is different from above)

3

3dxp(x)dx 3ln x ln x 3xg(x) e e e e x−− − −∫∫= = = = =

Now, multiplying by the integrating factor gives

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 12: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 12

( ) ( )3 4 3 3dx u ' 3x u x u x xdx

− − − −− = =

and, integrating both sides, gives

3 2 1x u x dx x c− − −= = −∫ +

or ( )2 3 2u x cx x cx 1= − + = −

4. Substitute u to get the final expression 2y=

( )2 2y x cx 1= −

which is the same answer as that derived for Example 1.2 (as expected). Example 1.8 compares the two approaches in Examples 1.2 and 1.3 to solutions derived numerically.

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 13: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 13

Example 1.4 -- Analytical Solution Methods (Method VI)

Problem Description:

Solve the following Bernoulli equation using Method VI (Reduction to Linear Form):

( )2 2xy ' y x y with y 0.5 0.5 + = =

Problem Solution:

1. Rewrite problem as

21y ' y xyx

+ =

and recognize this as a standard Bernoulli equation with a = 2. Now let

1 a 1u y y− −= =

and 2 2 2 11 1 1u ' y y ' y y xy y x u xx x x

− − − = − = − − + = − = −

or 1u ' u xx

− = −

2. The integrating factor, g(x), for this linear first order ODE (see Method V) can be written as

1

dxp(x)dx ln x ln x 1x 1g(x) e e e e x

x−− − −∫∫= = = = = =

Therefore,

1 1 d 1 1u ' u u x 1x x dx x x

− = = − = −

Integrating gives

( )1 u x c or u x c xx

= − + = −

or since , we have a general solution of the form 1u y−=

( )

1yx c x

=−

3. Now evaluate the initial condition to obtain a unique solution, or

( ) ( )1 1y 0.52 0.5 c 0.5

= =−

or 0.5 which gives c = 4.5. Therefore, the unique solution is c 0.25 2− =( )

1yx 4.5 x

=−

.

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 14: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 14

Example 1.5 -- Analytical Solution Methods (Chain Sliding Off Frictionless Table)

Problem Description:

A uniform chain of total length ‘a’ is on a horizontal frictionless table so that a length ‘b’ dangles over the side at time zero. Determine how long it will take the chain to slide off the table.

Problem Solution:

Model Development

Define the density of the chain as (mass per unit length) and let x(t) be the length hanging over the side at time t. Therefore, the force on the chain due to gravity is simply the weight of the portion of the chain overhanging the table, or

σ

gF mg x= = σ g

But the total mass being accelerated is given by

tdv dvF M adt dt

= = σ

And from Newton’s law we have

dva xdt

σ = σ g

Also, we know that position, x(t), and velocity, v(t), are related. In particular, one relationship that is often useful is

dv dv dx dvvdt dx dt dx

= =

Putting this into the above force balance gives the final mathematical model

dv gv xdx a

=

with initial conditions x(0) = b and v(0) = 0.

Solution Method

1. The above equation is separable (see Method I). Therefore, it can be written as

gvdv xdxa

=

2. Integration gives

( )2 2

2 2v g x gc or v x2 a 2 a

= + = +

c

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 15: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 15

or 2gv xa

= + c

3. Now applying the initial conditions gives

2g0 ba

= + c

Therefore, c and 2b= −

( ) 2 2gv x, t x ba

= −

This is the formal solution for v(x,t).

4. To find x(t) explicitly, we note that v(t) = dx/dt. Therefore,

2 2dx g x bdt a

= −

Separating variables again gives (see Method I)

2 2

dx g dtax b

=−

and, using standard integral tables, this becomes

( )1 x gcosh t cb a

− = +

5. To put this into a more usable form (there is no 1cosh− button on my calculator!), note that

( ) ( )u u 1 21cosh(u) e e and cosh (u) ln u u 12

− −= + = + −

Therefore,

( )2 2x x b gln t c

b a

+ −= +

6. Now applying the initial conditions again, we have that ln(b/b) = ln(1) = 0. Therefore, c = 0 in the above expression. Finally, the time it takes for the chain to overhang a distance x is given by

2 2a x x bT ln

g b

+ −=

7. For example, if a = 10 m, b = 1 m, and g = 9.8 m/s2, then the time required for the chain to fall off the (very tall) table is simply (note that x = a for this case)

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 16: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 16

( )2 210 10 10 1T ln 1.01*ln 19.95 3.02 sec

9.8 1

+ −= =

=

Thus, under these conditions, it takes about 3 sec for the chain to slide completely off the table.

8. The time to fall off the table can be evaluated as a function of the length of chain, a, and the initial overhang, b. Figure 1.1 shows these relationships. This was generated with the Matlab file chain.m, which is listed in Table 1.1 as a simple example of evaluating and plotting functions in Matlab. The reader is encouraged to become familiar with the various Matlab operations performed in this program.

Fig. 1.1 Time elapsed based on length of chain and initial overhang.

Table 1.1 Listing of Matlab file chain.m.

% % CHAIN.M Plotting and Function Evaluation with MATLAB % % This demo gives a simple example of the use of plots in MATLAB. % It also shows how to use MATLAB to perform simple functional evaluations % using vectorized operations and loops. See the MATLAB HELP files for more % description. % % This is an example of plotting a simple 2-d function, T(a,b), versus a and b. % In this case, T represents the time it takes a chain to slide off a frictionless % table. The variables a and b represent the total length of the chain and the % initial length of the overhang, respectively. The function T(b) is drawn for % several values of the variable a (giving a family of curves). % Solving the differential equation that describes this system gives the following % result: % T(a,b) = sqrt(a/g) * ln((a+sqrt(a*a-b*b))/b) % % The goal here is simply to evaluate and plot this function for various values of % a and b. % % File prepared by J. R. White, UMass-Lowell (July 2003)

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 17: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 17

% % % getting started clear all, close all, nfig = 0; % % parameter definition % a = length of chain % b = initial length of overhang % g = 9.8; % gravitational accel (m/sec^2) % ao = 5; % start value for a (m) af = 25; % end value for a (m) Na = 5; % number of values of a to use a = linspace(ao,af,Na); % actual vector of a values % bo = 0.1; % start value for b (fraction of a) bf = 0.9; % end value for b (fraction of a) Nb = 10; % number of values of b to use for each a value % T = zeros(Nb,Na); % initialize array for storage of results b = zeros(Nb,Na); % initialize array to keep Nb b values for Na a values txt = cell(Na,1); % cell array to store text for legend on plot % % loop over all values of a for n = 1:Na xa = a(n); b(:,n) = linspace(bo*xa,bf*xa,Nb)'; T(:,n) = sqrt(xa/g)*log((xa+sqrt(xa*xa-b(:,n).*b(:,n)))./b(:,n)); txt(n) = {['a',' = ',num2str(a(n)),' m']}; end % % set color and marker code for creating plots Ncm = 6; scm = ['r-'; % red solid 'g:'; % green dotted 'b-'; % blue solid 'm:'; % magenta dotted 'c-'; % cyan solid 'y:']; % yellow dotted % % plot family of curves (one for each value of a) nfig = nfig+1; figure(nfig) for j = 1:Na plot(b(:,j),T(:,j),scm(j,:),'LineWidth',2), grid on, hold on end title('Chain: Time to Fall Off Table for Several Chain Lengths') xlabel('b-value (m)'),ylabel('T(a,b)-value (sec)') legend(txt) % % place some additional text on the plot v = axis; xt = v(1)+0.52*(v(2)-v(1)); yt1 = v(3) + 0.07*(v(4)-v(3)); yt2 = v(3) + 0.03*(v(4)-v(3)); text(xt,yt1,'a = length of chain (m)') text(xt,yt2,'b = length of initial overhang (m)') hold off % % end simulation

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 18: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 18

Example 1.6 -- Analytical Solution Methods (Orthogonal Trajectories)

Oblique Trajectories

If the differential equation of a one parameter family (not containing the parameter) is given by

y '(x) f (x, y)=

then the family of curves intersecting the original family at an angle α is given by

f (x, y) tany '(x)1 f (x, y) tan

+ α=

− α

Orthogonal Trajectories

If, in particular, , then . Therefore, the orthogonal family is defined by 90α = tan α → ∞

( )

1y '(x)f x, y

= −

Orthogonal families appear quite often in engineering analysis. For example, in heat transfer problems, the lines of heat flux are perpendicular to the lines of constant temperature (isotherms).

Problem Description:

As a specific numerical example, consider a family of parabolas defined by

2y cx=

Find and plot a family of curves that represent the orthogonal trajectories for these parabolas (choose several values for the constant c to make the curves easy to visualize) . Show that the curves are indeed orthogonal by magnifying a portion of the plot. Do the curves intersect at right angles? Be careful with the aspect ratio of the Matlab plots!

Problem Solution:

1. The defining differential equation for the original family of parabolas is given as follows:

2dy y dy 2y2cx 2 x ordx dx xx

= = =

2. Therefore, the family of orthogonal trajectories is given by the solutions to

dy xdx 2y

−=

3. Now separating variables and integrating gives

2ydy xdx= −

2

2 xy k2

= − +

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 19: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 19

or 2 2x 2y+ = c

and this family of ellipses should be orthogonal to the original family of parabolas.

4. These families of curves are plotted in Fig. 1.2 for positive x and y. This plot was generated using Matlab traject.m (see Table 1.2), which is a simple example of evaluating and plotting functions in Matlab.

.

Fig. 1.2 Orthogonal trajectories for family of ellipses and parabolas.

Table 1.2 Listing of Matlab file traject.m.

% % % TRAJECT.M Plotting and Function Evaluation with MATLAB % % This demo gives a simple example of the use of plots in MATLAB. % It also shows how to use MATLAB to perform simple functional evaluations % using vectorized operations. See the MATLAB HELP files for more description. % % This is an example of plotting orthogonal trajectories (ie. families of % curves that intersect at right angles). It uses the cartesian representation % of the curves. % 1. family of parabolas y = c*x^2 % 2. family of ellipses x^2 + 2*y^2 = k = b^2 % % File prepared by J. R. White, UMass-Lowell (July 2003) % % % getting started clear all, close all, nfig = 0; % % define x-axis nx = 2000; x = linspace(-5,5,nx); % % create curves for original family -- parabolas cc = [0.0 0.1 0.3 0.8 1.5]; ncc = length(cc); % set constants y1 = zeros(nx,ncc); y2 = zeros(nx,ncc); % initialize function values for n = 1:ncc

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 20: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 20

c = cc(n); yy = c*x.^2; y1(:,n) = yy'; y2(:,n) = -y1(:,n); % for negative constants end % % create curves for orthogonal family -- ellipses kk = [5 10 20 30]; nkk = length(kk); % set constants z1 = zeros(nx,nkk); z2 = zeros(nx,nkk); % initialize function values for n = 1:nkk k = kk(n); zz = real(sqrt((k - x.^2)/2)); for i = 1:nx if zz(i) > 0, z1(i,n) = zz(i); else z1(i,n) = NaN; end end z2(:,n) = -z1(:,n); % for negative roots end % % Note: If the argument of the 'sqrt' function is negative, the result will be a % pure imaginary number. The 'real' command is used in the above equations to extract % only the real part. If the combination of the constant k and the value of x is such % that the argument is negative, then z(x) simply takes on a value of NaN (not-a-number) % -- and the Matlab plot command simply ignores these. Thus, we can be a little sloppy % here without impacting the desired plots. Thus, in the above development, we do not % need to adjust the range of x for each new coefficient. % % plot both families (full x-y range) nfig = nfig+1; figure(nfig) plot(x,y1,'LineWidth',2), hold on plot(x,y2,'LineWidth',2), plot(x,z1,'LineWidth',2), plot(x,z2,'LineWidth',2) axis('equal'); v1 = [-5 5 -5 5]; axis(v1); grid; title('Traject: Example of Orthogonal Trajectories') xlabel('x-axis'),ylabel('y-axis'), % % plot both families (first quadrant only) nfig = nfig+1; figure(nfig) plot(x,y1,'LineWidth',2), hold on plot(x,z1,'LineWidth',2) axis('equal'); v2 = [0 5 0 5]; axis(v2); grid; title('Traject: Example of Orthogonal Trajectories') xlabel('x-axis'),ylabel('y-axis'), % % end of problem

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 21: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 21

Approximate Solutions via Picard’s Iteration

Sometimes a simple iterative technique is useful for trying to get a general idea of the solution to a complicated first order differential equation. In particular, Picard’s Iteration Scheme is outlined below.

Picard’s Iteration Scheme

Consider the initial value problem (IVP):

(1.16) 0y ' f (x, y) with y(x ) y = 0=

τ

This has a formal solution

(1.17) [ ]0

x0 x

y(x) y f , y( ) d= + τ τ∫An iterative solution scheme can be developed as follows:

0. Guess [ ]0 0y (x) usually y (x) y= 0

τ

τ

] τ

1. Compute [ ]0

x1 0 0x

y (x) y f , y ( ) d= + τ τ∫

2. Compute [ ]0

x2 0 1x

y (x) y f , y ( ) d= + τ τ∫and, at step n, we have

n. Compute (1.18) [0

xn 0 n 1x

y (x) y f , y ( ) d−= + τ τ∫

Problem Description:

As a specific example, consider the following IVP:

y ' y 2x with y(0) 1 − = =

Solve this problem using Picard’s Iteration and compare the low order estimates with the exact solution.

Problem Solution:

1. Let’s try Picard’s Method with 2 iterations:

0. 0y (x) 1=

1. [ ] [ ]xx x 2 2

1 00 0 0y (x) 1 2 y ( ) d 1 2 1 d 1 1 x x = + τ + τ τ = + τ + τ = + τ + τ = + + ∫ ∫

2.

[ ]x x 2

2 10 0x2 3 2 3

0

y (x) 1 2 y ( ) d 1 2 1 d

3 3x x1 1 x2 3 2 3

= + τ + τ τ = + τ + + τ + τ τ

τ τ= + τ + + = + + +

∫ ∫

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 22: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 22

2. Now let’s find the exact solution to this first order linear system using the integrating factor approach (Method V). For this case the integrating factor is given by

( )1 dx xg(x) e e− −∫= =

Therefore, we have

( )x x xde y ' e y e y 2xedx

− − −− = = x−

x−

x

Integrating the right hand side using integration by parts, we have

udv uv vdu= −∫ ∫with

xu 2x and dv e dx −= =

xdu 2dx and v e −= =

Now c x x x x2xe dx 2xe 2 e dx 2xe 2e− − − −= − + = − − +∫ ∫Therefore, integration of the exact form of the equation gives

x xe y 2xe 2e c− − −= − − +

or

xy(x) 2x 2 ce= − − +

Finally, applying the initial condition, y(0) = 1, gives c = 3. Therefore,

xy(x) 3e 2x 2= − −

In comparing the two solutions, note that

2 3 4

x x x xe 1 x2! 3! 4!

= + + + + +

Therefore,

( ) ( )2 33x 3xy(x) 3 2 3x 2x

2 6= − + − + + +

or

2 33x xy(x) 1 x

2 2= + + + +

Thus we see that Picard’s method, up to second order, gives the first three terms exactly.

Picard’s low order estimates are compared to the exact solution in Fig. 1.3. Note that only a few terms are required to give a reasonable estimate of the solution. This plot was generated in Matlab file picard.m. The reader should study Table 1.3, which contains a listing of picard.m, as another example of evaluating and plotting functions in Matlab.

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 23: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 23

Fig. 1.3 Picard’s low order estimates as compared to the exact solution.

Table 1.3 Listing of Matlab file picard.m.

% % PICARD.M Sample Problem to Illustrate Picard's Iteration Method % % Evaluate and compare solutions to the following initial value problem (IVP): % y' - y = 2x with y(0) = 1 % % Picard's result: y0(x) = 1 % y1(x) = 1 + x + x^2 % y2(x) = 1 + x +(3x^2)/2 +(x^3)/3 % % Exact Solution: y(x) = 3*exp(x) - 2x -2 % % File prepared by J. R. White, UMass-Lowell (July 2003) % % % getting started clear all, close all, nfig = 0; % % set x domain x = linspace(0,1,51); % % evaluate various Picard estimates of order 0, 1, and 2 y0 = ones(size(x)); y1 = 1 + x + x.*x; y2 = 1 + x + (3/2)*x.*x + (1/3)*x.*x.*x; % % evaluate exact solution ye = 3*exp(x) - 2*x - 2; % % plot comparisons v = [0 1 .5 4.5]; nfig = nfig+1; figure(nfig) plot(x,y0,x,y1,x,y2,x,ye,'LineWidth',2),axis(v) title('Picard: Example of the Picard Iteration Method') xlabel('x values'),ylabel('y(x)'),grid gtext('0th order'),gtext('1st order'),gtext('2nd order') gtext('exact soln') % % end of demo

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 24: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 24

Overview of Numerical Solution Methods

We seek a numerical solution of an initial value problem (IVP) of the form:

(1.19) 0y ' f (x, y) with y(x ) y = 0=

There are two basic approaches:

One Step Methods

The method is based on the use of values from the preceding step for the calculation of the function in the current step (this technique gives rise to a variety of Runge-Kutta formulas). The basis for most one-step numerical integration formulas is given by the Taylor Series Expansion about the point x or xn:

2 3h hy(x h) y(x) hy '(x) y ''(x) y '''(x)

2 6+ = + + + + (1.20)

where normalized step size. h x= ∆ =

Also since , we have y ' f , y '' f ',= =

2hy(x h) y(x) hf (x, y) f '(x, y)

2+ = + + +

or in discrete notation [with and nx x→ ( )n 1 nh x x+= − ], we have

( ) ( )2

n 1 n n n n nhy y hf x , y f ' x , y2+ = + + +

or 2

n 1 n n nhy y hf f '2+ = + + + (1.21)

Multistep Methods

The basic idea here is to use two or more preceding steps to estimate the next value of the function (one particular technique is the Adams-Bashford-Moulton Method). Multistep methods integrate over a finite step as above,

(1.22) [ ]n 1

n

xn 1 n x

y y f x, y(x) d+

+ = + ∫ x

)

but they replace the integrand with an interpolating polynomial approximation which can be integrated analytically. The order of the interpolating polynomial identifies the particular multistep method.

Euler’s First Order Method (1st Order Runge Kutta)

Basic Method

The simplest one-step method truncates the Taylor series after the 1st order term, giving

(1.23) (n 1 n n ny y hf x , y+ = +

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 25: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 25

This is a 1st order method whose truncation error per step is of the order h2 (usually written as O(h2)).

Note: The local error in a numerical solution scheme is related to the truncation error. In the case of the Euler method, . The global error, in contrast, is related to the total error expected in the computation. Again, using the Euler method as an example, we know that the number of integration steps needed will be proportional to 1/h. Thus, for the Euler method, the global error becomes

2local O(h )ε ≈

2

globalO(h ) O(h)

hε ≈ ≈

Based on these arguments, the Euler method is referred to as a 1st order method since its global error is proportional to h.

Physical Interpretation

Assume that the slope is constant over the interval y ' f (x, y)= n 1 nh x x+= − . Therefore,

n 1 n 1

n n

y x

y xdy f (x, y)dx+ +=∫ ∫

or ( ) (n 1 n n n n 1 ny y f x , y x x+ +− = − )

n

k

h

which reduces to eqn. (1.23).

Algorithm

1. Evaluate function (just once per step)

( )1 nk hf x , y=

2. Increment function

n 1 n 1y y+ = +

3. Increment independent variable

n 1 nx x+ = +

Repeat all steps until done.

Modified Euler’s Method/Heun’s Method (2nd Order Runge Kutta)

Basic Method

In this approximation we truncate the Taylor series after the 2nd order term, giving

( ) ( )2

n 1 n n n n nhy y hf x , y f ' x , y2+ = + +

Now let

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 26: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 26

( ) ( ) ( )n 1 n 1 n nn n

f x , y f x , yf ' x , y

h+ + −

=

which gives

( ) ( *n 1 n n n n 1 n 1

1y y h f x , y f x , y2+

= + + )+ + (the corrector step) (1.24)

The problem here is that we need an estimate of *n 1y + to evaluate yn+1. Let’s try Euler’s First

Order Method, or

(the predictor step) (1.25) (*n 1 n n ny y hf x , y+ = + )

n

1

This is a 2nd order method whose local error is of order h3 (written as O(h3)).

Physical Interpretation

Assume that the slope varies linearly over interval h. y ' f (x, y)=

Algorithm

1. Evaluate function (two times per step)

( )1 nk hf x , y=

( )2 n 1 nk hf x , y k+= +

2. Increment function

( )n 1 n 1 21y y k k2+ = + +

3. Increment independent variable

n 1 nx x+ = + h

)h

)

)

Repeat all steps until done.

Runge-Kutta Methods

Basic Method

Runge-Kutta (RK) methods refer to a whole range of methods that use a truncated Taylor series expansion without requiring the calculation of the higher derivatives. In general, the end-of-step value is given by

(1.26) (n 1 n n ny y x , y ,+ = + φ

where is the increment function which can be interpreted as an average slope over the interval times the interval width h. This function is of the form

( n nx , y ,hφ

(1.27) ( ) (n n 1 1 2 2 N Nx , y ,h a k a k a kφ = + +

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 27: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 27

where N is the order of the RK method and the explicit formulae for the kj’s are determined by comparison to the original Taylor series expansion with appropriate approximations for the derivatives of . ( )n nf x , y

Algorithm for Classical 4th Order RK Method

1. Evaluate function (4 times per step)

( )1 nk hf x , y= n

2 n n1 1k hf x h, y k2 2

= + +

1

3 n n1 1k hf x h, y k2 2

= + +

2

3

( )4 n nk hf x h, y k= + +

2. Increment function

( )n 1 n 1 2 3 41y y k 2k 2k k6+ = + + + +

3. Increment independent variable

n 1 nx x+ = + h

x

Repeat all steps until done.

This method is easy to implement on a computer and is accurate to 4th order (i.e. error per step is on the order of h5).

Adams-Bashford-Moulton Method (A Multistep Method)

Basic Idea

Given the initial value problem (IVP)

( )0 0y ' f (x, y) with y x y = =

one integrates over a finite step using a simple interpolating polynomial approximation for f(x,y) over the interval, or

[ ]n 1

n

xn 1 n x

y y f x, y(x) d+

+ = + ∫The particular choice of the interpolating polynomials and the specific implementation give rise to various multistep methods. Probably the most common implementation is the Adams-Bashford-Moulton method.

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 28: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 28

Adams-Bashford Method -- The Predictor Step

1. Fit a cubic interpolating polynomial to the function f(x,y) through the points xn, xn-1, xn-2, xn-3. Note that four points are needed to fit a 3rd order polynomial.

2. Integrate this simple function over the interval xn to xn+1, giving

(n 1 n n n 1 n 2 n 3hy y 55f 59f 37f 9f24

∗+ −= + − + − )− −

+

(1.28)

Note that this requires extrapolation beyond the original range of the interpolating polynomial.

Adams-Moulton Method -- The Corrector Step

3. Fit a cubic polynomial to f(x,y) through the points xn+1, xn, xn-1, xn-2, where . ( )n 1 n 1 n 1f f x , y∗ ∗

+ +=

4. Integrate the resultant polynomial, giving

(n 1 n n 1 n n 1 n 2hy y 9f 19f 5f f24

∗+ + −= + + − + )−

)∗+

(1.29)

Algorithm

1. Use the RK method to determine y3, y2, and y1 (y0 is known).

2. Calculate using eqn. (1.28). n 1y∗+

3. Evaluate f f and calculate y(n 1 n 1 n 1x , y∗+ += n+1 using eqn. (1.29).

4. Increment the x interval and go to step 2 until done.

Summary Notes

The Adams-Bashford-Moulton Method is of 4th order (i.e. local error is on the order of h5).

This method is faster than the 4th order RK method since only two new functional evaluations are needed in each step.

However, the method is not self starting. Therefore the RK method is needed to initiate this multistep method.

Adaptive Control of Integration Step

General Notation and Comments Concerning Error Control

Error analysis and control are important features of any numerical scheme for solving first order ODEs. Automatic error control is a feature that is usually built directly into the software, making the use of an ODE solver relatively straightforward for the user. In summarizing this subject, we first define the following symbols:

maximum desired error (ε = ε = tol/10, where tol is user specified)

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 29: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 29

estimate of error at each step δ =

and the basic guidelines for setting step size are roughly given by:

1. if 0.2 , leave h unchanged 10ε < δ < ε

2. if , decrease h by a factor of 2 and redo current step 10δ > ε

3. if , increase h by a factor of 2 0.2δ < ε

In general, h should not be too small because the number of steps will be large (leads to increased round-off error and computational inefficiency). The interval size should also not be too large because truncation error will be large. Thus, one should try to follow the above guidelines to assure optimum performance of the numerical algorithm.

Obtaining Error Estimates

Method I - Computations with Two Different Step Sizes (h and 2h) Let’s illustrate this method with an example that uses a 4th order RK method. The desired solution, , can be written in terms of the computed solution using step size h, y(h), and an estimate of the error, δ , or

y

5y y(h) O(h ) y(h)= + ≈ + δ

δ

Now if the same calculation is repeated with size 2h, then the solution can be written as

( )5 5y y(2h) O (2h) y(2h) 2= + ≈ +

Subtracting these two equations gives

( )50 y(h) y(2h) 1 2= − + δ −

or letting y y(h) y(2h)∆ = −

we have 5 5y 1 1y y

322 1 2∆

δ = ≈ ∆ ≈ ∆−

This estimate of the error is often increased by a factor of two for conservatism and because of the additional round-off error in the case of the small step size [i.e. y(h) requires twice as many steps as y(2h)]. Therefore, for the 4th order RK method, the error estimate for simultaneous computation with step sizes h and 2h is given by

y where y y(h) y(2h)16

∆δ ≈ ∆ = − (1.30)

Method II - Computations with Two Different RK Approximations In Predictor-Corrector methods, the difference between n 1y∗

+ (the predicted value) and yn+1 (the corrected value) can give an estimate of the error in yn+1. For example, in the Modified Euler Method, the desired result using the high-order (corrected) solution is given by

( )3y y O h y= + ≈ + δ

and the desired solution using the low-order (predicted) solution can be written as

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 30: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 30

( )2y y O h yh

∗ ∗ δ= + ≈ +

Therefore, as before, we subtract these to get

0 y yh

∗ δ= − + δ −

or letting y y y∗∆ = −

we have y h y (for small h)11h

∆δ ≈ ≈ ∆

Therefore, for the Modified Euler (or RK1-2) method, the error estimate for simultaneous computation with different order RK approximations is given by

h y where y y y ∗δ ≈ ∆ ∆ = − (1.31)

Summary Notes

When one performs error analysis using the Taylor Series, the independent variable is always normalized to span the range [0,1]. Therefore, the step size, h, is always less than unity. Note also that the above analyses are quite crude and they are included to help illustrate the basic strategies used. The point here is that error estimates are readily available in software implementing numerical solution schemes, and they can be used to automatically adjust h as the calculation progresses. This gives the user control in specifying the desired error tolerance without the burden of choosing and changing h during the calculation. This usually leads to significant gains in accuracy, efficiency, and general ease-of-use.

ODE Solvers in Matlab

Overview

Matlab has a suite of routines for numerical solution of ordinary differential equations. These functions use a variety of techniques depending upon the particular routine that is chosen (see the Matlab help files for a list of available options). The ode23 and ode45 built-in functions are the most commonly chosen routines for the solution of many typical ODE problems. These routines apply a variation of the adaptive step control techniques described above by obtaining error estimates using two RK predictions of different order. The ode23 routine uses a combination of 2nd and 3rd order formulas for low accuracy, and ode45 uses a 4th and 5th order set for medium accuracy per step for a given step size.

Use of the Routines

An overview of how to use the ode23 and ode45 routines in Matlab can be obtained by simply typing help ode23 or help ode45 at the Matlab prompt (this procedure is the best way to learn how to use any Matlab function -- be sure to use lower case letters since Matlab is case sensitive). A description of the ODE function file format and the many options that can be set

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 31: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 31

can also be obtained with the help odeset command. Matlab also has several built-in example cases (orbitode, ballode, rigidode, etc.) that can be run locally on the student’s machine. They are quite instructive and can be run by simply typing the appropriate filename at the Matlab prompt.

From the above help files and examples it should be apparent that there is lots of flexibility within the Matlab suite of ODE solvers (for both stiff and non-stiff equations). In a typical application, most of this flexibility (and complexity) is not needed, and the use of ode23, for example, becomes quite straightforward. To illustrate the relative ease-of-use in routine applications, three sample cases have been prepared for demonstration here. Example 1.7 and Example 1.8 represent numerical solutions for comparison to analytical solutions established in Examples 1.1 - 1.3. In addition, Example 1.9, which illustrates a more realistic engineering situation, requires several solution steps in addition to numerical integration of a first order ordinary differential equation. This combination of examples should give the reader a good understanding of how to use Matlab’s ODE solvers for the solution of a single non-stiff ordinary differential equation. The student can refer to Matlab’s built-in ODE examples for use of some of the additional features that are available. We will also expand our ability to use the Matlab ODE solvers in a variety of situations in future sections of these notes.

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 32: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 32

Example 1.7 -- Numerical Solution Methods (Ex. 1.1 Revisited)

Problem Description:

Solve the following equation using Matlab’s ode23 routine and compare the numerical solution to the analytical solution from Example 1.1 - Analytical Solution Methods:

2

2dy 3xy 2y with y(1) 2dx 2x y x

+= − =

+

Problem Solution:

1. The general solution was derived analytically in Example 1.1 as

3 2 2x y x y c+ =

Applying the initial condition, y(1) = 2, gives c = 6. Therefore, the unique solution to the given IVP problem is

3 2 2x y x y 6+ =

This solution is in implicit form. Equations of this type give an implied relationship between x and y but, quite often, an implicit function, y(x) = F(x), cannot be derived. However, this solution can be written in the form

3 2 2f (x, y) x y x y 6 0= + − =

and the problem becomes one of finding the roots of f(x,y) = 0 given some specific range of values for x (i.e. given some x, what value of y gives f(x,y) = 0?). Matlab has a root finding routine called fzero and this was used in the Matlab code (see below) to evaluate the roots of f(x,y) = 0 over the range x = [1,3].

2. A numerical solution to the given IVP was obtained using the ode23 numerical integration routine. Although it is somewhat difficult to see, Fig. 1.4 does show that the step interval is not constant, illustrating the built-in adaptive step size algorithm.

3. The numerical and analytical solutions are also compared in Fig. 1.4. Clearly the numerical and analytical solutions show good agreement. These results were obtained using the main Matlab file odenex1.m. This script file also refers to files odenex1a.m and odenex1b.m, which perform the function evaluations for fzero and ode23, respectively. The Matlab script and function files for this problem are listed in Table 1.4. This sequence of Matlab m-files represents a good example of root finding and numerical integration of a single first order differential equation within the Matlab environment.

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 33: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 33

Fig. 1.4 A comparison of numerical and analytical solution methods.

Table 1.4 Listing of Matlab files for Example 1.7.

% % ODENEX1.M Solution of ODEs with MATLAB (IVP) % % This demo gives an example of how to use the ODE23 routine to integrate % a simple first order differential equation. A slight extension to vectors % allows the solution of systems of first order ODEs. This extension will be % treated in later examples (see section on General IVPs). % % This example also illustrates how to evaluate and plot a function in implicit % form. This requires use of Matlab's FZERO root finding routine. % % also see ODENEX1A.M - function file for use in FZERO % ODENEX1B.M - function file for use in ODE23 % % This example solves y' = -(3xy^2 + 2y)/(2yx^2 + x) with y(1) = 2 % and compares to exact soln x^3*y^2 + x^2*y = 6 (implicit form) % % File prepared by J. R. White, UMass-Lowell (July 2003) % % % getting started clear all, close all, nfig = 0; % % initial setup xo = 1; xf = 3; yo = 2; tol = .00001; global XX % % evaluate exact solution % The implicit form can be written as f(x,y) = x^3*y^2 + x^2*y - 6 = 0 % Thus, to plot y versus x, we find the 'roots' of this implicit eqn % for each value of x, and then plot the results. % % first set the range for x Nx = 31; xe = linspace(xo,xf,Nx); ye = zeros(size(xe)); % % now evaluate the implicit eqn at each point (uses previous y-value as guess) xe(1)= xo; ye(1) = yo; for i = 2:Nx XX = xe(i); ye(i) = fzero('odenex1a',ye(i-1));

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 34: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 34

end % % now evaluate the numerical solution (using ODE23) options = odeset('RelTol',tol); [x1,y1] = ode23('odenex1b',[xo xf],yo,options); % % now plot results (numerical(points), exact(solid)) nfig = nfig+1; figure(nfig) plot(xe,ye,'b-',x1,y1,'ro','LineWidth',2) title('ODEnex1: Numerical & Analytical Solutions to 1st Order ODE') xlabel('x values'),ylabel('y(x)'),grid legend('EXACT','ODE23'); % % end of simulation % % ODENEX1A.M Function file for use in FZERO (see ODENEX1.M) % % Evaluates f(x,y) = x^3*y^2 + x^2*y - 6 for various y values % for a specified x value which is given as XX in the global list. % % File prepared by J. R. White, UMass-Lowell (July 2003) % function f = funfile(y) global XX f = XX.^3.*y.^2 + XX.^2.*y - 6; % % end of function % % ODENEX1B.M Function evaluation for ODE step in ODENEX1.M % % Evaluate y' = -(3xy^2 + 2y)/(2yx^2 + x) % % File prepared by J. R. White, UMass-Lowell (July 2003) % function yp = odefile(x,y) yp = -(3*x*y^2 + 2*y)/(2*y*x^2 + x); % % end of function

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 35: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 35

Example 1.8 -- Numerical Solution Methods (Ex. 1.2 and Ex. 1.3 Revisited)

Problem Description:

Solve the following equation using Matlab’s ode23 routine and compare the numerical solution to the analytical solution from Example 1.2 and Example 1.3 - Analytical Solution Methods:

2 2dy x 3y with y(1) 2

dx 2xy +

= =

Problem Solution:

1. The general solution was derived analytically in Examples 1.2 and 1.3 as

( )2 2y x cx 1= −

Applying the initial condition, y(1) = 2, gives c = 5. Therefore, the unique solution to the given IVP problem is simply

y(x) x 5x 1= −

2. The numerical solution to the given IVP was obtained using the ode23 numerical integration routine. Again, the step size is not constant, as expected.

3. The numerical and analytical solutions are compared in Fig 1.5. Clearly the numerical and analytical solutions give the same result.

The data in Fig. 1.5 were obtained using the main Matlab file odenex2.m and the ODE function file odenex2a.m, and these are listed in Table 1.5. The reader should study these files thoroughly, since they represent the most common implementation for solving a simple IVP. This pair of Matlab m-files represents another example of numerical integration of a single first order differential equation within the Matlab environment.

Fig. 1.5 Another comparison of numerical and analytical solutions.

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 36: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 36

Table 1.5 Listing of the Matlab files for Example 1.8.

% % ODENEX2.M Solution of ODEs with MATLAB (IVP) % % This demo gives an example of how to use the ODE23 routine to integrate % a simple first order differential equation. A slight extension to vectors % allows the solution of systems of first order ODEs. This extension will be % treated in later examples (see section on General IVPs). % % also see ODENEX2A.M - function file for use in ODE23 % % This example solves y' = (x^2 + 3y^2)/(2xy) with y(1) = 2 % and compares to exact soln y(x) = x*sqrt(5x-1) % % File prepared by J. R. White, UMass-Lowell (July 2003) % % % getting started clear all, close all, nfig = 0; % % initial setup xo = 1; xf = 3; yo = 2; tol = .0001; % % evaluate exact solution Nx = 31; xe = linspace(xo,xf,Nx); ye = xe.*sqrt(5*xe-1); % % now evaluate numerical solution (using ODE23) options = odeset('RelTol',tol); [x1,y1] = ode23('odenex2a',[xo,xf],yo,options); % % now plot results (numerical(points), exact(solid)) figure(1) plot(xe,ye,'b-',x1,y1,'ro','LineWidth',2) title('ODEnex2: Numerical & Analytical Solutions to 1st Order ODE') xlabel('x values'),ylabel('y(x)'),grid legend('EXACT','ODE23'); % % end of simulation % % ODENEX2A.M Function evaluation for ODE step in ODENEX2.M % % Evaluate y' = (x^2 + 3y^2)/(2xy) % % File prepared by J. R. White, UMass-Lowell (July 2003) % function yp = odefile(x,y) yp = (x^2 + 3*y^2)/(2*x*y); % % end of function %

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 37: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 37

Example 1.9 -- Numerical Solution Methods (Flow From a Damaged Oil Tank)

Problem Description:

A tanker truck loaded with oil is involved in what is believed to be a minor highway accident. However, the accident damaged the plug on the discharge pipe at the lowest point of the tanker, leaving the equivalent of a one-inch diameter hole spilling oil on the ground. Find the relationship for the amount of oil that leaks from the tank as a function of time after the plug is damaged. If it takes the response team 50 minutes to stop the leak, determine how much oil will be lost?

Assume that the tank is a horizontal cylinder with a length of 5.6 m and a diameter of 2.1 m. The oil has a density of 800 kg/m3. The tank initially has 5000 gallons of fuel oil and the top surface of the oil is at atmospheric pressure as shown in Fig 1.6.

Fig. 1.6 A rough sketch of the side and front views of the tank.

Problem Solution:

Model Development

A mass balance for the oil in the tank can be written as

outdM mdt

= −

The mass, M, is the oil density, ρ, times the volume, V, which changes with time. Since the density is constant, we have

dM dV dALdt dt dt

= ρ = ρ

The volume is simply the length times the cross sectional area perpendicular to the tank’s main axis that is occupied by the oil. When the tank is completely full, the area is given by

2

fullDA4

= π

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 38: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 38

However, when only partially filled to a height, h, the cross-sectional area is given by the following development (see Fig 1.6):

In Cartesian coordinates, the relationship between surface radius, R = D/2, and the x, y coordinates along the surface is given by

( )22 2x y R R+ − =

Solving this for x(y) gives

( )2 2 2x R y 2Ry R= − − + 2

or 2x(y) 2Ry y= ± −

Now, focusing on only the positive x values, the shaded area in the front view of the tank for any height, h, is given by

h

0A(h) 2 x(y)dy= ∫

Now we are interested in an expression for dA/dt. We need to use Leibnitz Rule.

------------------------------------

Leibnitz Rule

When differentiating integral expressions that have variable limits of integration, one uses Leibnitz Rule which can be stated as follows:

Given

( )( )

( )b t

a tF(t) x, t dx= φ∫

then with suitable conditions on a(t), b(t), and ( )x, tφ [i.e. that they are well behaved with no discontinuities], we have

( )( )

( ) [ ] [ ]b t

a t

d dF(t) x, t dx b(t), t b(t) a(t), t a(t)dt t dt dt

∂= φ + φ − φ

∂∫d

------------------------------------

Using Leibnitz Rule for differentiation of an integral with variable limits of integration, we have

h

0

dA x(y) dh d2 dy x(h) x(0)dt t dt dt

∂ = + − ∂ ∫ (0)

or, since the first and last terms are zero, this becomes

dA dh2x(h)dt dt

=

Finally, substituting these expressions into the left hand side of the mass continuity equation gives

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 39: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 39

2dM dh2 L 2Rh hdt dt

= ρ −

Now, the mass flow rate out of the tank is given by

out out outm A v= ρ

Performing an energy balance on a small control volume at the exit gives

( ) ( )2 21 2 1 2

m m0 m u v v m g z P P2

= ∆ + − + ∆ + −ρ

where it is assumed that the energy and mass storage in this small control volume are zero. If we also assume negligible change in internal energy and elevation (i.e. u∆ and ∆ are zero), and that the oil velocity in the tank just prior to the exit (denoted as v

z1) is small compared to the exit

velocity (denoted as v2), then the conservation of energy equation indicates that the change in flow work across the orifice is related to the increase in fluid velocity across the open plug, or

( )2 out 1 22v v P P= = −ρ

However, since the pressure at the bottom of the tank is simply the atmospheric pressure at the top plus the pressure due to a column of oil of height h, we have

1 aP P g= + ρ h

Since the opening is also at atmospheric pressure, (i.e. P2 = Pa), the exit velocity is given as

outv 2= gh

Finally, the overall mass balance gives

2out

dh2 L 2Rh h A 2ghdt

ρ − = −ρ

or out out2

2ghdh A A 2gdt 2L 2L D hDh h

= − = − −−

with the initial condition, , being determined by the tank geometry and the amount of oil initially stored in the tank.

0h(0) h=

Solution Method

The above problem is solved numerically using Matlab (see listing of oiltank.m and the associated function files in Table 1.6). The program logic utilized in oiltank.m is outlined as follows:

1. Specify problem data and convert to consistent units.

2. Use the quadl routine in Matlab to determine A(h) via numerical integration for several values of h. Also determine V(h) = L*A(h).

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 40: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 40

3. Use the spline routine in Matlab to perform a spline fit to get a piecewise polynomial expression for V(h).

4. From the initial volume of oil, use fzero (root finding) and ppval (piecewise polynomial interpolation) in Matlab to determine the initial height of the oil in the tank, h0.

5. Use the ode23 routine in Matlab to solve for the oil height versus time, h(t).

6. With h(t), use ppval again to determine the volume of oil remaining versus time, V(t), and the amount of oil spilled from the tank, ( ) ( )( )s oV V t= −V t .

7. From these data, determine the magnitude of the accident as a function of response time. In particular, if it takes 50 minutes to stop the leak nearly 2200 gallons of oil could be lost! This is not a minor accident. Note that this estimate has assumed an orifice coefficient of unity, which gives a conservative evaluation of the oil spilled.

Graphical Results

The key Matlab plots for V(h), h(t), and Vs(t) are all shown in Fig. 1.7. As indicated above, the data for these plots were generated with the main Matlab m-file oiltank.m. This file also requires three function files for use in quadl, fzero, and ode23. The student should carefully study Table 1.6, which contains a listing of the Matlab script and function files, as follows:

oiltank.m -- main program to solve Example 1.9

oiltanka.m -- function file for use with quadl (numerical integration)

oiltankb.m -- function program for fzero (root finding)

oiltankc.m -- function file for ode23 (ODE solver)

Fig. 1.7 Summary results for Example 1.9.

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 41: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 41

This set of Matlab m-files illustrates the solution procedure for a realistic engineering example. This particular problem includes examples of numerical integration of functions, curve fitting using cubic splines, root finding, piecewise polynomial interpolation, and numerical solution of a single first order differential equation within the Matlab environment. The combination and proper integration of several tools within the overall solution procedure is very common for realistic engineering applications. Thus, a full understanding of this problem should aid in the solution of similar problems in the future.

Table 1.6 Listing of the Matlab files used to solve Example 1.9.

% % OILTANK.M Modeling of Flow from a Damaged Oil Tank % % This demo models and evaluates the flow of oil from a horizontal tank. This % simulation requires several numerical procedures including: % 1. numerical integration of a fairly complicated function (QUADL) % 2. spline fitting, root finding, and piecewise polynomial interpolation (SPLINE, % FZERO, and PPVAL) % 3. numerical solution of a first order differential equation (ODE23) % % The cross sectional area of the horizontal cylindrical tank is given by % A(h) = 2*int[x(y)dy] for 0 < y < h % where x(y) = sqrt(Dy - y^2) % This integral is performed numerically for several values of h using QUADL. % This gives a relationship for A(h) and V(h) = L*A(h). % % The volume of oil in the tank, V(h) = L*A(h), is then fit to a set of piecewise % cubic polynomials using SPLINE. Then, knowing the initial volume of oil, one can % determine the initial height of oil in the tank, ho. This is done using FZERO % to find the root of f(h) = V(h) - Vo = 0. PPVAL is also used here to perform the % piecewise polynomial interpolation - evaluating V(h) for arbitrary h. % % With the initial height known, ODE23 is used to numerically integrate the % differential equation that represents a mass balance on the oil in the tank % [written in terms of h(t)]. The equation of interest here is % dh/dt = -(Aout/2L)*sqrt(2g/(D-h)) with h(0) = ho % % Finally, now that h(t) is known, PPVAL is used again to determine V(t) = V(h(t)) % and to evaluate the amount of oil that is spilled from the tank, Vs(t) = Vo-V(t). % % This main m-file also refers to the following function files: % oiltanka.m - function file for QUADL routine % oiltankb.m - function file for FZERO routine % oiltankc.m - function file for ODE23 routine % % File prepared by J. R. White, UMass-Lowell (July 2003) % % % getting started clear all, close all, nfig = 0; global PP Vo % % specify problem data and convert to consistent units DIAM = 2.1; % tank diameter (m) LENGTH = 5.6; % tank length (m) Vo = 5000; % initial volume of oil (gallons) cg2m3 = 3.785*0.001; % conversion factor (gallons to m^3) Vo = Vo*cg2m3; % initial volume of oil (gallons converted to m^3) Dout = 1.0; % diameter of opening (inches) cin2m = 2.54/100; % conversion factor (inches to m) Dout = Dout*cin2m; % diameter of opening (inches converted to m) AOUT = pi*Dout*Dout/4; % area of opening (m^2) GACCEL = 9.8; % gravitational accel (m^2/sec) Tresp = 50; % response time (minutes) Tresp = Tresp*60; % response time (minutes converted to seconds) hmin = 0; hmax = DIAM; % min & max value for height of oil (m) %

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 42: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 42

% find volume of oil versus height of oil Nh1 = 11; hd1 = linspace(hmin,hmax,Nh1); Vh1 = zeros(size(hd1)); for i = 2:Nh1 Vh1(i) = 2*LENGTH*quadl('oiltanka',hmin,hd1(i),[],[],DIAM); end % % find piecewise cubic polynomial fit to discrete data PP = spline(hd1,Vh1); % % plot oil volume versus oil height (over fine grid for smooth curve) Nh2 = 51; hd2 = linspace(hmin,hmax,Nh2); Vh2 = ppval(PP,hd2); Vhg = Vh2/cg2m3; % oil volume in gallons nfig = nfig+1; figure(nfig) subplot(2,1,1),plot(hd2,Vhg,'b-','LineWidth',2),grid range = axis; range(2)= hmax; axis(range); title('OilTank: Volume of Oil Versus Oil Height') xlabel('Oil Height (m)'),ylabel('Oil Volume (gallons)') % % knowing the initial oil volume, Vo, determine the initial height, ho ho = fzero('oiltankb',hmax); disp('Initial oil height (m) = '), ho % % now, with ho known, solve the differential balance equation for h(t) to = 0; tf = 120*60; tol = 0.00001; options = odeset('RelTol',tol); [t,h] = ode23('oiltankc',[to tf],ho,options,AOUT,LENGTH,GACCEL,DIAM); % % plot h(t) subplot(2,2,3),plot(t/60,h,'b-','LineWidth',2),grid range = axis; range(2)= 120; axis(range); title('Height of Oil Versus Time') xlabel('Time (min)'),ylabel('Oil Height (m)') % % now find and plot amount of oil spilled Vs = Vo-ppval(PP,h); Vsg = Vs/cg2m3; % spilled oil volume in gallons subplot(2,2,4),plot(t/60,Vsg,'b-','LineWidth',2),grid range = axis; range(2)= 120; axis(range); title('Volume of Oil Spilled Versus Time') xlabel('Time (min)'),ylabel('Oil Spilled (gallons)') % range = axis; xt = range(1) + 0.40*(range(2)-range(1)); yt1 = range(3) + 0.20*(range(4)-range(3)); yt2 = range(3) + 0.08*(range(4)-range(3)); text(xt,yt1,['V_o = ',num2str(Vo/cg2m3),' gallons']) text(xt,yt2,['D_{out} = ',num2str(Dout/cin2m),' inches']) % % end simulation % % OILTANKA.M Function evaluation for QUADL step in OILTANK.M % % Evaluate integrand x(y) = sqrt(Dy - y^2) for some y vector in integration range % % File prepared by J. R. White, UMass-Lowell (July 2003) % function x = oiltanka(y,D) x = sqrt(D*y - y.^2); % % end of function

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)

Page 43: Mathematical Methods (10/24.539) I. First Order ... · PDF fileFirst Order Differential Equations Introduction ... and solution of both ordinary and partial differential equations

Math Methods -- Section I: First Order Differential Equations 43

% % OILTANKB.M Function evaluation for FZERO step in OILTANK.M % % Evaluate function f(h) = V(h) - Vo for some h % % File prepared by J. R. White, UMass-Lowell (July 2003) % function f = oiltankb(h) global PP Vo f = ppval(PP,h)-Vo; % % end of function % % OILTANKC.M Function evaluation for ODE step in OILTANK.M % % Evaluate dh/dt = -(Aout/2L)*sqrt(2g/(D-h)) for given t and h % % File prepared by J. R. White, UMass-Lowell (July 2003) % function hp = oiltankc(t,h,flag,A,L,G,D) hp = -(A/(2*L))*sqrt(2*G/(D-h)); % % end of function%

Lecture Notes for Math Methods by Dr. John R. White, UMass-Lowell (updated July 2003)