46
Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu NUMERICAL METHODS: SOME SIMULATION ALGORITHMS Quotes of the Day "It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject permits and not to seek an exactness where only an approximation of the truth is possible.” --- Aristotle I. BACKGROUND (1) Why Numerical Methods? Numerical methods are needed for solving complex differential equations used to model physical, biological, and socioeconomic systems because most such models cannot be solved analytically. (2) A Theoretical Basis for Numerical Methods According to the Taylor Theorem, a function Y(x) can be expanded, about a point x = x m , into a series, which is known as the Taylor series: Page 1

SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

NUMERICAL METHODS: SOME SIMULATION ALGORITHMS

Quotes of the Day "It is the mark of an instructed mind to rest satisfied with the degree of precision

which the nature of the subject permits and not to seek an exactness where only an approximation of the truth is possible.”

--- Aristotle

I. BACKGROUND

(1) Why Numerical Methods? Numerical methods are needed for solving complex differential equations used to

model physical, biological, and socioeconomic systems because most such models cannot be solved analytically.

(2) A Theoretical Basis for Numerical Methods

According to the Taylor Theorem, a function Y(x) can be expanded, about a point x = xm, into a series, which is known as the Taylor series:

Page 1

Page 2: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Y (x) =Ym +Ym' (x−xm )+Ym

'' (x−xm )2

2!+Ym

''' (x −xm )3

3!+...... +Ym

(n) (x −xm )n

n!+Yx

(n+1) (x−xm )n+1

(n +1)!Page 2

Page 3: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Page 3

Page 4: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

where

Page 4

Page 5: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Ym =Y(xm )Page 5

Page 6: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

,

Page 6

Page 7: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Ym' ,Ym

' ' ,Ym' ' ' , ...,Ym

(n)

Page 7

Page 8: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

are the first, second, third, ..., and nth derivative of Y(x) evaluated at x = xm, and

Page 8

Page 9: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

xm ≤x ≤xPage 9

Page 10: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

.

The Taylor series expansion provides a theoretical basis for evaluating and comparing commonly used numerical methods. We can use the series to approximate the solution Y(x) at x = xm+∆x simply by replacing x by xm+∆x in the Taylor series. In the context of differential equations describing temporal dynamics, we use t for x and thus have:

Page 10

Page 11: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Page 11

Page 12: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Yt+Δt =Yt+Yt'(Δt)+Yt

'' (Δt)2

2!+Yt

''' (Δt)3

3!+ ...... +Yt

n (Δt)n

n!Page 12

Page 13: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

We can select the first few terms in the Taylor series to approximate the solution of a function, but this will result in truncation error. Apparently, the more terms a procedure uses, the more accurate the approximation is.

(1) First-order approximation:

Page 13

Page 14: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Page 14

Page 15: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Yt+Δt =Yt+Yt'(Δt)+ εT

1Page 15

Page 16: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

,

Page 16

Page 17: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Page 17

Page 18: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

εT1 ≅ K(Δt)2

Page 18

Page 19: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

where

Page 19

Page 20: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

εT1

Page 20

Page 21: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

is the truncation error, and K is a constant. (Note: Compare the above equation with the definition of the first-order derivative!)

(2) Second-order approximation:

Page 21

Page 22: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Page 22

Page 23: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Yt+Δt =Yt+Yt'(Δt)+Yt

'' (Δt)2

2!+ εT

2Page 23

Page 24: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Page 24

Page 25: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

where the truncation error is reduced to

Page 25

Page 26: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

εT2 ≅ K(Δt)3

Page 26

Page 27: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

.

(3) kth-order approximation:

Page 27

Page 28: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Page 28

Page 29: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Yt+Δt =Yt+Yt'(Δt)+Yt

'' (Δt)2

2!+Yt

''' (Δt)3

3!+ ...... +Yt

k (Δt)k

k!+ εT

kPage 29

Page 30: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

in which the truncation error is reduced by several orders of magnitude as compared to that in the first-order approximation.

Page 30

Page 31: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Page 31

Page 32: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

εTk ≅ K(Δt) k+1

Page 32

Page 33: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

II. EULER’S AND RUNGE-KUTTA METHODS

There exist many different numerical methods for solving differential equations (see Dorn and McCracken 1972, Press et al. 1992). Two types of numerical methods can be distinguished: (1) one-step, self-starting methods (e.g., Euler’s and modified Euler’s methods, Runge-Kutta methods) and (2) Multistep, non-self-starting methods (e.g., predictor-corrector methods).

Amongst the simplest and most commonly used are Euler’s method and Runge-Kutta methods. These numerical techniques are all one time-step methods; that is, only information at one time interval is used to estimate change in state variables. They agree with the Taylor series through terms in (∆t)k, where k is the order of the method. For example, k is 2 in the 2nd-order Runge-Kutta methods and 4 in the 4th-order Runge-Kutta method. Thus, Euler’s method may be considered as a 1st-order Runge-Kutta method.

Page 33

Page 34: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

A general procedure of numerical approximation of solutions to differential equations

1. Euler’s Method

Euler’s method is one of the oldest and best known numerical methods for integrating differential equations. It simply uses the estimate of the rate of change (Y’) at time t to calculate the change in the state variable over the time interval (∆t), assuming the rate of change is constant over ∆t. The formula for Euler’s method is simply

Page 34

Page 35: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Page 35

Page 36: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Yt+Δt =Yt+Yt'(Δt)Page 36

Page 37: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Although it is simple and computationally efficient, Euler’s method may introduce significant truncation and integration errors especially when ∆t is relatively large. Besides, it is often unstable, meaning that small roundoff and truncation errors may propagate as simulation time progresses. This error propagation may lead to artifactual behaviors that do not really come from the differential equations.

2. Runge-Kutta Methods

To reduce truncation and integration errors, Runge-Kutta methods require additional evaluations within each iteration. Two most commonly used Runge-Kutta methods are the second- and fourth-order Runge-Kutta Methods. As compared to Euler’s method, the 2nd-order Runge-Kutta method requires the evaluation of the rate of change at two points within a time interval, whereas the 4th-order requires the evaluation at four points within a time interval. The accuracy of numerical solutions is substantially improved at the expense of computational time.

Page 37

∆t2∆t3∆tTimeYY(3∆t)Y(2∆t)Y(∆t)Y(0)∆t∆YY(t+∆t) = Y(t) + Y’∆tY’ = ∆Y/∆t

A geometrical representation of Euler’s method: a series of Euler approximations (straight lines) to a true solution (curved line) over Δt solution intervals (Haefner 1996)

Page 38: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Let

Page 38

Page 39: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Y =F(t) and dYdt

=f(t,Y)Page 39

Page 40: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

, a commonly used formula for the 2nd-order Runge-Kutta is

Page 40

Page 41: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Page 41

Page 42: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Yt+Δt =Yt+12

f(t,Yt) + f(t+Δt,Yt+Δt)[ ]Page 42

Page 43: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

,which simply means that ∆Y is calculated based on an average of the two estimates of the rate of change at two points, t and t+∆t (namely, the beginning and end of a time interval). The fourth-order Runge-Kutta method uses a weighted average of 4 estimates of the rate of change to calculate the value of the state variable over each ∆t. A widely used 4th-order Runge-Kutta formulation is as follows:

Yt+Δt =Yt+16(F1 + F2 + F3 + F4 )

whereF1 = f(t,Yt)Δt

F2 = f(t+Δt2,Yt +

12F1)Δt

F3 =f(t+ Δt2,Yt +

12F2 )Δt

F4 = f(t+Δt,Yt +12F3)Δt

3. Accuracy Comparison of Euler’s and Runge-Kutta Methods

Runge-Kutta methods are usually much more accurate than Euler’s method, but higher-order Runge-Kutta techniques may become rather time-consuming when a small ∆t is used. However, with larger time steps and thus with about the equivalent amount of computational time, integration errors produced by Runge-Kutta methods are usually significantly smaller than those by Euler’s method.

Page 43

Page 44: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

3:51 PM Thu, Oct 4, 2007Untitled Graph

Page 20.00 5.00 10.00 15.00 20.00

Hours

1:

1:

1:

2:

2:

2:

50.00

150.00

250.00

1: Tc sim 2: Tc actual

1

11 1

2

2 2 2

Comparison the simulated solution to the analytical solution using Euler’s Method versus RK-2 Method (DT = 1)

III. NOTES ON SIMULATION ALGORITHMS IN STELLA

STELLA includes three numerical techniques: Euler’s method, and the second- and fourth-order Runge-Kutta methods. Here are some very important points (cf. Technical Documentation of STELLA), which one needs to know when using STELLA for any serious, scientific purposes (not just for fun!).

1. For modeling discrete dynamics or discrete objects, use Euler’s method only. This includes situations in which you use any of STELLA’s discrete objects (queues, conveyors, and ovens), or built-in functions to generate integer values (e.g., IF-THEN-ELSE logic to set 0-1 flags, INT, ROUND, SWITCH, etc). STELLA can be used for simulating discrete systems, but in this case ∆t is usually set to 1 and only Euler’s method has the appropriate mathematical formulation. Runge-Kutta methods are designed for continuous systems, and inappropriate for simulating discrete dynamics.

2. For modeling continuous systems, the Runge-Kutta methods are generally preferable. For those that exhibit oscillations the Runge-Kutta methods must be used. Runge-Kutta methods are not only more accurate, but also are more stable. On the other hand, error propagation in Euler’s method may turn a sustained oscillation into an ever expanding oscillation!

3. The aspects of accuracy and simulation time should be simultaneously considered in choosing a proper ∆t.

REFERENCES

Dorn, W. S. and D. D. McCracken. 1972. Numerical Methods with Fortran IV Case Studies. Wiley, New York.

Page 44

Page 45: SIMULATION MODELINGleml.asu.edu/jingle/web_pages/ecomod_website/lecture... · Web viewThey agree with the Taylor series through terms in (∆t)k, where k is the order of the method

Lecture Notes for Ecological Modeling --- Jianguo (Jingle) Wu

Press, W. H., W.A. Teukolsky, W. T. Vetterling, and B. P. Flannery. 1992. Numerical Recipes in C: The Art of Scientific Computing. 2nd Ed. Cambridge University Press, Cambridge. 994 pp.

Page 45