lecture #14 matlab

40
1 Math 211 Math 211 Lecture #14 MATLAB’s ODE Solvers September 27, 2002

Upload: others

Post on 18-Dec-2021

11 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture #14 MATLAB

1

Math 211Math 211

Lecture #14

MATLAB’s ODE Solvers

September 27, 2002

Page 2: Lecture #14 MATLAB

Return

2

SolversSolvers

MATLAB has several solvers.

Page 3: Lecture #14 MATLAB

Return

2

SolversSolvers

MATLAB has several solvers.

• ode45

Page 4: Lecture #14 MATLAB

Return

2

SolversSolvers

MATLAB has several solvers.

• ode45

� This is the first choice.

Page 5: Lecture #14 MATLAB

Return

2

SolversSolvers

MATLAB has several solvers.

• ode45

� This is the first choice.

• ode23

Page 6: Lecture #14 MATLAB

Return

2

SolversSolvers

MATLAB has several solvers.

• ode45

� This is the first choice.

• ode23

• Stiff solvers

Page 7: Lecture #14 MATLAB

Return

2

SolversSolvers

MATLAB has several solvers.

• ode45

� This is the first choice.

• ode23

• Stiff solvers

� ode15s

Page 8: Lecture #14 MATLAB

Return Solvers

3

ode45ode45

Page 9: Lecture #14 MATLAB

Return Solvers

3

ode45ode45

• Uses variable step size.

Page 10: Lecture #14 MATLAB

Return Solvers

3

ode45ode45

• Uses variable step size.

� Specify error tolerance instead of step size.

Page 11: Lecture #14 MATLAB

Return Solvers

3

ode45ode45

• Uses variable step size.

� Specify error tolerance instead of step size.

� MATLAB chooses the step size at each step to

achieve the limit on the error.

Page 12: Lecture #14 MATLAB

Return Solvers

3

ode45ode45

• Uses variable step size.

� Specify error tolerance instead of step size.

� MATLAB chooses the step size at each step to

achieve the limit on the error.

� The default tolerance is good enough for this course.

Page 13: Lecture #14 MATLAB

Return Solvers

3

ode45ode45

• Uses variable step size.

� Specify error tolerance instead of step size.

� MATLAB chooses the step size at each step to

achieve the limit on the error.

� The default tolerance is good enough for this course.

• Syntax

Page 14: Lecture #14 MATLAB

Return Solvers

3

ode45ode45

• Uses variable step size.

� Specify error tolerance instead of step size.

� MATLAB chooses the step size at each step to

achieve the limit on the error.

� The default tolerance is good enough for this course.

• Syntax

[t,y] = ode45(derfile,[t0, tf ], y0);

Page 15: Lecture #14 MATLAB

Return Solvers

3

ode45ode45

• Uses variable step size.

� Specify error tolerance instead of step size.

� MATLAB chooses the step size at each step to

achieve the limit on the error.

� The default tolerance is good enough for this course.

• Syntax

[t,y] = ode45(derfile,[t0, tf ], y0);

plot(t,y)

Page 16: Lecture #14 MATLAB

Return

4

Solving SystemsSolving Systems

Page 17: Lecture #14 MATLAB

Return

4

Solving SystemsSolving Systems

• Example:

x′ = v

v′ = −9.8 − 0.04v|v|

Page 18: Lecture #14 MATLAB

Return

4

Solving SystemsSolving Systems

• Example:

x′ = v

v′ = −9.8 − 0.04v|v|

• Change to vector notation. (Use MATLAB vector

notation)

Page 19: Lecture #14 MATLAB

Return

4

Solving SystemsSolving Systems

• Example:

x′ = v

v′ = −9.8 − 0.04v|v|

• Change to vector notation. (Use MATLAB vector

notation)

� u(1) = x

Page 20: Lecture #14 MATLAB

Return

4

Solving SystemsSolving Systems

• Example:

x′ = v

v′ = −9.8 − 0.04v|v|

• Change to vector notation. (Use MATLAB vector

notation)

� u(1) = x

� u(2) = v

Page 21: Lecture #14 MATLAB

Return System

5

Derivative m-file ball.mDerivative m-file ball.m

function upr = ball(t,u)

x = u(1);

v = u(2);

xpr = v;

vpr = -9.8 - 0.04*v*abs(v);

upr = [xpr; vpr];

Page 22: Lecture #14 MATLAB

Return System ball.m

6

Derivative m-file ballshort.mDerivative m-file ballshort.m

function upr = ballshort(t,u)

upr = zeros(2,1);

upr(1) = u(2);

upr(2) = -9.8 - 0.04*u(2)*abs(u(2));

Page 23: Lecture #14 MATLAB

Return

7

Computing and Plotting Solutions to

Systems

Computing and Plotting Solutions to

Systems

Page 24: Lecture #14 MATLAB

Return

7

Computing and Plotting Solutions to

Systems

Computing and Plotting Solutions to

Systems• [t,u] = ode45(’ball’,[0,3],[0;50]);

Page 25: Lecture #14 MATLAB

Return

7

Computing and Plotting Solutions to

Systems

Computing and Plotting Solutions to

Systems• [t,u] = ode45(’ball’,[0,3],[0;50]);

• plot(t,u) – plots all of the components versus t.

Page 26: Lecture #14 MATLAB

Return

7

Computing and Plotting Solutions to

Systems

Computing and Plotting Solutions to

Systems• [t,u] = ode45(’ball’,[0,3],[0;50]);

• plot(t,u) – plots all of the components versus t.

• plot(t,u(:,1)) – first component versus t.

Page 27: Lecture #14 MATLAB

Return

7

Computing and Plotting Solutions to

Systems

Computing and Plotting Solutions to

Systems• [t,u] = ode45(’ball’,[0,3],[0;50]);

• plot(t,u) – plots all of the components versus t.

• plot(t,u(:,1)) – first component versus t.

• plot(u(:,1),u(:,2)) – second component versus

the first.

Page 28: Lecture #14 MATLAB

Return

7

Computing and Plotting Solutions to

Systems

Computing and Plotting Solutions to

Systems• [t,u] = ode45(’ball’,[0,3],[0;50]);

• plot(t,u) – plots all of the components versus t.

• plot(t,u(:,1)) – first component versus t.

• plot(u(:,1),u(:,2)) – second component versus

the first. This is a phase plane plot.

Page 29: Lecture #14 MATLAB

Return

7

Computing and Plotting Solutions to

Systems

Computing and Plotting Solutions to

Systems• [t,u] = ode45(’ball’,[0,3],[0;50]);

• plot(t,u) – plots all of the components versus t.

• plot(t,u(:,1)) – first component versus t.

• plot(u(:,1),u(:,2)) – second component versus

the first. This is a phase plane plot.

• plot3(u(:,1),u(:,2),t) – 3-D plot.

Page 30: Lecture #14 MATLAB

Return

8

Solving Higher Order EquationsSolving Higher Order Equations

Page 31: Lecture #14 MATLAB

Return

8

Solving Higher Order EquationsSolving Higher Order Equations

• Reduce to a first order system and solve the system.

Page 32: Lecture #14 MATLAB

Return

8

Solving Higher Order EquationsSolving Higher Order Equations

• Reduce to a first order system and solve the system.

• Example: The motion of a pendulum is modeled by

θ′′ = − g

Lsin θ − Dθ′.

Page 33: Lecture #14 MATLAB

Return

8

Solving Higher Order EquationsSolving Higher Order Equations

• Reduce to a first order system and solve the system.

• Example: The motion of a pendulum is modeled by

θ′′ = − g

Lsin θ − Dθ′.

• Introduce ω = θ′.

Page 34: Lecture #14 MATLAB

Return

8

Solving Higher Order EquationsSolving Higher Order Equations

• Reduce to a first order system and solve the system.

• Example: The motion of a pendulum is modeled by

θ′′ = − g

Lsin θ − Dθ′.

• Introduce ω = θ′. Notice

ω′ = − g

Lsin θ − Dθ′.

Page 35: Lecture #14 MATLAB

Return Higher order

9

Equivalent First Order SystemEquivalent First Order System

Page 36: Lecture #14 MATLAB

Return Higher order

9

Equivalent First Order SystemEquivalent First Order System

θ′ = ω

ω′ = − g

Lsin θ − Dω

Page 37: Lecture #14 MATLAB

Return Higher order

9

Equivalent First Order SystemEquivalent First Order System

θ′ = ω

ω′ = − g

Lsin θ − Dω

• Change to vector notation. (Use MATLAB vector

notation)

Page 38: Lecture #14 MATLAB

Return Higher order

9

Equivalent First Order SystemEquivalent First Order System

θ′ = ω

ω′ = − g

Lsin θ − Dω

• Change to vector notation. (Use MATLAB vector

notation)

� u(1) = θ

Page 39: Lecture #14 MATLAB

Return Higher order

9

Equivalent First Order SystemEquivalent First Order System

θ′ = ω

ω′ = − g

Lsin θ − Dω

• Change to vector notation. (Use MATLAB vector

notation)

� u(1) = θ

� u(2) = ω

Page 40: Lecture #14 MATLAB

Pendulum Short

10

Derivative m-file pend.mDerivative m-file pend.m

function upr = pend(t,u)

L= 1;

global D

th = u(1);

om = u(2);

thpr = om;

ompr = -(9.8/L)*sin(th) - D*om;

upr = [thpr; ompr];