lecture #14 matlab
TRANSCRIPT
1
Math 211Math 211
Lecture #14
MATLAB’s ODE Solvers
September 27, 2002
Return
2
SolversSolvers
MATLAB has several solvers.
Return
2
SolversSolvers
MATLAB has several solvers.
• ode45
Return
2
SolversSolvers
MATLAB has several solvers.
• ode45
� This is the first choice.
Return
2
SolversSolvers
MATLAB has several solvers.
• ode45
� This is the first choice.
• ode23
Return
2
SolversSolvers
MATLAB has several solvers.
• ode45
� This is the first choice.
• ode23
• Stiff solvers
Return
2
SolversSolvers
MATLAB has several solvers.
• ode45
� This is the first choice.
• ode23
• Stiff solvers
� ode15s
Return Solvers
3
ode45ode45
Return Solvers
3
ode45ode45
• Uses variable step size.
Return Solvers
3
ode45ode45
• Uses variable step size.
� Specify error tolerance instead of step size.
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.
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.
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
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);
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)
Return
4
Solving SystemsSolving Systems
Return
4
Solving SystemsSolving Systems
• Example:
x′ = v
v′ = −9.8 − 0.04v|v|
Return
4
Solving SystemsSolving Systems
• Example:
x′ = v
v′ = −9.8 − 0.04v|v|
• Change to vector notation. (Use MATLAB vector
notation)
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
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
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];
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));
Return
7
Computing and Plotting Solutions to
Systems
Computing and Plotting Solutions to
Systems
Return
7
Computing and Plotting Solutions to
Systems
Computing and Plotting Solutions to
Systems• [t,u] = ode45(’ball’,[0,3],[0;50]);
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.
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.
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.
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.
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.
Return
8
Solving Higher Order EquationsSolving Higher Order Equations
Return
8
Solving Higher Order EquationsSolving Higher Order Equations
• Reduce to a first order system and solve the system.
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θ′.
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 ω = θ′.
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θ′.
Return Higher order
9
Equivalent First Order SystemEquivalent First Order System
Return Higher order
9
Equivalent First Order SystemEquivalent First Order System
θ′ = ω
ω′ = − g
Lsin θ − Dω
Return Higher order
9
Equivalent First Order SystemEquivalent First Order System
θ′ = ω
ω′ = − g
Lsin θ − Dω
• Change to vector notation. (Use MATLAB vector
notation)
Return Higher order
9
Equivalent First Order SystemEquivalent First Order System
θ′ = ω
ω′ = − g
Lsin θ − Dω
• Change to vector notation. (Use MATLAB vector
notation)
� u(1) = θ
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) = ω
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];