Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to...

22
Computational Methods (PHYS 2030) York University Winter 2018 Lecture 8 Instructors: Prof. Christopher Bergevin ([email protected]) Schedule: Lecture: MWF 11:30-12:30 (CLH M) Website: http://www.yorku.ca/cberge/2030W2018.html

Transcript of Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to...

Page 1: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

Computational Methods (PHYS 2030)

York UniversityWinter 2018Lecture 8

Instructors: Prof. Christopher Bergevin ([email protected])

Schedule: Lecture: MWF 11:30-12:30 (CLH M)

Website: http://www.yorku.ca/cberge/2030W2018.html

Page 2: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

Ex. RLC circuit, mass-on-a-spring, quantum harmonic oscillator

à Recall the harmonic oscillator as our (reoccurring) example

Goal:Motivatehowtodealw/theHO

à Howdowedealnumericallyw/thesetypesofODEs?

Page 3: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

Systems of ODEs

Ø Sofar,wehavelimitedourselvestoasinglefirstorderODE.Butwhatabout‘systems’ofequations?

Lorenzequations

dx

dt

= �(y � x)

dy

dt

= rx� y � xz

dz

dt

= xy � bz

SIRmodel

dS

dt= ��IS

dI

dt= �IS � �I

dR

dt= �I

Predator-Prey(Lotka-Volterra equations)

dx

dt

= x(↵� �y)

dy

dt

= �y(� � �x)

§ Whatdoeseachtermphysicallyrepresent?

§ Aretheseequationslinear?Isthereanexactsolution?

§ Whatisthe‘atto-fox’problem?

Page 4: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

Systems of ODEs

Ø Solveintheexactsamewayasbefore,wejusthaveone(ormore)additionalequation(s)tosolveforeachtimestep

% -----------------------------------------------------% User input (Note: All paramters are stored in a structure)P.y0(1) = 3.0; % initial prey populationP.y0(2) = 3.0; % initial predator populationP.a= 1; % prey pop. growth rate const.P.b= 0.5; % predation upon prey rate const.P.c= 5; % predator pop. growth rate const. (negative means loss)P.d= 0.5; % predator pop. growth rate const. due to prey comsumption

% Integration limitsP.t0 = 0.0; % Start valueP.tf = 10.0; % Finish valueP.dt = 0.001; % time step

% ----------------------------------------------------% +++% use built-in ode45 to solve[t y] = ode45('LVfunction', [P.t0:P.dt:P.tf],P.y0,[],P);

figure(1); clf;plot(t,y(:,1)); hold on; grid on;plot(t,y(:,2),'r');xlabel('t'); ylabel('Population size'); legend('Prey','Predator')figure(2); clf; % Phase planeplot(y(:,1), y(:,2)); hold on; grid on; xlabel('Prey'); ylabel('Predator')

function [out1] = LVfunction(t,y,flag,P)% y(1) ... prey% y(2) ... predatorout1(1)= y(1)*(P.a-P.b*y(2)); out1(2)= -y(2)*(P.c-P.d*y(1));out1= out1';

Page 5: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

LVode45EX.m

0 1 2 3 4 5 6 7 8 9 100

5

10

15

20

25

t

Popu

latio

n si

ze

PreyPredator

Page 6: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

LVode45EX.m

0 1 2 3 4 5 6 7 8 9 100

5

10

15

20

25

t

Popu

latio

n si

ze

PreyPredator

0 5 10 15 20 250

2

4

6

8

10

12

Prey

Predator

Phasespace

Page 7: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

http://math.rice.edu/~dfield/dfpp.html

à Phaseplaneanalysisfor(2-D)systemsofODEs

Systems of ODEs

Page 8: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

2nd order ODEsHarmonicoscillator

Ø WhatdoyoudowhenyourODEcontainshigherorderderivatives?

General1st orderODE

General2nd orderODE

Ø Simplybreakdownintoasystemof1st orderODEs,thenproceedasbefore.Thiscanbedonesimplybyintroducinganewvariables

Devries(1994)

e.g.,harmonicoscillator

dx

dt

= v

dv

dt

= �!

2o

x� �v

becomes

then

Page 9: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

% ### HOode45EX.m ### % Numerically integrate the damped/driven harmonic oscillator% m*x''+ b*x' + k*x = A*sin(wt)clear% -----------------------------------------------------% User input (Note: All paramters are stored in a structure)P.y0(1) = 0.0; % initial position [m]P.y0(2) = 1.0; % initial velocity [m/s]P.b= 0.1; % damping coefficient [kg/s]P.k= 250.0; % stiffness [N m]P.m= 0.01; % mass [kg]

% sinusoidal driving termP.A= 0.0; % amplitude [N] (set to zero to turn off)fD= 1.05*sqrt(P.k/P.m)/(2*pi); % freq. (Hz) [expressed as fraction of resonant freq.]

% Integration limitsP.t0 = 0.0; % Start valueP.tf = 3.0; % Finish valueP.dt = 0.0001; % time step% ----------------------------------------------------------------------% +++% spit back out some basic derived quantitiesP.wr= 2*pi*fD; % convert to angular freq.disp(sprintf('Resonant frequency ~%g [Hz]', sqrt(P.k/P.m)/(2*pi)));Q = (sqrt(P.k/P.m))/(P.b/P.m); % quality factordisp(sprintf('Q-value = %g', Q));% +++% use built-in ode45 to solve[t y] = ode45('HOfunction', [P.t0:P.dt:P.tf],P.y0,[],P);

% ------------------------------------------------------% visualizefigure(1); clf;plot(t,y(:,1)); hold on; grid on; xlabel('t [s]'); ylabel('x(t) [m]')% Phase planefigure(2); clf;plot(y(:,1), y(:,2)); hold on; grid on; xlabel('x [m]'); ylabel('dx/dt [m/s]’)

function [out1] = HOfunction(t,y,flag,P)% -----------% y(1) ... position x% y(2) ... velocity dx/dtout1(1)= y(2); out1(2)= -1*(P.b/P.m)*y(2) - (P.k/P.m)*y(1)

+ (P.A/P.m)*sin(P.wr*t); out1= out1’;

Page 10: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

0 0.5 1 1.5 2 2.5 3−6

−4

−2

0

2

4

6

8 x 10−3

t [s]

x(t)

[m]

HOode45EX.m

Page 11: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

0 0.5 1 1.5 2 2.5 3−6

−4

−2

0

2

4

6

8 x 10−3

t [s]

x(t)

[m]

HOode45EX.m

−6 −4 −2 0 2 4 6 8x 10−3

−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

x [m]

dx/d

t [m

/s]

Phasespace

§ Changethedamping§ Changethestiffnessormass§ Changetheinitialconditions§ Turnonthe(non-autonomous)

sinusoidaldrivingterm§ Othertypesofdrivingterms?

(e.g.,animpulse)§ Otherchanges?

Thingstotry:

Page 12: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

French(1971)

à Thinkabouthowyouwouldgoaboutmakingtheseplots…

Steady-statefrequencyresponse

Q isthe‘qualityfactor’Q = wo / g

Resonance

Considerthesinusoidally“driven”case:

Page 13: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

“Impulseresponse”intuitivelydefinedintwodifferent(butequivalent)ways:

1. Timeresponseof‘system’whensubjectedtoanimpulse(e.g.,strikingabellw/ahammer)

2. Fouriertransform ofresultingresponse(e.g.,spectrumofbellringing)

ex.Harmonicoscillator

à Forlinear,time-invariantsystems,impulseresponse(don’tforgetthephase!!)completelycharacterizesthesystemsoutputforanygiveninput(à TransferFunction)

Þ Dampingcausesenergylossfromsystem

Temporalimpulseresponse

Spectralimpulseresponse

Lookingahead (respectralanalysis)

Page 14: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

Summary re ODEs

Stabilityofnumericsolutions

0 1 2 3 4 5 6 7 8 9 100

5

10

15

20

25

t

Popu

latio

n si

ze

PreyPredator

SystemsofODEs

262 OrdinaryDifferential Equations Chap.24

,v,) vr(/o) : vro

, y,) vz?) : vzo

:

i,= f,(t,!t,!2, "',!,) Y,Qo) : Y,o

where y, : dy, f dt , n is the number of first-order differential equations, and y,o is the ini-tial condition associated with the lth equation. When an initial value problem is not speci-fied as a set of first-order differential equations, it must be rewritten as one. For example,consider the classic van der Pol equation:

i - p(r - *'); * x: o

whercpisaparametergreaterthanzero.If wechoose!t=xandyr: dxldt,thenthevander Pol equation becomes

it: lzir: t (1- Y?)Y, - Y,

This initial value problem will be used throughout this chapter to demonstrate aspects ofthe M,qrr.ts ODE suite.

24,2 ODE SUITE SOLVERS

The Merrls ODE suite offers five initial value problem solvers. Each has characteristicsappropriate for different initial value problems. The calling syntax for each solver is iden-tical, making it relatively easy to change solvers for a given problem. A description of eachsolver is given in the following table.

Solver Description

ode23 An explicit, one-step Runge-Kutta low-order (2-3) solver. Suitable for problems that exhibitmild stiffness, problems where lower accuracy is acceptable, or problems where..f(t,y) is notsmooth (e.g., discontinuous).

ode45 An explicit, one-step Runge-Kutta medium-order (4-5) solver. Suitable for nonstiff problemsthat require moderate accuracy. This is typically the Jirst solver to try- on a new problem.

ode113 A multistep Adams-Bashforth-Moulton PECE solver of varying order (1-13). Suitable fornonstiff problems that require moderate to high accuracy involving problems where/(t, y) isexpensive to compute. Not suitable for problems where/(1, y) is not smooth (i.e., where it isdiscontinuous or has discontinuous lower-order derivatives).

ode23s An implicit, one-step modified Rosenbrock solver of order 2. Suitable for stiff probiems wherelower accuracy is acceptable, or whereflt, y) is discontinuous . StiJf problems are generttllydescribed as problems where the underlying time constants varl by several orders ofmagnitude or more.

odel 5s An implicit, multistep numerical differentiation solver of varying order (1-5). Suitable for stiffproblems that require moderate accuracy. This is typically the solver to try if ode45 fails oris too inefficient.

v,:lz=

fr(t, vu vr,

fr(t, vuvr,

243

Matlab’s built-insolvers

Warning:Bewaretheblackbox!

2nd orderODEs

−6 −4 −2 0 2 4 6 8x 10−3

−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

x [m]

dx/d

t [m

/s]

Page 15: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

Exercises

Ø Challenge:Considerthe1-Dtime-independentSchrodingerequationforanelectronboundinananharmonic potential:

Ø ModifytheharmonicoscillatorcodetosimulatethevanderPolsystem

Ø Writeacodetosolvethegravity-drivenpendulum: d2✓

dt2= �

pg/` sin (✓)

Ø WriteacodetoexplicitlyimplementRK4fortheharmonicoscillator

Whataretheinitialconditions?Ordoyouinsteadhaveabetterhandleonthe‘boundaryconditions’?

Page 16: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

Ø We“interact”w/variousclassesofdevicesinavarietyofways

Ø Inessence,thesedevices“acquire”“data”fromtheexternalworldandconvertitto(???)....

ATLASdetector(CERN)

“DataAcquisition”

Page 17: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

DAQ 101

Ø Whatis ‘DAQ’? DataAcQuisition

Ø Basically,inanutshell,usingacomputer(usuallyinaprogrammablefashion)tocollect‘data’

Ø Broadlyinterpreted,DAQisaverycommonthingwedointhemodernworld.e.g.,

§ Takingapicturewithacellphone(i.e.,ChargedCoupledDevice,orCCD)§ ATLASdetectoratCERN§ Many(all?)ofthemeasurementsystemsonRosetta’slandingmodulePhilae§ Usingacomputertorecordyourvoiceforaspectrogram§ Forcetransducertomeasurestiffnessofamaterial§ Voltmeter(incl.usingathermocoupletomeasuretemperature)§ UseanArduino asaTheramin§ ....(thelistcouldgoonandon)

http://www.ni.com/data-acquisition/what-is/

Ø Mostbasicpieces:

Page 18: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

http://www.ni.com/data-acquisition/what-is/

DAQ101

Page 19: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

http://www.ni.com/data-acquisition/what-is/

DAQ101

Page 20: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

DAQ 101

http://www.ni.com/data-acquisition/what-is/

Ø Matlab canbeusedforDAQ,buttherearealotofsoftwareoptions(e.g.,LabView,C,...)

Note:Ourfocusin2030isongeneralcomputationalmethods(i.e.,notsoftwaresyntaxspecific)

Page 21: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls

A/D

Ø Digitizingasignalrequiressampling&convertingfrom‘Analog’to ‘Digital’(A/D,orADC)

Continuoussignal(analog) Discretizedsignal(digital)

Oppenheim&Willsky

Ø Assuch,thereissomesamplerate(SR)andbit-depth(moreonthisinabit,pun!)

Ø Doorswingsbothways:Digitalgoing‘out’requiresD/A(orDCA)

Page 22: Computational Methods (PHYS 2030)This initial value problem will be used throughout this chapter to demonstrate aspects of the M,qrr.ts ODE suite. 24,2 ODE SUITE SOLVERS The Merrls