Download presentation
Presentation is loading. Please wait.
Published byLynn McBride Modified over 9 years ago
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 & Mechanical Engineer BMayer@ChabotCollege.edu Engr/Math/Physics 25 Chp9: ODE Solns By MATLAB
2
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 2 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
3
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 3 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods MATLAB ODE Solvers MATLAB has several 1 st 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.
4
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 4 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Solver Summary SolverProblem TypeOrder of AccuracyWhen to Use ode45NonstiffMediumMost of the time. This should be the first solver you try. ode23NonstiffLowFor problems with crude error tolerances or for solving moderately stiff problems. ode113NonstiffLow to highFor problems with stringent error tolerances or for solving computationally intensive problems. ode15sStiffLow to mediumIf ode45 is slow because the problem is stiff. ode23sStiffLowIf using crude error tolerances to solve stiff systems and the mass matrix is constant. ode23tModerately StiffLowFor moderately stiff problems if you need a solution without numerical damping. ode23tbStiffLowIf using crude error tolerances to solve stiff systems.
5
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 5 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods MATLAB ODE Format - 1 MATLAB ODE Solver Form for Multiple Dependent Variables (MultiVar Probs) m-Eqns (1 st order ODEs) in m-Unknowns
6
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 6 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 st order ODE System Example
7
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 7 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods MATLAB Solution To solve this MultiODE system using MATLAB the functions f 1, f 2, …, f m must be provided to the computer, along with the initial values of the variables; i.e., t 0 and b 1,b 2, …, b m The functions f 1, f 2, …, f m 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
8
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 8 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 t 0 and Tf is set to be the final time at the end of the interval of interest.
9
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 9 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 y 1, y 2, …, y m, as a function of t Alternatively plot(t,y(:,1)) gives a plot of y 1 as a function of t
10
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 10 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 t 1, t 2, t 3, … given by t k+1 = t k +Δt k, with Δt k selected by ode45. Thus EACH component of the solution y 1, y 2, …, y n is ITSELF a vector containing values such as y 1 (t 1 ), y 1 (t 2 ), y 1 (t 3 ), …, then y 2 (t 1 ), y 2 (t 2 ), y 2 (t 3 ), …, then y 3 (t 1 ), y 3 (t 2 ), y 3 (t 3 ), etc.
11
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 11 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example ode45 (1) Solve 2 nd ORDER ODE Note that to solve a 2 nd order eqn we need to know the SLOPE (dy/dt) at t = 0 To use the 1 st Order Solver, cast this 2 nd Order eqn into 1 st order (state Var) form LET: With ICs
12
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 12 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example ode45 (2) With the Xform Subbing in the x 1 & x 2 Find that ReArranging the ODE to isolate Highest order term Thus the 1 st Order Eqn System in 2 Vars
13
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 13 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example ode45 (3) Note that applying this Xform And also dx 2 /dt Converted the SINGLE 2 nd Order ODE to a LINEAR System of TWO 1 st Order ODEs Sub into ODE
14
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 14 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example ode45 (4) Now Isolate dx 1 /dt Next Isolate dx 2 /dt Recall the IC’s
15
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 15 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example ode45 (5) Thus the Transformation to State-Var Form of Two 1 st Order ODEs The final xForm
16
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 16 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example ode45 (6) Compare xForm to Slide-4
17
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 17 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example ode45 (7) The Function File = xdot_lec24.m 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))
18
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 18 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 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')
19
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 19 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods ODE Example Result (0 for ICs)
20
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 20 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods ODE Result NONzero ICs
21
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 21 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods ODE Result Both y & dy/dt
22
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 22 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 3 rd 1 st Reduction of Order (1)
23
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 23 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 3 rd 1 st Reduction of Order (2)
24
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 24 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 3 rd 1 st Reduction of Order (3) Thus the 3-Eqn, 1 st Order, ODE System
25
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 25 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods ODE: LittleOnes out of BigOne (Reduction of Order) V =S =C =
26
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 26 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods ODE: LittleOnes out of BigOne
27
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 27 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods ODE: LittleOnes out of BigOne
28
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 28 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods ODE: LittleOnes out of BigOne
29
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 29 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods One more: Anonymous z(t) The Transformation
30
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 30 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
31
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 31 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Anonymous NonLinear Consider % 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
32
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 32 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Anonymous NonLinear SimuLink
33
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 33 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Anonymous NonLinear Consider % 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
34
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 34 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Anonymous NonLinear SimuLink
35
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 35 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
36
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 36 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Anonymous NonLinear Consider % 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
37
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 37 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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.
38
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 38 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engr/Math/Physics 25 Appendix
39
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 39 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 2 nd Order, Homogeneous ODE See next Slide for Eqn Derivation Solve for θ(t) m W = mg
40
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 40 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob 9.34: ΣF = Σma N-T CoORD Sys Use Normal-Tangential CoOrds; θ+ → CCW Use ΣF T = Σma T
41
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 41 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob 9.34: Simplify ODE
42
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 42 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Convert to State Variable Form
43
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 43 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)
44
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 44 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods SimuLink Diagram
45
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 45 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob 9.34 Results for Case-a
46
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 46 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob 9.34 Results for Case-b
47
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 47 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob 9.34 Results for Case-c
48
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 48 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob 9.34 Script File
49
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 49 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob 9.34 Function File
50
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 50 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): m W = mg L = 1.6 metersD = 0.07 meters/secg = 9.8 meters/sec 2 n = 0.40 meters/sec 3 b = −3.0 meters/sec 2
51
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 51 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Θ with Torsional Damping E25_FE_Damped_Pendulum_1104.mdl
52
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 52 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Θ with Torsional Damping plot(tout,Q, 'k', 'LineWidth', 2), grid, xlabel('t (sec)'), ylabel('\theta (rads)'), title('Accelerating Pendulum Angular Position')
53
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 53 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods MATLAB ODE Format - 2 In Vector Form (saves Writing time; does NOT make Solution Easier) If we have written ROW vectors for the y & b quantities can Transpose to Column-Vectors
54
BMayer@ChabotCollege.edu ENGR-25_Lec-22_ODE_MATLAB.ppt 54 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Solver Summary SolverProblem TypeOrder of AccuracyWhen to Use ode45NonstiffMediumMost of the time. This should be the first solver you try. ode23NonstiffLowFor problems with crude error tolerances or for solving moderately stiff problems. ode113NonstiffLow to highFor problems with stringent error tolerances or for solving computationally intensive problems. ode15sStiffLow to mediumIf ode45 is slow because the problem is stiff. ode23sStiffLowIf using crude error tolerances to solve stiff systems and the mass matrix is constant. ode23tModerately StiffLowFor moderately stiff problems if you need a solution without numerical damping. ode23tbStiffLowIf using crude error tolerances to solve stiff systems.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.