Chp9: ODE Solns By MATLAB Engr/Math/Physics 25 Bruce Mayer, PE

advertisement
Engr/Math/Physics 25
Chp9: ODE Solns
By MATLAB
Bruce Mayer, PE
Licensed Electrical & Mechanical Engineer
BMayer@ChabotCollege.edu
Engineering/Math/Physics 25: Computational Methods
1
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Learning Goals
 Use MATLAB’s ODE Solvers to find
Solutions to Ordinary Differential Eqns
 When Possible use Analytical ODE
Solutions to Check the ACCURACY of
the MATLAB Numerical Soln
 Make Approximations to perform a
REALITY CHECK on MATLAB
Solutions to NONLinear ODEs
Engineering/Math/Physics 25: Computational Methods
2
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
MATLAB ODE Solvers
 MATLAB has several 1st Order ODE
Solver Commands:
• ode23
• ode45 (Best OverAll)
• ode113
 These Solvers are based on the
Runge-Kutta method, which is usually
the best technique to apply as a
“first try” for most problems.
Engineering/Math/Physics 25: Computational Methods
3
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Solver Summary
Solver
ode45
Problem Type
Nonstiff
ode23
Nonstiff
ode113
Nonstiff
Low to high
For problems with stringent error tolerances
or for solving computationally intensive
problems.
ode15s
Stiff
Low to medium
If ode45 is slow because the problem is stiff.
ode23s
Stiff
Low
If using crude error tolerances to solve stiff
systems and the mass matrix is constant.
ode23t
Moderately Stiff
Low
For moderately stiff problems if you need a
solution without numerical damping.
Low
If using crude error tolerances to solve stiff
systems.
ode23tb Stiff
Order of Accuracy When to Use
Medium
Most of the time. This should be the first
solver you try.
Low
For problems with crude error tolerances or
for solving moderately stiff problems.
Engineering/Math/Physics 25: Computational Methods
4
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
MATLAB ODE Format - 1
 MATLAB ODE Solver Form for Multiple
Dependent Variables (MultiVar Probs)
dy1
 f1 t , y1 , y2 ,  , ym ,
y1 t0   b1 ,
dt
dy2
 f 2 t , y1 , y2 ,  , ym ,
y2 t0   b2 ,
dt
 m-Eqns (1st order ODEs) in

m-Unknowns
dym
 f m t , y1 , y2 ,  , ym ,
dt
Engineering/Math/Physics 25: Computational Methods
5
ym t0   bm ,
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
1st order ODE System Example
dw
t
2
 e  w  xy  tz
dt
dx
56

 37 wx  8 y t
dt
t  37
w4   33
dy
3
xy1.7
  
 ln  z  3
dt
11
t  66
dz
sin  y 
37
2 w t
 z cost   e

dt
ln t  9 
Engineering/Math/Physics 25: Computational Methods
6
x4   88
y 4   22
y 4   55
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
MATLAB Solution
 To solve this MultiODE system using MATLAB
the functions f1, f2, …, fm must be provided to
the computer, along with the initial values of
the variables; i.e., t0 and b1,b2, …, bm
 The functions f1, f2, …, fm are input using a
function which we have to name, say
fcn_vec. Then stored in an m-file called in
this case fcn_vec.m
 The initial values of the y’s are stored in a one
dimensional COLUMN vector, say y0
Engineering/Math/Physics 25: Computational Methods
7
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
MATLAB Syntax
 Obtain the Solution by typing in the
command window:
[t,y]=ode45(@fcn_vec,Trng,y0)
 Where Trng  is a vector containing the
initial and final values for t.
• Example: Trng = [T0 Tf]
– Where T0 is set to be t0 and Tf is set to be the
final time at the end of the interval of interest.
Engineering/Math/Physics 25: Computational Methods
8
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Plotting the Solution
 MATLAB can also be used to plot the
ODE Solution results.
 For example: plot(t,y) gives a plot
of ALL components of the solution y1,
y2, …, ym , as a function of t
 Alternatively plot(t,y(:,1))gives a
plot of y1 as a function of t
Engineering/Math/Physics 25: Computational Methods
9
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Comments on ode45
 Note that in its simplest form ode45 chooses
its OWN time step, Δt, and VARIES the time
step according to how fast the solution is
changing (the steepness of the slope) .
 Thus ode45 generates solution values at a
sequence of times t1, t2, t3, … given by tk+1=
tk+Δtk, with Δtk selected by ode45.
• Thus EACH component of the solution y1, y2, …,
yn is ITSELF a vector containing values such as
y1(t1), y1(t2), y1(t3), …, then y2(t1), y2(t2), y2(t3), …,
then y3(t1), y3(t2), y3(t3), etc.
Engineering/Math/Physics 25: Computational Methods
10
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Example  ode45 (1)
 Solve 2nd
ORDER ODE


y  2 y  5 y  sin t
 With ICs
y 0   68

dy
 y 0   2
dt t 0
• i.e.,
𝑅𝐼𝑆𝐸
𝑅𝑈𝑁
=2
Engineering/Math/Physics 25: Computational Methods
11
 Note that to solve a
2nd order eqn we need
to know the SLOPE
(dy/dt) at t = 0
 To use the 1st Order
Solver, cast this 2nd
Order eqn into 1st
order (state Var) form
LET: x1  y

x2  y
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Example  ode45 (2)
 With the Xform
x1  y

x2  y



dx1 dy 
x1 

 y  x2
dt
dt
 ReArranging the
ODE to isolate
Highest order term

y  sin t  5 y  2 y
Engineering/Math/Physics 25: Computational Methods
12

y  x2
 Find that

 Subbing in the x1 & x2
x2  sin t  5 x1  2 x2
 Thus the 1st Order
Eqn System in 2 Vars

dx1
x1 
 x2
dt

dx2
x2 
 sin t  5 x1  2 x2
dt
y 0   x1 0   68

y 0   x2 Bruce
0  Mayer,
2 PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Example  ode45 (3)
 Note that applying
this Xform

x1  y x2  y
 Converted the
SINGLE 2nd Order
ODE to a LINEAR
System of TWO 1st
Order ODEs

dx1 dy

 y  x2
dt dt
Engineering/Math/Physics 25: Computational Methods
13
 And also dx2/dt
dx2 d     
  y   y  x2
dt dt  
 Sub into ODE


y  2 y  5 y  sin t
dx2
 2 x 2 5 x1  sin t
dt
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Example  ode45 (4)
 Now Isolate dx1/dt
dx1 dy 

 y  x2
dt dt
dx1
 x2
dt
 Next Isolate dx2/dt
dx2
 2 x 2 5 x1  sin t
dt
dx2
 sin t  5 x1  2 x 2
dt
 Recall the IC’s
y 0   x1 0   68
Engineering/Math/Physics 25: Computational Methods
14

y 0   x2 0   2
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Example  ode45 (5)
 Thus the Transformation to State-Var
Form of Two 1st
y 0   68
Order ODEs



dy
y  2 y  5 y  sin t &
 y 0   2
dt t 0
 The final xForm
dx1
 x2
dt
dx2
 sin t  5 x1  2 x2
dt
Engineering/Math/Physics 25: Computational Methods
15
x1 t  0   68
x 2 t  0   2
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Example  ode45 (6)
 Compare xForm to Slide-4
dx1
 x2
x1 t  0   68
dt
dx2
 sin t  5 x1  2 x2
x 2 t  0   2
dt
dy1
 f1 (t , y1 , y2 ),
dt
dy2
 f 2 (t , y1 , y2 ),
dt
Engineering/Math/Physics 25: Computational Methods
16
y1 (t0 )  b1 ,
y2 (t0 )  b2 ,
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
 The Function File = xdot_lec24.m
Example  ode45 (7)
function xd = xdot_lec24(t_val,y_vals);
% Bruce Mayer, PE * 05Nov11
% ENGR25 * Lec24 on MATLAB ODE solvers
%
%This is the function that makes up the system
%of differential equations solved by ode45
%
% the Vector y_vals contains yk & [dy/dt]k
%
% DEBUG Section
%Ttest = t_val; y_vals_test = y_vals; xd
%
xd(1)=y_vals(2); % at t=0, xdot(1) = dy(0)/dt
xd(2)= sin(t_val) -5*y_vals(1) - 2*y_vals(2);
% at t=0, xdot(2) = d2y(0)/dt2
%
% Must return a COLUMN Vector
xd = [xd(1); xd(2)];
%
% DEBUG Section
disp('xd(1) = dy/dt ='), disp(xd(1))
disp('xd(2) = d2y/dt2 = '), disp(xd(2))
Engineering/Math/Physics 25: Computational Methods
17
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Example  ode45 (8)
% Bruce Mayer, PE * 05Nov11
% ENGR25 * Lec24 on MATLAB ODE solvers
% file = Demo_ODE_Lec24.m
% Revised to include a set of non-zero ICs
%
%This script file calls FUNCTION xdot_lec24
%
clear
% clear memory
%
% CASE-I => set the IC's y(0) & dy(0)/dt as COL Vector
% y0=[0; 0]; % comment-out if Not Used
% CASE-II => set the IC's y(0) & dy(0)/dt as COL Vector
Y0 = [-0.19; -0.73]; % Comment-Out if Not Used
%
% Default Time Interval of 20 Time-Units; user can change this
tmax = input('input tmax = ')
trng = [0, tmax];
%
%
%Call the ode45 routine with the above data inputs
[t,y]=ode45('xdot_lec24', trng, y0);
%
%Plot the first column of the solution “matrix”
%giving x1 or y.
plot(t,y, 'LineWidth', 2), xlabel('t'), ylabel('ODE Solution y(t) & dy/dt'),...
title('ODE Example - Lecture24'), grid, legend('y(t)','dy/dt')
Engineering/Math/Physics 25: Computational Methods
18
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
ODE Example
Result
(0 for ICs)
ODE Example
- Lecture23
0.25
0.2
ODE Solution, y(t)
0.15
0.1
0.05
0
-0.05
-0.1
-0.15
-0.2
-0.25
0
2
4
6
Engineering/Math/Physics 25: Computational Methods
19
8
10
12
Time, t
14
16
18
20
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
ODE Result  NONzero ICs
ODE Example - Lecture23

0.3

y  2 y  5 y  sin t
0.2
ODE Solution y(t)
0.1
0
-0.1
y0  0.19
-0.2

dy
 y0  0.73
dt t 0
-0.3
-0.4
0
2
4
6
8
10
12
14
16
18
20
t
Engineering/Math/Physics 25: Computational Methods
20
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
ODE Result  Both y & dy/dt
ODE Example - Lecture24
0.6
y(t)
dy/dt
0.4
ODE Solution y(t) & dy/dt

y  2 y  5 y  sin t
0.2
0
y0  0.19
-0.2
-0.4
-0.6
-0.8
0

dy
 y0  0.73
dt t 0
1
2
Engineering/Math/Physics 25: Computational Methods
21

3
t
4
5
6
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
3rd1st Reduction of Order (1)
d3y
d2y
dy
 3 2  7  19 y  73 ln t  5 OR
3
dt
dt
dt



y  3 y  7 y  19 y  73 ln t  5
y 7   4

dy
and IC' s :
 y 7   3
dt t 7

d2y
 y 7   2
2
dt t 7
Engineering/Math/Physics 25: Computational Methods
22
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
3rd1st Reduction of Order (2)
dy 
d 2 y 
x1  y x 2 
 y x3  2  y
dt
dt


dx1
x1  y 
 x2
dt


dx2
x2  y 
 x3
dt

 
dx3
x3  y 
dt
 


y  3 y  7 y  19 y  73 ln t  5 OR

x 3  3 x3  7 x2  19 x1  73 ln t  5
Engineering/Math/Physics 25: Computational Methods
23
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
3rd1st Reduction of Order (3)
 Thus the 3-Eqn, 1st Order, ODE System

dx1 dt  x1  x2

dx2 dt  x 2  x3

dx3 dt  x 3  73 ln t  5  3 x3  7 x2  19 x1
y 7   x1 7   4

y 7   x2 7   3

y 7   x3 7   2
Engineering/Math/Physics 25: Computational Methods
24
IC - 1
IC - 2
IC - 3
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
1
2
3
ODE: LittleOnes out of BigOne
(Reduction of Order)
V=
25
S=
C=
Engineering/Math/Physics 25: Computational Methods
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
ODE: LittleOnes out of BigOne
Engineering/Math/Physics 25: Computational Methods
26
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
ODE: LittleOnes out of BigOne
Engineering/Math/Physics 25: Computational Methods
27
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
ODE: LittleOnes out of BigOne



28



Engineering/Math/Physics 25: Computational Methods
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
One more: Anonymous  z(t)
2


d z
dz
2 dz
 4 1 z
 z  0 z 0  1
2
dt
dt
dt 
 3
z 0
 The Transformation
dz
dy1
y1  z y1  y2  z 
 y2 
dt
dt
2
d z dy2
dy2
2

 y 2  41  y1 y2  y1 
2
dt
dt
dt
y1 0  1 y2 0  3
Engineering/Math/Physics 25: Computational Methods
29
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Anonymous Example  z(t)
dz dt
d 2 z dt 2
>> zAnon = @(t,y) [y(2); 4*(1-y(1)^2)*y(2)-y(1)]
zAnon =
@(t,y)[y(2);4*(1-y(1)^2)*y(2)-y(1)]
>> [tz,yz] = ode45(zAnon, [0, 50], [1, -3]);
>> plot(tz,yz(:,1), 'LineWidth',2), xlabel('t'),
2.5
ylabel('z'), grid
2
1.5
1
z
0.5
0
-0.5
-1
-1.5
-2
-2.5
0
5
Engineering/Math/Physics 25: Computational Methods
30
10
15
20
25
t
30
35
40
45
50
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Anonymous NonLinear
2


dy
y



 ln 
 Consider
  t  0.9  
dt
20

10
0
y
% Bruce Mayer, PE * 29Apr14
% ENGR25 * Lec24 on MATLAB ODE
solvers
% Use Anonymous fcn to pass ode45
%
clear; clc, clf % clear out: memory,
workspace, plot
%
%
dydt = @(t,y) log((y/(t+0.9))^2)
% note that zAnon has a place-holder
for t
%
% Call ode45 into action using zAnon
[tz,yz] = ode45(dydt, [0, 50], 2.7);
axes; set(gca,'FontSize',12);
whitebg([0.8 1 1]); % Chg Plot
BackGround to Blue-Green
plot(tz,yz(:,1), 'LineWidth',3),
xlabel('t'), ylabel('y'), grid
y t  0   2.7
-10
-20
-30
-40
0
10
20
30
t
Engineering/Math/Physics 25: Computational Methods
31
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
40
50
Anonymous NonLinear
2


dy
y



 ln 
 SimuLink
  t  0.9  
dt


Engineering/Math/Physics 25: Computational Methods
32
y t  0   2.7
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Anonymous NonLinear
% Bruce Mayer, PE * 29Apr14
% ENGR25 * Lec24 on MATLAB ODE
solvers
% Use Anonymous fcn to pass ode45
% file = Anon_ODE_Example_1304.m
%
clear; clc, clf % clear out: memory,
workspace, plot
%
%
dydt = @(t,y) cos(t/(y+3))
% note that zAnon has a place-holder
for t
%
% Call ode45 into action using zAnon
[tz,yz] = ode45(dydt, [0, 100], 2.7);
axes; set(gca,'FontSize',12);
whitebg([0.8 1 1]); % Chg Plot
BackGround to Blue-Green
plot(tz,yz, 'LineWidth',3),
xlabel('t'), ylabel('y'), grid
Engineering/Math/Physics 25: Computational Methods
33
y t  0   2.7
30
25
20
y
 Consider
 t 
dy

 ln
dt
 y 3
15
10
5
0
0
20
40
60
t
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
80
100
Anonymous NonLinear
 SimuLink
 t 
dy

 cos
dt
 y 3
Engineering/Math/Physics 25: Computational Methods
34
y t  0   2.7
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Engineering/Math/Physics 25: Computational Methods
35
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Anonymous NonLinear
2


dy
y



 ln 
 Consider
  t  0.9  
dt
20

10
0
y
% Bruce Mayer, PE * 29Apr14
% ENGR25 * Lec24 on MATLAB ODE
solvers
% Use Anonymous fcn to pass ode45
%
clear; clc, clf % clear out: memory,
workspace, plot
%
%
dydt = @(t,y) log((y/(t+0.9))^2)
% note that zAnon has a place-holder
for t
%
% Call ode45 into action using zAnon
[tz,yz] = ode45(dydt, [0, 50], 2.7);
axes; set(gca,'FontSize',12);
whitebg([0.8 1 1]); % Chg Plot
BackGround to Blue-Green
plot(tz,yz(:,1), 'LineWidth',3),
xlabel('t'), ylabel('y'), grid
y t  0   2.7
-10
-20
-30
-40
0
10
20
30
t
Engineering/Math/Physics 25: Computational Methods
36
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
40
50
All Done for Today
Foucault
Pendulum
While our clocks are set by an average 24 hour day for the passage
of the Sun from noon to noon, the Earth rotates on its axis in 23
hours 56 minutes and 4.1 seconds with respect to the rest of the
universe. From our perspective here on Earth, it appears that the
entire universe circles us in this time. It is possible to do some
rather simple experiments that demonstrate that it is really the
rotation of the Earth that makes this daily motion occur.
In 1851 Leon Foucault (1819-1868) was made famous when he devised an experiment with a pendulum that demonstrated the
rotation of the Earth.. Inside the dome of the Pantheon of Paris he suspended an iron ball about 1 foot in diameter from a wire
more than 200 feet long. The ball could easily swing back and forth more than 12 feet. Just under it he built a circular ring on
which he placed a ridge of sand. A pin attached to the ball would scrape sand away each time the ball passed by. The ball was
drawn to the side and held in place by a cord until it was absolutely still. The cord was burned to start the pendulum swinging
in a perfect plane. Swing after swing the plane of the pendulum turned slowly because the floor of the Pantheon was moving
under the pendulum.
Engineering/Math/Physics 25: Computational Methods
37
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Engr/Math/Physics 25
Appendix
f x   2 x  7 x  9 x  6
3
2
Bruce Mayer, PE
Licensed Electrical & Mechanical Engineer
BMayer@ChabotCollege.edu
Engineering/Math/Physics 25: Computational Methods
38
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Demo – Problem 9.34
 Accelerating Pendulum
 For an Arbitrary LateralAcceleration Function, a(t),
the ANGULAR Position, θ, is
described by the (nastily)
NONlinear 2nd Order,
Homogeneous ODE

L   g sin   at  cos   0
at 

L
m
W = mg
• See next Slide for Eqn Derivation
 Solve for θ(t)
Engineering/Math/Physics 25: Computational Methods
39
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Prob 9.34: ΣF = Σma

at  cos 

at 

L
T

n
N-T CoORD Sys
ds  Ld
m

ds
d
ds  Ld 
L
 L
dt
dt

d 2 s 
d 2
 s  L 2  L
2
dt
dt
FT  W sin 

abase,T  at  cos 
 

  mg sin   m L   at  cos  
W sin 


aS ,T  L 
W = mg
Engineering/Math/Physics 25: Computational Methods
40
 Use Normal-Tangential
CoOrds; θ+ → CCW
 Use ΣFT = ΣmaT
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Prob 9.34: Simplify ODE
 Cancel m:
−𝑚𝑔 sin 𝜃 = 𝑚 𝐿𝜃 − 𝑎 𝑡 cos 𝜃
 Collect All θ terms on L.H.S.
𝐿𝜃 + 𝑔 sin 𝜃 − 𝑎 𝑡 cos 𝜃 = 0
at 
 Next make Two Little
Ones out of the Big One
• That is, convert the
ODE to State Variable Form

L
m
W = mg
Engineering/Math/Physics 25: Computational Methods
41
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Convert to State Variable Form
 Let: 𝑢1 = 𝜃 → 𝑑𝑢1 𝑑𝑡 = 𝑢2 = 𝜃
 Thus: 𝑑𝜃 𝑑𝑡 = 𝜃 or 𝑑𝑢2 𝑑𝑡 = 𝜃
 Then the 2nd derivative
𝑑2 𝜃
𝑎 𝑡 ∙ cos 𝑢 − 𝑔 ∙ sin 𝑢
𝑑𝑢2
=
=
2
𝑑𝑡
𝐿
𝑑𝑡
 Have Created Two 1st Order Eqns
Engineering/Math/Physics 25: Computational Methods
42
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
SimuLink Solution
 The ODE using y in place of θ
2
d y
L 2  g sin y  at  cos y  0
dt
 Isolate Highest d 2 y at  cos y  g sin y

2
Order Derivative dt
L
 Double Integrate to find y(t)
 at  cos y  g sin y 
y   
dt dt
L


Engineering/Math/Physics 25: Computational Methods
43
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
SimuLink Diagram
Engineering/Math/Physics 25: Computational Methods
44
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Prob 9.34 Results for Case-a
P8.30 - Accelerating Pendulum - Case (a)
0.5
0.49
theta (rads)
0.48
0.47
0.46
0.45
0.44
0
1
2
3
Engineering/Math/Physics 25: Computational Methods
45
4
5
6
t (sec)
7
8
9
10
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Prob 9.34
Results
for
Case-b
P8.30 - Accelerating Pendulum - Case (b)
4
3
theta (rads)
2
1
0
-1
-2
-3
0
1
2
3
4
5
6
7
8
9
10
t (sec)
Engineering/Math/Physics 25: Computational Methods
46
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Prob 9.34 Results for Case-c
P8.30 - Accelerating Pendulum - Case (c)
4
3
theta (rads)
2
1
0
-1
-2
-3
0
1
2
3
4
5
6
7
8
9
10
t (sec)
Engineering/Math/Physics 25: Computational Methods
47
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Engineering/Math/Physics 25: Computational Methods
48
Prob 9.34 Script File
% Bruce Mayer, PE * 05Nov11
% ENGR25 * problem 9.34
% file = Demo_Prob9_34.m
%
%This script file calls FUNCTION pendacc
%
clear
% clears memory
global m b;
% globalize accel calc constants
% Acceleration, a(t) = m*t + b
% ask user for max time; suggest starting at 25
tmax = input('tmax = ');
%
%set the case consts, and IC's y(0) & dy(0)/dt
%=> remove the leading "%" to toggle between cases
m = 0, b = 5, y0 = [0.5 0]; % case-a
%m = 0, b = 5, y0 = [3 0]; % case-b
%m = 0.5, b = 0, y0 = [3 0]; % case-c
% m = 0.4, b = -4, y0 = [1.7 2.3]; % case-d => EXTRA
%
%Call the ode45 routine with the above data inputs
[t,x]=ode45('pendacc', [0, tmax], y0);
%
%PLot theta(t)
subplot(1,1,1)
plot(t,x(:,1)), xlabel('t (sec)'), ylabel('theta (rads)'),...
title('P9.34 - Accelerating Pendulum'), grid;
disp('Plotting ONLY theta - Hit Any Key to continue')
pause
%Plot the FIRST column of the solution “matrix”
%giving x1 or y.
subplot(2,1,1)
plot(t,x(:,1)), xlabel('t (sec)'), ylabel('theta (rads)'),...
title('P9.34 - Accelerating Pendulum'), grid;
%Plot the SECOND column of the solution “matrix”
%giving x2 or dy/dt.
subplot(2,1,2)
plot(t,x(:,2)), xlabel('t (sec)'), ylabel('dtheta/dt (r/s)'), grid;
disp('Plotting Both theta and dtheta/dt; hit any key to continue')
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Prob 9.34 Function File
function dxdt = pendacc(t_val,z);
% Bruce Mayer, PE * 05Nov05
% ENGR25 * Prob 8-30
%
%This is the function that makes up the system
%of differential equations solved by ode45
%
% the Vector z contains yk & [dy/dt]k
%
%Globalize the Constants used to calc the Accel
global m b
% set the physical constants
L = 1;
% in m
g = 9.81;
% in m/sq-Sec
%
%DEBUG § => remove semicolons to reveal t_val & z
t_val; z;
%
% Calc the Cauchy (State) values
dxdt(1)= z(2); % at t=0, dxdt(1) = dy(0)/dt
dxdt(2)= ((m*t_val + b)*cos(z(1)) - g*sin(z(1)))/L;
% at t = 0, dxdt(2) =((m*t_val + b)*cos(y(0)) - g*sin(y(0)))/L;
%
% make the dxdt into a COLUMN vector
dxdt = [dxdt(1); dxdt(2)];
Engineering/Math/Physics 25: Computational Methods
49
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Θ with Torsional Damping
 The Angular Position, θ, of a linearly
accelerating pendulum with a Journal
Bearing mount that produces torsional
friction-damping can be described by this
second-order, non-linear Ordinary
Differential Equation (ODE) and Initial
Conditions (IC’s) for θ(t):
D
 0  2.8 rads
L = 1.6 meters
n = 0.40 meters/sec3
Engineering/Math/Physics 25: Computational Methods
50
 1.9 rads sec
m
t 0
D = 0.07 meters/sec
L

L  D  g sin   n  t  bcos  0
d

 0 
dt
at 
g = 9.8 meters/sec2
b = −3.0 meters/sec2
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
W = mg
Θ with Torsional Damping

E25_FE_Damped_Pendulum_1104.mdl
Engineering/Math/Physics 25: Computational Methods
51
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
plot(tout,Q, 'k', 'LineWidth', 2), grid,
xlabel('t (sec)'), ylabel('\theta (rads)'),
title('Accelerating Pendulum Angular
Position')
Θ with Torsional Damping
Accelerating Pendulum Angular Position
3
2
 (rads)
1
0
-1
-2
-3
0
10
20
30
40
Engineering/Math/Physics 25: Computational Methods
52
50
t (sec)
60
70
80
90
100
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
MATLAB ODE Format - 2
 In Vector Form (saves Writing time;
does NOT make Solution Easier)

y  f (t , y ),
 If we have written
ROW vectors for
the y & b
quantities can
Transpose to
Column-Vectors
Engineering/Math/Physics 25: Computational Methods
53
y (t0 )  b
y   y1 ; y2 ;; ym 
T
b  b1 ; b2 ;; bm 
T
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Solver Summary
Solver
ode45
Problem Type
Nonstiff
ode23
Nonstiff
ode113
Nonstiff
Low to high
For problems with stringent error tolerances
or for solving computationally intensive
problems.
ode15s
Stiff
Low to medium
If ode45 is slow because the problem is stiff.
ode23s
Stiff
Low
If using crude error tolerances to solve stiff
systems and the mass matrix is constant.
ode23t
Moderately Stiff
Low
For moderately stiff problems if you need a
solution without numerical damping.
Low
If using crude error tolerances to solve stiff
systems.
ode23tb Stiff
Order of Accuracy When to Use
Medium
Most of the time. This should be the first
solver you try.
Low
For problems with crude error tolerances or
for solving moderately stiff problems.
Engineering/Math/Physics 25: Computational Methods
54
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Lec-22_ODE_MATLAB.ppt
Download