ma/cs 375 fall 20021 ma/cs 375 fall 2002 lecture 13

Post on 25-Dec-2015

220 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

MA/CS 375 Fall 2002 1

MA/CS 375

Fall 2002

Lecture 13

MA/CS 375 Fall 2002 2

Office Hours

• My office hours– Rm 435, Humanities, Tuesdays from 1:30pm to 3:00pm– Rm 435, Humanities, Thursdays from 1:30pm to 3:00pm

• Tom Hunt is the TA for this class. His lab hours are now as follow

– SCSI 1004, Tuesdays from 3:30 until 4:45 – SCSI 1004, Wednesdays from 12:00 until 12:50 – Hum 346 on Wednesdays from 2:30-3:30

MA/CS 375 Fall 2002 3

Ordinary Differential Equation

• Example:

0

?

u a

duu

dt

u T

• we should know from intro calculus that:

• then obviously:

tu t ae

Tu T ae

Review

MA/CS 375 Fall 2002 4

Forward Euler Numerical Scheme

• Numerical scheme:

• Discrete scheme:

1n nnu uu

t

1 1n nu t u where: approximate solution at t=n tnu

Review

MA/CS 375 Fall 2002 5

Direct Proof of Convergence

• Fix T and let delta t drop to zero

• In this case n needs to increase to infinity

1 0

1 0

0 as n

nn

n

T

u t u

Tu

n

e u

Review

T n t

MA/CS 375 Fall 2002 6

Stable Approximations

• 0<dt<1

dt

dt=0.5

dt=0.25

dt=0.125

Review

MA/CS 375 Fall 2002 7

Application: Newtonian Motion

Review

MA/CS 375 Fall 2002 8

Two Gravitating Particle Masses

m1

m2

Each particle has a scalar mass quantity

Review

MA/CS 375 Fall 2002 9

Particle Positions

x1

x2

(0,0)

Each particle has a vector position

Review

MA/CS 375 Fall 2002 10

Particle Velocities

v1

v2

Each particle has a vector velocity

Review

MA/CS 375 Fall 2002 11

Particle Accelerations

a1 a2

Each particle has a vector acceleration

Review

MA/CS 375 Fall 2002 12

N-Body Newtonian Gravitation• For particle n out of N

3

1,2

nn

i Ni n in

i i n i n

d

dtM Gd

dt

xv

x - xv

x x

The force on each particle is a sum of the gravitational force between each other particle

Review

MA/CS 375 Fall 2002 13

N-Body Newtonian Gravitation Simulation

• Goal: to find out where all the objects are after a time T

• We need to specify the initial velocity and positions of the objects.

• Next we need a numerical scheme to advance the equations in time.

• Can use forward Euler…. as a first approach.

Review

MA/CS 375 Fall 2002 14

Numerical Scheme

13

1,2

1

m mi Ni n im m

n nm m

i i n i n

m m mn n n

M Gdt

dt

x - x

v vx x

x x v

For m=1 to FinalTime/dt For n=1 to number of objects

End For n=1 to number of objects

EndEnd

Review

MA/CS 375 Fall 2002 15

planets1.m Matlab script• I have written a planets1.m script.• The quantities in the file are in units of

– kg (kilograms -- mass)– m (meters – length)– s (seconds – time)

• It evolves the planet positions in time according to Newton’s law of gravitation.

• It uses Euler-Forward to discretize the motion. • All planets are lined up at y=0 at t=0• All planets are set to travel in the y-direction at t=0

Review

MA/CS 375 Fall 2002 16

Mercury

VenusEarth

Mercury has nearly completed its orbit. Data shows 88 days. Run for 3 more days and the simulation agrees!!!.

Sun

Review

MA/CS 375 Fall 2002 17

Team Exercise• Get the planets1.m file from the web site• This scripts includes:

– the mass of all planets and the sun– their mean distance from the sun– the mean velocity of the planets.

• Run the script, see how the planets run!• Add a comet to the system (increase Nsphere etc.) • Start the comet out near Jupiter with an initial velocity

heading in system.• Add a moon near the earth.• Extra credit if you can make the comet loop the sun

and hit a planet

Today

MA/CS 375 Fall 2002 18

This Lecture

• More accurate schemes

• More complicated ODEs

• Variable time step and embedded methods used to make sure errors are within a tolerance.

MA/CS 375 Fall 2002 19

Adams-Bashforth Schemes

• In the forward Euler scheme we only used the value of the right hand side at the previous time step.

• i.e. we only used a linear approximation to the time derivative

MA/CS 375 Fall 2002 20

AB Schemes

• Idea: we set

• where If interpolates fn,fn-1,fn-2,..,fn+1-Nstages

• i.e.:

1

1

n

n

tn n

t

y y If t

MA/CS 375 Fall 2002 21

f0

f1

f2

f3

We interpolate the function through thefirst 4 points. Then we integrate under the curve between t=3 and t=4.

AB Schemes Essentially we use

interpolation and a Newton-Cotes quadrature formula to formulate:

1 1

1 1 2

1

1

1 2 3

1

Linear:

Quadratic:

3

2 2Cubic:

23 16 5

12 12 12Quartic:

55 59 37 9

24 24 24 24Quintic:

1901 2774

720 7

n n n n

n n n n n

n n n n n n

n n n

n n n

t ty y f f

t t ty y f f f

t t t ty y f f f

y y t f

f

t ty y f

1 2 3 42616 1274 251

20 720 720 720n n n nt t tf f f f

MA/CS 375 Fall 2002 23

Runge-Kutta Schemes• See van Loan for derivation of RK2 and

RK4.

• I prefer the following (simple) scheme due to Jameson, Schmidt and Turkel (1981):

n

1

y

for : 1:1

y=y

n

n

y

k s

tf y

kend

y y

MA/CS 375 Fall 2002 24

Runge-Kutta Schemes• Beware, it only works when f is a function

of y and not t

• here s is the order of the scheme.

n

1

y

for : 1:1

y=y

n

n

y

k s

tf y

kend

y y

MA/CS 375 Fall 2002 25

Error Estimate

• Matlab has a number of time integrators built in.

• ode23

• ode45

• and others..

MA/CS 375 Fall 2002 26

ode23• For n=1:#timesteps

– ode23 uses two estimates for yn+1 .– A 2nd order RK scheme and a 3rd order RK

scheme are used to build two guesses for yn+1. – If the difference between these two estimates are

within a tolerance ode23 progresses on to calculating yn+2

– If the difference is greater than the specified tolerance, ode23 reduces the dt and tries again. It repeats until the difference is lower than the tolerance.

End

MA/CS 375 Fall 2002 27

Planets Example Using ode23

• Idea: replace our home grown Euler Forward scheme with Matlab’s ode23 scheme in the planets1.m script.

MA/CS 375 Fall 2002 28

Parameters forthe planets asbefore

MA/CS 375 Fall 2002 29

Initial velocities

GatherX,Y,VX,VYinto one vector

MA/CS 375 Fall 2002 30

[T,CoordVel] = ode23(‘forcing’, TimeLimits, CoordVel(:));

Call to Matlab ode23 function

function whichcalculates f(y)

[0 FinalTime]

Vector holdsX,Y,VX,VY

Works in Matlab v6.1.0 … at least

MA/CS 375 Fall 2002 31

Call to ode23Find out the time at each time step

Extract final coordinates and velocities

Plot planet orbits

MA/CS 375 Fall 2002 32

The routine whichcalculates the forcing function.

MA/CS 375 Fall 2002 33

Team Exercise• Grab planets2.m and forcing.m from the

http://useme.org/MA375.html

• Run the script

• Use the Tsteps vector to find out the time step for each integration stage. Plot a graph showing the time step (dt) at each time step.

• Use help to find out how to change the tolerance used by ode23 (hint you will need to use odeset)

• Rerun the simulation with a tolerance of 0.1

MA/CS 375 Fall 2002 34

Application: One-Dimensional Electrostatic Motion

MA/CS 375 Fall 2002 35

Charge Repulsion

• Now we will consider the case of charged particles with the same sign charge

• Instead of attracting each other, the charges repel each other.

MA/CS 375 Fall 2002 36

Particle Accelerations

a1

a2

Each particle has a vector accelerationdirectly away from the other particle

MA/CS 375 Fall 2002 37

Team ProjectQ1) Modify the planets2.m and forcing.m to simulate the

following:– There are N electrically charged particles confined to move in the

x-direction only– Distribute the charges initially at equispaced points in [-1,1]– The equations of motion of the charges are:

1 1

31,

2

1, 0

1, 0

for each n=2,3,...,(N-1)

-

N N

nn

i Ni nn

i i n i n

x t v t

x t v t

dxv

dtx xdv

dt x x

MA/CS 375 Fall 2002 38

Team Project Report Required on 9/25/02

Q1cont) Include the following in one project write up per team:– A scatter plot, with x as the horizontal axis and t as the

vertical axis, showing the paths of all the charged particles using ode23

– A graph showing the size of each time step used by ode23

– Replace ode23 with ode45 and rerun– Plot the ode23 and ode45 (t,x) paths on the same graph.– Plot the ode23 and ode45 time step sizes on the same

graph– Names of team members

top related