Modeling Time Dependent Systems in MS Excel

28
Modeling Time Dependent Systems in MS Excel Jake Blanchard University of Wisconsin – Madison [email protected]

description

Modeling Time Dependent Systems in MS Excel. Jake Blanchard University of Wisconsin – Madison [email protected]. Outline. Sample problems Solution techniques for Differential Equations Cellular Automata exercises. Files. The following files can be downloaded from my web site - PowerPoint PPT Presentation

Transcript of Modeling Time Dependent Systems in MS Excel

Page 1: Modeling Time Dependent Systems in MS Excel

Modeling Time Dependent Systems in

MS Excel

Jake BlanchardUniversity of Wisconsin – Madison

[email protected]

Page 2: Modeling Time Dependent Systems in MS Excel

Outline Sample problems Solution techniques for Differential

Equations Cellular Automata exercises

Page 3: Modeling Time Dependent Systems in MS Excel

Files The following files can be downloaded

from my web site These powerpoint slides Word Doc on Differential Equations and

Cellular Automata Spreadsheet on Differential Equations Spreadsheet on Cellular Automata

http://silver.neep.wisc.edu/~jake/workshop

Page 4: Modeling Time Dependent Systems in MS Excel

Some Sample Problems Filling a Water Tank Radioactive Decay Carbon Dating Terminal Velocity Population Growth Worm Population Growth

Page 5: Modeling Time Dependent Systems in MS Excel

The water tank

water levelorifice

h

ghaQindt

dhA 2

Qin

A=area of tank

a=effective area of orifice

Page 6: Modeling Time Dependent Systems in MS Excel

Radioactive Decay

2/1

)2ln(.

tconstdecay

atomsofnumberN

rateproductionR

NRdt

dN

Page 7: Modeling Time Dependent Systems in MS Excel

Carbon Dating All living organisms have a fixed ratio of

radioactive carbon to stable carbon After they die, the ratio changes as the

radioactive carbon decays This can be used to determine the age

of formerly living things Procedure: determine initial activity,

guess age, solve DE to determine current activity, update guess for age until activity matches measured activity

Page 8: Modeling Time Dependent Systems in MS Excel

Terminal Velocity Falling objects reach a terminal

velocity when drag forces match gravity

5.02

1 2

D

D

C

AVCmgdt

dVm

Page 9: Modeling Time Dependent Systems in MS Excel

Population Growth

1001

PP

dt

dP

Growth

Rate

Reduction Due to

Overcrowding

Page 10: Modeling Time Dependent Systems in MS Excel

Procedure for Solving DE’s

time

y

h

Page 11: Modeling Time Dependent Systems in MS Excel

4th Order Runge Kutta Scheme

6

))(*2()()(

),(*

)2

,2

(*

)2

,2

(*

),(*

4321

34

23

12

1

kkkktyhty

kyhtfhk

ky

htfhk

ky

htfhk

ytfhk

),( ytfdt

dy

Page 12: Modeling Time Dependent Systems in MS Excel

VBA routine

Function f(t, y) r = [h10].Value lambda = [h12].Value f = r - lambda * yEnd Function

Function rk(h, t, y) k1 = h * Module6.f(t, y) k2 = h * Module6.f(t + h / 2, y + k1 / 2) k3 = h * Module6.f(t + h / 2, y + k2 / 2) k4 = h * Module6.f(t + h, y + k3) rk = y + (k1 + 2 * (k2 + k3) + k4) / 6End Function

Grab value from worksheet

These must

match

Calls f(t,y) from Module

6

Page 13: Modeling Time Dependent Systems in MS Excel

Simple Worksheett N

0 =n0=$A8+E$14 =module6.rk(E$14,$A8,$B8)=$A9+E$14 =module6.rk(E$14,$A9,$B9) R 1 atoms/s=$A10+E$14 =module6.rk(E$14,$A10,$B10) half-life 5 s=$A11+E$14 =module6.rk(E$14,$A11,$B11) lambda =LN(2)/E11 1/s=$A12+E$14 =module6.rk(E$14,$A12,$B12) initial number 3 -=$A13+E$14 =module6.rk(E$14,$A13,$B13) time step 1 s

t N0 3.0001 3.5452 4.020 R 1 atoms/s3 4.434 half-life 5 s4 4.793 lambda 0.14 1/s5 5.107 initial number 3 -6 5.379 time step 1 s

Page 14: Modeling Time Dependent Systems in MS Excel

Another Approach Have macro carry out several steps

and write solution back to sheet Can add button on sheet to run

macro View/Toolbars/Forms

Page 15: Modeling Time Dependent Systems in MS Excel

The Calling RoutineSub rungekutta_first() Range("b13", Range("c13").End(xlDown)).Clear [b13].Select steps = [g4].Value tnot = [g5].Value tend = [g6].Value Yo = [g7].Value h = (tend - tnot) / steps t = 0 y = Yo For i = 1 To steps Call Module5.rk2(h, t, y, ynew) ActiveCell.Offset(i - 1, 0).Value = t ActiveCell.Offset(i - 1, 1).Value = y t = t + h y = ynew Next [a1].SelectEnd Sub

Page 16: Modeling Time Dependent Systems in MS Excel

SpreadsheetFilling a tank with waterSee Module 5

A 1 m^2 steps= 200Qin 1 m^3/s tnot= 0

a 0.05 m^2 tend= 200g 9.81 m/s^2 No= 0

omega 0.1 1/s

t h0 01 0.8418992 1.5260773 2.1037244 2.592627

Run

ghaQindt

dhA 2

Page 17: Modeling Time Dependent Systems in MS Excel

Exercises Radioactive Decay

PET scans are used more and more for studying brain activity

18F produces positrons – it has a half-life of 1.8 hours

If I need 1 gram of 18F, at what rate must I produce it and how long will it take me to accumulate 1 gram?

How much of what I produce decays before I complete production?

Page 18: Modeling Time Dependent Systems in MS Excel

Exercises Water Tank

Suppose a tank has an area of 1 m and there is an outlet pipe with an area of 4 cm2

At what rate must I fill the tank to achieve a height of 2 m in the tank?

Starting from scratch, how fast will the tank fill at this flow rate?

Page 19: Modeling Time Dependent Systems in MS Excel

Exercises Terminal Velocity

What is the terminal velocity of a 1 cm diameter hailstone? How about 2 cm?

What is the terminal velocity of a 1 cm steel ball?

What would be the terminal velocity of a 1 cm steel ball on the moon?

What would be the terminal velocity of a 1 cm steel ball in water?

Page 20: Modeling Time Dependent Systems in MS Excel

Exercises Carbon Dating

If an old sample has an activity of 2.1 and a new sample has an activity of 5.3, what is the age of the sample?

What is the age of a similar sample with an activity of 1.05?

Page 21: Modeling Time Dependent Systems in MS Excel

Cellular Automata These model a system by changing the

state of a cell depending on the states of its nearest neighbors

A typical model uses a 2 by 2 grid of cells Each cell has a representative value

which indicates its state The model then steps through time,

updating all the states in each step Stephen Wolfram (of Mathematica fame)

just wrote a lengthy book on these

Page 22: Modeling Time Dependent Systems in MS Excel

Example 1 - Population Each cell is either a “1”

(populated) or “0” (unpopulated) If a populated cell has more than 5

neighbors (overcrowded) or less than 2 (isolated), it perishes

Others spawn children into empty neighboring cells (with assumed probability)

http://classes.entom.wsu.edu/529/529Homework4.html

Page 23: Modeling Time Dependent Systems in MS Excel

Example 2 - Fire Cell values are:

3-burning 2-burnt 1-re-grown 0-susceptible

Susceptible cells with a burning neighbor burn in next step

Other values are reduced by 1http://www.ecu.edu/si/cd/excel/tutorials/forestfire_model.html

Page 24: Modeling Time Dependent Systems in MS Excel

Solution Approach Models generally consist of series

of “If” statements These can be done right in cells,

but formulas get convoluted I prefer to use VBA macros

Page 25: Modeling Time Dependent Systems in MS Excel

Sample Fire MacroFor i = 2 To ncells - 1 For j = 2 To ncells - 1 If values(i, j) <> 0 Then nuvalues(i, j) = values(i, j) - 1 Else c = (values(i - 1, j - 1) - 3) c = c * (values(i, j - 1) - 3) c = c * (values(i + 1, j - 1) - 3) c = c * (values(i - 1, j) - 3) c = c * (values(i + 1, j) - 3) c = c * (values(i - 1, j + 1) - 3) c = c * (values(i, j + 1) - 3) c = c * (values(i + 1, j + 1) - 3) If c = 0 Then nuvalues(i, j) = 3 Else nuvalues(i, j) = 0 End If End If NextNext

Page 26: Modeling Time Dependent Systems in MS Excel

Sample “Fire” Sheet

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 3 3 2 1 0 0 0 1 2 3 0 0 0 0 3 2 1 1 1 00 2 2 2 1 0 0 0 1 2 3 0 0 0 0 3 2 1 0 0 00 1 1 1 1 1 1 1 1 2 3 3 3 3 3 3 2 1 0 0 00 1 0 0 0 1 2 2 2 2 2 2 2 2 2 2 2 1 0 0 00 1 0 0 0 1 2 3 3 3 2 1 1 1 1 1 2 1 1 1 00 1 0 0 0 1 2 3 3 3 2 1 0 0 0 1 2 2 2 2 00 1 1 1 1 1 2 2 2 2 2 1 0 0 0 1 2 3 3 3 00 2 2 2 2 1 1 1 1 1 2 1 0 0 0 1 2 3 0 0 00 3 3 3 2 1 0 0 0 1 2 1 1 1 1 1 2 3 0 0 00 0 0 3 2 1 0 0 0 1 2 2 2 2 2 2 2 3 0 0 00 0 0 3 2 1 0 0 0 1 2 2 2 2 2 2 2 3 0 0 00 0 0 3 2 1 1 1 1 1 2 1 1 1 1 1 2 3 0 0 00 0 0 3 2 2 2 2 2 2 2 1 0 0 0 1 2 3 0 0 00 3 3 3 3 3 3 3 3 3 2 1 0 0 0 1 2 3 0 0 00 2 2 2 2 2 2 2 3 3 2 1 0 0 0 1 2 3 0 0 00 2 1 1 1 1 1 2 3 3 2 1 1 1 1 1 2 3 0 0 00 2 1 0 0 0 1 2 3 3 2 2 2 2 2 2 2 3 0 0 00 2 1 0 0 0 1 2 3 3 3 3 3 3 3 3 3 3 0 0 00 2 1 0 0 0 1 2 3 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Step

Reset

Use conditional formatting to color cells

Page 27: Modeling Time Dependent Systems in MS Excel

Exercises Fire

What will a fire in the center do? What will happen with random initial

conditions? What will a U-shaped fire do? What if we start with this in the center?

3 2 1 2 3

3 2 1 2 3

3 2 2 2 3

3 3 3 3 3

Page 28: Modeling Time Dependent Systems in MS Excel

Wrap-Up Lots of interesting problems can be

solved using either differential equations or cellular automata

If you would like help developing more of these, feel free to contact me