Lecture #14 MATLAB
Transcript of Lecture #14 MATLAB
![Page 1: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/1.jpg)
1
Math 211Math 211
Lecture #14
MATLAB’s ODE Solvers
September 27, 2002
![Page 2: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/2.jpg)
Return
2
SolversSolvers
MATLAB has several solvers.
![Page 3: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/3.jpg)
Return
2
SolversSolvers
MATLAB has several solvers.
• ode45
![Page 4: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/4.jpg)
Return
2
SolversSolvers
MATLAB has several solvers.
• ode45
� This is the first choice.
![Page 5: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/5.jpg)
Return
2
SolversSolvers
MATLAB has several solvers.
• ode45
� This is the first choice.
• ode23
![Page 6: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/6.jpg)
Return
2
SolversSolvers
MATLAB has several solvers.
• ode45
� This is the first choice.
• ode23
• Stiff solvers
![Page 7: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/7.jpg)
Return
2
SolversSolvers
MATLAB has several solvers.
• ode45
� This is the first choice.
• ode23
• Stiff solvers
� ode15s
![Page 8: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/8.jpg)
Return Solvers
3
ode45ode45
![Page 9: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/9.jpg)
Return Solvers
3
ode45ode45
• Uses variable step size.
![Page 10: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/10.jpg)
Return Solvers
3
ode45ode45
• Uses variable step size.
� Specify error tolerance instead of step size.
![Page 11: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/11.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/12.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/13.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/14.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/15.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/16.jpg)
Return
4
Solving SystemsSolving Systems
![Page 17: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/17.jpg)
Return
4
Solving SystemsSolving Systems
• Example:
x′ = v
v′ = −9.8 − 0.04v|v|
![Page 18: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/18.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/19.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/20.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/21.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/22.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/23.jpg)
Return
7
Computing and Plotting Solutions to
Systems
Computing and Plotting Solutions to
Systems
![Page 24: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/24.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/25.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/26.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/27.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/28.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/29.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/30.jpg)
Return
8
Solving Higher Order EquationsSolving Higher Order Equations
![Page 31: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/31.jpg)
Return
8
Solving Higher Order EquationsSolving Higher Order Equations
• Reduce to a first order system and solve the system.
![Page 32: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/32.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/33.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/34.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/35.jpg)
Return Higher order
9
Equivalent First Order SystemEquivalent First Order System
![Page 36: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/36.jpg)
Return Higher order
9
Equivalent First Order SystemEquivalent First Order System
θ′ = ω
ω′ = − g
Lsin θ − Dω
![Page 37: Lecture #14 MATLAB](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/37.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/38.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/39.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022012802/61bd135661276e740b0f1992/html5/thumbnails/40.jpg)
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];