54
[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer [email protected] Engr/Math/Physics 25 Chp9: ODE Solns By MATLAB

[email protected] ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

Embed Size (px)

Citation preview

Page 1: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt1

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Bruce Mayer, PELicensed Electrical & Mechanical Engineer

[email protected]

Engr/Math/Physics 25

Chp9: ODE Solns

By MATLAB

Page 2: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt2

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 3: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt3

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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.

Page 4: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt4

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Solver Summary Solver Problem Type Order of Accuracy When to Useode45 Nonstiff Medium Most of the time. This should be the first

solver you try.

ode23 Nonstiff Low For problems with crude error tolerances or for solving moderately stiff problems.

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.

ode23tb Stiff Low If using crude error tolerances to solve stiff systems.

Page 5: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt5

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

MATLAB ODE Format - 1

MATLAB ODE Solver Form for Multiple Dependent Variables (MultiVar Probs)

,,,,,,

,,,,,,

,,,,,,

021

2022122

1012111

mmmmm

m

m

btyyyytfdt

dy

btyyyytfdt

dy

btyyyytfdt

dy

m-Eqns (1st order ODEs) in m-Unknowns

Page 6: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt6

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

1st order ODE System Example

3342 wtzxywedt

dw t

88483737

56

xtywxtdt

dx

2243ln6611

3 7.1

yzt

xy

dt

dy

554

9ln

sincos 273

y

t

yetz

dt

dz tw

Page 7: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt7

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 8: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt8

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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.

Page 9: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt9

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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

Page 10: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt10

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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.

Page 11: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt11

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Example ode45 (1)

Solve 2nd ORDER ODE

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) formLET:

tyyy sin52

20

680

0

ydt

dy

y

t

yx

yx

2

1

With ICs

• i.e.,

Page 12: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt12

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Example ode45 (2)

With the Xform Subbing in the x1 & x2

221 xyyxyx

Find that

21

1 xydt

dy

dt

dxx

ReArranging the ODE to isolate Highest order term

212 25sin xxtx

yyty 25sin

Thus the 1st Order Eqn System in 2 Vars

212

2

21

1

25sin xxtdt

dxx

xdt

dxx

200

6800

2

1

xy

xy

Page 13: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt13

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Example ode45 (3)

Note that applying this Xform

And also dx2/dt

yxyx 21

Converted the SINGLE 2nd Order ODE to a LINEAR System of TWO 1st Order ODEs

Sub into ODE

2

2 xyydt

d

dt

dx

21 xy

dt

dy

dt

dx

tyyy sin52

txxdt

dxsin52 12

2

Page 14: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt14

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Example ode45 (4)

Now Isolate dx1/dt Next Isolate dx2/dt

21 xy

dt

dy

dt

dx

txxdt

dxsin52 12

2

21 xdt

dx 21

2 25sin xxtdt

dx

Recall the IC’s

2006800 21

xyxy

Page 15: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt15

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Example ode45 (5)

Thus the Transformation to State-Var Form of Two 1st Order ODEs

tyyy sin52

&

20

680

0

ydt

dy

y

t

2025sin

680

2212

121

txxxtdt

dx

txxdt

dx The final xForm

Page 16: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt16

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Example ode45 (6)

Compare xForm to Slide-4

2025sin

680

2212

121

txxxtdt

dx

txxdt

dx

,)(),,,(

,)(),,,(

2022122

1012111

btyyytfdt

dy

btyyytfdt

dy

Page 17: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt17

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Example ode45 (7)T

he Function F

ile =

xdot_lec24.mfunction 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 Vectorxd = [xd(1); xd(2)];%% DEBUG Sectiondisp('xd(1) = dy/dt ='), disp(xd(1))disp('xd(2) = d2y/dt2 = '), disp(xd(2))

Page 18: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt18

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

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 thistmax = 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')

Page 19: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt19

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

ODE Example Result (0 for ICs)

0 2 4 6 8 10 12 14 16 18 20

-0.25

-0.2

-0.15

-0.1

-0.05

0

0.05

0.1

0.15

0.2

0.25

Time, t

OD

E S

olu

tion

, y(t

)ODE Example - Lecture23

Page 20: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt20

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

ODE Result NONzero ICs

0 2 4 6 8 10 12 14 16 18 20-0.4

-0.3

-0.2

-0.1

0

0.1

0.2

0.3

t

OD

E S

olut

ion

y(t)

ODE Example - Lecture23

tyyy sin52

73.000

ydt

dy

t

19.00 y

Page 21: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt21

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

ODE Result Both y & dy/dt

0 1 2 3 4 5 6-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

t

OD

E S

olut

ion

y(t)

& d

y/dt

ODE Example - Lecture24

y(t)

dy/dt

tyyy sin52

73.000

ydt

dy

t

19.00 y

Page 22: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt22

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

3rd1st Reduction of Order (1)

5ln731973

OR 5ln7319732

2

3

3

tyyyy

tydt

dy

dt

yd

dt

yd

27

37

47

:sIC' and

7

2

27

ydt

yd

ydt

dyy

t

t

Page 23: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt23

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

3rd1st Reduction of Order (2)

ydt

ydxy

dt

dyxyx

2

2

321

dt

dxyx

xdt

dxyx

xdt

dxyx

33

32

2

21

1

5ln731973

OR 5ln731973

1233

txxxx

tyyyy

Page 24: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt24

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

3rd1st Reduction of Order (3)

Thus the 3-Eqn, 1st Order, ODE System

319735ln73

2

1

12333

322

211

xxxtxdtdx

xxdtdx

xxdtdx

3-IC277

2-IC377

1-IC477

3

2

1

xy

xy

xy

Page 25: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt25

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

ODE: LittleOnes out of BigOne(Reduction of Order)

V =

S =

C =

Page 26: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt26

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

ODE: LittleOnes out of BigOne

Page 27: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt27

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

ODE: LittleOnes out of BigOne

Page 28: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt28

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

ODE: LittleOnes out of BigOne

Page 29: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt29

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

One more: Anonymous z(t)

The Transformation

310014

0

22

2

zdt

dzzz

dt

dzz

dt

zd

dt

dyy

dt

dzzyyzy 1

2211

dt

dyyyyy

dt

dy

dt

zd 212

212

22

2

14

3010 21 yy

Page 30: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt30

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Anonymous Example z(t)>> 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'), ylabel('z'), grid

0 5 10 15 20 25 30 35 40 45 50-2.5

-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

2.5

t

z

dtdz 22 dtzd

Page 31: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt31

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Anonymous NonLinear

Consider 7.209.0

ln2

tyt

y

dt

dy

0 10 20 30 40 50-40

-30

-20

-10

0

10

20

t

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-Greenplot(tz,yz(:,1), 'LineWidth',3), xlabel('t'), ylabel('y'), grid

Page 32: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt32

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Anonymous NonLinear

SimuLink 7.209.0

ln2

tyt

y

dt

dy

B. Mayer * 29Apr14

dy/dt = ln([y/(t+0.9)]^2 * y(0) = 2.7

Clock1s

Integrator Scope0.9

Constant

Divide

ln

MathFunction

Addu2

MathFunction1

Page 33: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt33

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Anonymous NonLinear

Consider 7.203

ln

tyy

t

dt

dy

% 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-Greenplot(tz,yz, 'LineWidth',3), xlabel('t'), ylabel('y'), grid

0 20 40 60 80 1000

5

10

15

20

25

30

t

y

Page 34: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt34

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Anonymous NonLinear

SimuLink 7.203

cos

tyy

t

dt

dy

B. Mayer * 29Apr14

dy/dt = cos(t/(y+3) * y(0) = 2.7

Colors:R = 204G = 255B = 255

Clock

1s

Integrator Scope

Add

3

Constant

Divide

cos

TrigonometricFunction

Page 35: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt35

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

B. Mayer * 29Apr14

dy/dt = cos(t/(y+3) * y(0) = 2.7

Clock

1s

Integrator Scope

Add

3

Constant

Divide

cos

TrigonometricFunction

B. Mayer * 29Apr14

dy/dt = ln([y/(t+0.9)]^2 * y(0) = 2.7

Clock1s

Integrator Scope0.9

Constant

Divide

ln

MathFunction

Addu2

MathFunction1

Page 36: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt36

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Anonymous NonLinear

Consider 7.209.0

ln2

tyt

y

dt

dy

0 10 20 30 40 50-40

-30

-20

-10

0

10

20

t

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-Greenplot(tz,yz(:,1), 'LineWidth',3), xlabel('t'), ylabel('y'), grid

Page 37: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt37

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

All Done for Today

FoucaultPendulum

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.

Page 38: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt38

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Bruce Mayer, PELicensed Electrical & Mechanical Engineer

[email protected]

Engr/Math/Physics 25

Appendix 6972 23 xxxxf

Page 39: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt39

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Demo – Problem 9.34

Accelerating Pendulum For an Arbitrary Lateral-Acceleration Function, a(t), the ANGULAR Position, θ, is described by the (nastily) NONlinear 2nd Order, Homogeneous ODE

0cossin

tagL• See next Slide for Eqn Derivation

Solve for θ(t)

qL

m W = mg

ta

Page 40: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt40

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Prob 9.34: ΣF = Σma

N-T CoORD Sys

n

T

Ldt

dLs

dt

sd

Ldt

dL

dt

dsLdds

2

2

2

2

Use Normal-Tangential CoOrds; θ+ → CCW

cos

sin

,, taaLa

WF

TbaseTS

T

cossin taLmmg

L

mW = mg

ta

L

mW = mg

ta

sinW

costa

Ldds

Use ΣFT = ΣmaT

Page 41: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt41

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Prob 9.34: Simplify ODE

Cancel m:

Collect All θ terms on L.H.S.

Next make Two Little Ones out of the Big One• That is, convert the

ODE to State Variable FormL

mW = mg

ta

L

mW = mg

ta

Page 42: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt42

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Convert to State Variable Form

Let: Thus:

Then the 2nd derivative

Have Created Two 1st Order Eqns

Page 43: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt43

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

SimuLink Solution

The ODE using y in place of θ

Isolate Highest Order Derivative

Double Integrate to find y(t)

0cossin2

2

ytaygdt

ydL

L

ygyta

dt

yd sincos2

2

dtdt

L

ygytay

sincos

Page 44: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt44

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

SimuLink Diagram

Page 45: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt45

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Prob 9.34 Results for Case-a

0 1 2 3 4 5 6 7 8 9 100.44

0.45

0.46

0.47

0.48

0.49

0.5

t (sec)

the

ta (

rad

s)

P8.30 - Accelerating Pendulum - Case (a)

Page 46: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt46

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Prob 9.34 Results for Case-b

0 1 2 3 4 5 6 7 8 9 10-3

-2

-1

0

1

2

3

4P8.30 - Accelerating Pendulum - Case (b)

t (sec)

the

ta (

rad

s)

Page 47: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt47

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Prob 9.34 Results for Case-c

0 1 2 3 4 5 6 7 8 9 10-3

-2

-1

0

1

2

3

4

t (sec)

the

ta (

rad

s)

P8.30 - Accelerating Pendulum - Case (c)

Page 48: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt48

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Pro

b 9.34 S

cript F

ile

% Bruce Mayer, PE * 05Nov11% ENGR25 * problem 9.34% file = Demo_Prob9_34.m% %This script file calls FUNCTION pendacc%clear % clears memoryglobal m b; % globalize accel calc constants% Acceleration, a(t) = m*t + b% ask user for max time; suggest starting at 25tmax = input('tmax = '); %%set the case consts, and IC's y(0) & dy(0)/dt%=> remove the leading "%" to toggle between casesm = 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')

Page 49: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt49

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Prob 9.34 Function Filefunction 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 Accelglobal m b% set the physical constantsL = 1; % in mg = 9.81; % in m/sq-Sec%%DEBUG § => remove semicolons to reveal t_val & zt_val; z;%% Calc the Cauchy (State) valuesdxdt(1)= z(2); % at t=0, dxdt(1) = dy(0)/dtdxdt(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 vectordxdt = [dxdt(1); dxdt(2)];

Page 50: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt50

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Θ 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):

qL

m W = mg

taD

0cossin btngDL

rads 8.20 secrads 9.100

tdt

d

L = 1.6 meters D = 0.07 meters/sec g = 9.8 meters/sec2

n = 0.40 meters/sec3 b = −3.0 meters/sec2

Page 51: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt51

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Θ with Torsional Damping

E25_FE_Damped_Pendulum_1104.mdl

Page 52: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt52

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Θ with Torsional Damping

0 10 20 30 40 50 60 70 80 90 100-3

-2

-1

0

1

2

3

t (sec)

(r

ads)

Accelerating Pendulum Angular Position

plot(tout,Q, 'k', 'LineWidth', 2), grid,

xlabel('t (sec)'), ylabel('\theta (rads)'),

title('Accelerating Pendulum Angular

Position')

Page 53: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt53

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

MATLAB ODE Format - 2

In Vector Form (saves Writing time; does NOT make Solution Easier)

byyfy

)(),,( 0tt

If we have written ROW vectors for the y & b quantities can Transpose to Column-Vectors

myyy ;;; 21 Ty

mbbb ;;;b 21 T

Page 54: BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Lec-22_ODE_MATLAB.ppt54

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Solver Summary Solver Problem Type Order of Accuracy When to Useode45 Nonstiff Medium Most of the time. This should be the first

solver you try.

ode23 Nonstiff Low For problems with crude error tolerances or for solving moderately stiff problems.

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.

ode23tb Stiff Low If using crude error tolerances to solve stiff systems.