Solving Ordinary Differential Equations in...

25
Solving Ordinary Differential Equations in MATLAB Fundamental Engineering Skills Workshops asee.engin.umich.edu John Pitre | PhD Candidate, Biomedical Engineering | University of Michigan | Oct 7, 2013

Transcript of Solving Ordinary Differential Equations in...

Page 1: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Solving Ordinary Differential Equations in MATLABFundamental Engineering Skills Workshops

asee.engin.umich.edu

John Pitre | PhD Candidate, Biomedical Engineering | University of Michigan | Oct 7, 2013

Page 2: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

IntroductionWhat is a differential equation?

f(x) = f

✓x, y,

dy

dx

,

d

2y

dx

2, · · · , d

ny

dx

n

◆x y

dy

dx

,

d

2y

dx

2

d

ny

dx

nx

f(x) = f

✓x, y,

dy

dx

,

d

2y

dx

2, · · · , d

ny

dx

n

◆f(x) = f

✓x, y,

dy

dx

,

d

2y

dx

2, · · · , d

ny

dx

n

◆f(x) = f

✓x, y,

dy

dx

,

d

2y

dx

2, · · · , d

ny

dx

n

◆f(x) = f

✓x, y,

dy

dx

,

d

2y

dx

2, · · · , d

ny

dx

n

◆f(x) = f

✓x, y,

dy

dx

,

d

2y

dx

2, · · · , d

ny

dx

n

◆f(x) = f

✓x, y,

dy

dx

,

d

2y

dx

2, · · · , d

ny

dx

n

◆dy

dx

,

d

2y

dx

2

Independent variableDependent variable

Derivatives of the dependent variable

Page 3: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

IntroductionWhat is an ordinary differential equation?

“In mathematics, an ordinary differential equation or ODE is an equation containing a function of one independent variable and its derivatives.” Wikipedia

d2u

dr2+

1

r

du

dr= 0

@u

@t

+ u

@u

@x

= �1

@p

@x

ODE PDE

Page 4: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

IntroductionA simple example equation

dy

dt= ky

y(0) = y0

The Problem The Solution

y(t) = y0ekt

Separable Equation

Page 5: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

MATLABMotivation for using numerical solvers

The previous example was easy to solve by hand. In most cases though, it’s not so simple.

Page 6: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

MATLABODE45 - “The” MATLAB numerical solver

Runge-Kutta Method

• solves first order systems of ODEs• 4th or 5th order accurate• adaptive step sizing

Syntax:[t,y] = ode45(‘myode’,tspan,y0)

*

*Wikipedia

Page 7: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

MATLABODE45 - “The” MATLAB numerical solver

function dydt = simpleode(t,y) k = 20; %[/hr] dydt = k*y; %[bacteria/hr]end

The Differential Equation

dy

dt= ky

% BACTERIAL GROWTHy0 = 5; %[bacteria]tspan = [0 10]; %[hr][t,y] = ode45(‘simpleode’,tspan,y0);

The Main Programy(0) = y0

ODE45

tt

dydt

y

Page 8: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example Problem #1A simple example, revisitedExponential growth

Page 9: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example #1A simple example equation, revisited

dy

dt= ky

y(0) = y0

The Problem The Solution

y(t) = y0ekt

Separable Equation

Page 10: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example Problem #2Solving a coupled system of ODEsPredator-Prey model

Page 11: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example #2Predator-Prey model

VS

*Peter Trimming *JJ Harrison

Page 12: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example #2Predator-Prey model

0 5 10 15 20 25 30 35 400

10

20

30

40

50

60

70

Time

Popu

latio

n

Page 13: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example #2Predator-Prey (Lotka-Volterra) model

0 5 10 15 20 25 30 35 400

10

20

30

40

50

60

70

TimePo

pula

tion

dx

dt

= (b� py)x

dy

dt

= (rx� d)y

x = preyy = predator

b = prey growth ratep = predation rater = predator growth rated = predator death rate

Page 14: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example #2Predator-Prey (Lotka-Volterra) model

% Define the two coupled equationsdxdt = (b - p*y)*x;dydt = (r*x - d)*y;

% Define the system of equationsdppdt(1) = dxdt;dppdt(2) = dydt;

Let p =

x

y

�. Then p0 =

x

0

y

0

�=

(b� py)x(rx� d)y

�.

In our ODE function file...

Page 15: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example Problem #3Solving a second order ODESpring-mass-damper system

Page 16: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example #3Spring-mass-damper system

k

c

m f(t)

Page 17: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example #3Capacitor-inductor-resistor system

V (t)R

C

L

k

c

m f(t)

Page 18: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example #3Spring-mass-damper system

Fspring

Fdamper

Fmass

= kx

= cx

= mx

k

c

m f(t)

⌃Fmass = mx

Page 19: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example #3Spring-mass-damper system

Fspring

Fdamper

Fmass

= kx

= cx

= mx

⌃Fmass = mx

m f(t)

Fdamper

Fspring

FBD

Page 20: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example #3Spring-mass-damper system

Fspring

Fdamper

Fmass

= kx

= cx

= mx

⌃Fmass = mx

m f(t)

Fdamper

Fspring

mx = �kx� cx+ f(t)

x = � k

m

x� c

m

x+1

m

f(t)

Page 21: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example #3Spring-mass-damper system

Let y = x. Then y = x.

Substitution into the ODE yields : y = � k

m

x� c

m

y +1

m

f(t)

Now, let g =

✓x

y

◆. Then g =

✓x

y

◆=

✓y

� kmx� c

my + 1mf(t)

◆.

The Workaround

Page 22: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Example #3Spring-mass-damper system

Now our second order equation is a system of first order equations: ode45 will work!

g =

✓x

y

◆=

✓y

� kmx� c

my + 1mf(t)

Page 23: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

ExerciseCode your own spring-mass-damper solver

g =

✓x

y

◆=

✓y

� kmx� c

my + 1mf(t)

function f = force(t)% force defines a Heaviside step % force input at time t = t0 % with magnitude M t0 = 5; %[s] M = 2; %[N] f = M*double(t >= t0);end

tt0

f(t)

M

(0, 0)

Page 24: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Questions?Solving ODEs in MATLAB

asee.engin.umich.edu

Page 25: Solving Ordinary Differential Equations in MATLABasee.engin.umich.edu/wordpress/wp-content/.../PresentationMatlabOD… · Solving Ordinary Differential Equations in MATLAB Fundamental

Please help us improve.

asee.engin.umich.edu