Engr/Math/Physics 25 Chp9: ODE’s Numerical Solns 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-21_Integ_Diff.ppt Learning Goals List Characteristics of Linear, MultiOrder, NonHomgeneous Ordinary Differential Equations (ODEs) Solve ANALYTICALLLY, Linear, 2nd Order, NonHomogeneous, Constant Coefficient ODEs Use MATLAB to determine Numerical Solutions to Ordinary Differential Equations (ODEs) Engineering/Math/Physics 25: Computational Methods 2 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Differential Equations Ordinary Diff Eqn Partial Diff Eqn 2 y y y d2y dy x, t x , t b x , t b y x , t t a1 t a2 yt f (t ) 2 1 2 2 dt dt t t x PDE’s Not Covered in ENGR25 • Discussed in More Detail in ENGR45 Examining the ODE, Note that it: • is LINEAR → y, dy/dt, d2y/dt2 all raised to Power of 1 • 2nd ORDER → Highest Derivative is 2 • is NONhomogenous → RHS 0; – i.e., y(t) has a FORCING Fcn f(t) • has CONSTANT Coefficients Engineering/Math/Physics 25: Computational Methods 3 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Numerical ODE Solution Today we’ll do some MTH25 We’ll “look under the hood” of NUMERICAL Solutions to ODE’s The BASIC GamePlan for even Sophisticated Solvers: • Given a STARTING POINT, y(0) • Use ODE to find dy/dt at t=0 • ESTIMATE y1 as dy y1 y0 t dt t 0 Engineering/Math/Physics 25: Computational Methods 4 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Numerical Solution - 1 Notation n Step Number t Time Step Length tn n t Exact Numerical Method (impossible to achieve) by Forward Steps yn+1 y n y (t n ) f n f (t n , y n ) yn Now Consider dy f (t , y ) dt Engineering/Math/Physics 25: Computational Methods 5 tn t tn+1 t Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Numerical Solution - 2 yn+1 The diagram at Left shows that the relationship between yn, yn+1 and the CHORD slope Tangent Slope yn Chord Slope tn t The Analyst Chooses Δt tn+1 y n1 y n chord slope t The problem with this formula is we canNOT calculate the t chord slope exactly • We Know Only Δt & yn, but NOT the NEXT Step yn+1 Engineering/Math/Physics 25: Computational Methods 6 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Numerical Solution -3 However, we can calculate the TANGENT slope at any point FROM the differential equation itself dy mn f (t n , yn ) dt t t n Recognize dy/dt as the Tangent Slope tangent slope f (t , y ) Engineering/Math/Physics 25: Computational Methods 7 The Basic Concept for all numerical methods for solving ODE’s is to use the TANGENT slope, available from the R.H.S. of the ODE, to approximate the chord slope yn 1 yn dy f t n , yn t dt tn Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Euler Method – 1st Order Solve 1st Order ODE with I.C. dy f (t , y ) dt y ( 0) b Use: [Chord Slope] [Tangent Slope at start of time step] yn 1 yn dy f t n , yn t dt tn Engineering/Math/Physics 25: Computational Methods 8 ReArranging dy yn 1 t yn dt t n or yn1 yn t f n Then Start the “Forward March” with Initial Conditions t0 0 y0 b Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Euler Example Consider 1st Order ODE with I.C. dy y 1 dt y (0) 0 Use The Euler Forward-Step Reln yn 1 yn t f n dy yn t dt tn Engineering/Math/Physics 25: Computational Methods 9 But from ODE dy yn 1 dt tn So In This Example: yn1 yn t ( yn 1) See Next Slide for the 1st Nine Steps For Δt = 0.1 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Euler Exmpl Calc dy y 1 t 0.1 dt tn yn fn= – yn+1 yn+1= yn+t fn 0 0 0.000 1.000 0.100 1 0.1 0.100 0.900 0.190 2 0.2 0.190 0.810 0.271 3 0.3 0.271 0.729 0.344 4 0.4 0.344 0.656 0.410 5 0.5 0.410 0.590 0.469 6 0.6 0.469 0.531 0.522 7 0.7 0.522 0.478 0.570 8 0.8 0.570 0.430 0.613 9 0.9 0.613 0.387 0.651 n Slope Plot Engineering/Math/Physics 25: Computational Methods 10 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Euler vs Analytical 0.8 The Analytical Solution 0.6 y 1 e y 0.4 Numerical 0.2 Exact 1.25 1 0.75 0.5 0.25 0 0 t Engineering/Math/Physics 25: Computational Methods 11 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt t Analytical Soln dy y 1 y t 0 0 dt Let u = −y+1 Integrate Both Sides Then u 1 y du dy 0 1 dy du Sub for y & dy in ODE du dt u du Separate dt Variables u Engineering/Math/Physics 25: Computational Methods 12 du u 1dt Recognize LHS as Natural Log ln u t C Raise “e” to the power of both sides e ln u e t C Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Analytical Soln dy y 1 y t 0 0 dt And Now use IC e e ln u 1 0 Ke u t C C t e e Ke Thus Soln u(t) u Ke t t 0 K 1 The Analytical Soln 1 y 1 e t Sub u = 1−y 1 y Ke t Engineering/Math/Physics 25: Computational Methods 13 y 1 e t Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Predictor-Corrector - 1 Again Solve 1st Mathematically Order ODE with I.C. y y n 1 n 0.5 f (tn , yn ) f (tn 1 , yn 1 ) dy dt f (t , y ) y ( 0) b This Time Let: Chord slope average of tangent slopes at start and END of time step Engineering/Math/Physics 25: Computational Methods 14 t Avg of the Tangent Slopes at (tn,yn) & (tn+1,yn+1) BUT, we do NOT know yn+1 and it appears on the RHS... Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Predictor-Corrector - 2 Use Two Steps to estimate yn+1 First → PREDICT* y n 1 yn y yn1 yn t dy dt n y n 1 yn t f t n , yn yn1 yn t f n Use y* in the Avg Calc 0.5t f y n1 yn * f n n 1 Then Start the “Forward March” with the Initial Conditions Then Correct Engineering/Math/Physics 25: Computational Methods 15 y n1 yn 0.5t f tn , yn f tn1 , yn1 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Predictor-Corrector Example dy y 1 dt Solve ODE with IC y (0) 0 yn 1 yn 0.5t f n f The Corrector step The next Step Eqn for dy/dt = f(t,y)= –y+1 yn 1 yn 0.5t yn 1 y * n 1 1 Numerical Results on Next Slide Engineering/Math/Physics 25: Computational Methods 16 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt n 1 Predictor-Corrector Example f dy dt y 1 n tn yn yn 1 yn 0.5t f n f n1 fn Slope f n*1 yn1 Slope 0 0 0.000 1.000 0.100 0.900 0.095 1 0.1 0.095 0.905 0.186 0.815 0.181 2 0.2 0.181 0.819 0.263 0.737 0.259 3 0.3 0.259 0.741 0.333 0.667 0.329 4 0.4 0.329 0.671 0.396 0.604 0.393 Engineering/Math/Physics 25: Computational Methods 17 y n*1 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Predictor-Corrector 0.8 Greatly Improved Accuracy 0.6 y 0.4 Exact 0.2 Mod. Euler t Engineering/Math/Physics 25: Computational Methods 18 1.25 1 0.75 0.5 0.25 0 0 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt dy Example: dt 3.9 cos4.2 y 3.9 ln 5.1t 6 ODE Euler Solution with ∆t = 0.25 The Solution Table Euler Solution to dy/dt = 3.9cos(4.2y)-ln(5.1t+6) 38 36 34 y(t) by Euler 32 30 28 26 24 22 0 1 2 3 4 5 t 6 7 8 9 Engineering/Math/Physics 25: Computational Methods 19 10 n t y dy/dt dely yn+1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 0 0.25 0.5 0.75 1 1.25 1.5 1.75 2 2.25 2.5 2.75 3 3.25 3.5 3.75 4 4.25 4.5 4.75 5 5.25 5.5 5.75 6 6.25 6.5 6.75 7 7.25 7.5 7.75 8 8.25 8.5 8.75 9 9.25 9.5 9.75 10 37.0000 36.5636 36.9143 36.5769 36.8872 36.5806 36.8418 36.6641 36.9608 36.3357 35.6768 35.2701 35.2882 35.2273 35.3380 35.0526 35.0491 35.0223 34.8909 34.2399 33.9524 33.1997 32.4496 31.6958 30.9492 30.1897 29.4564 28.6710 27.9981 27.1110 26.6745 25.9565 25.3424 25.2245 24.6604 24.6512 24.6268 24.5593 24.2973 23.3007 23.0678 -1.7457 1.4027 -1.3492 1.2410 -1.2264 1.0448 -0.7108 1.1868 -2.5004 -2.6357 -1.6265 0.0722 -0.2436 0.4430 -1.1420 -0.0139 -0.1072 -0.5255 -2.6041 -1.1497 -3.0108 -3.0006 -3.0151 -2.9862 -3.0384 -2.9328 -3.1419 -2.6916 -3.5484 -1.7458 -2.8722 -2.4562 -0.4717 -2.2562 -0.0369 -0.0977 -0.2699 -1.0481 -3.9863 -0.9318 -1.0551 -0.4364 0.3507 -0.3373 0.3103 -0.3066 0.2612 -0.1777 0.2967 -0.6251 -0.6589 -0.4066 0.0181 -0.0609 0.1107 -0.2855 -0.0035 -0.0268 -0.1314 -0.6510 -0.2874 -0.7527 -0.7502 -0.7538 -0.7466 -0.7596 -0.7332 -0.7855 -0.6729 -0.8871 -0.4365 -0.7180 -0.6141 -0.1179 -0.5641 -0.0092 -0.0244 -0.0675 -0.2620 -0.9966 -0.2329 -0.2638 36.5636 36.9143 36.5769 36.8872 36.5806 36.8418 36.6641 36.9608 36.3357 35.6768 35.2701 35.2882 35.2273 35.3380 35.0526 35.0491 35.0223 34.8909 34.2399 33.9524 33.1997 32.4496 31.6958 30.9492 30.1897 29.4564 28.6710 27.9981 27.1110 26.6745 25.9565 25.3424 25.2245 24.6604 24.6512 24.6268 24.5593 24.2973 23.3007 23.0678 22.8040 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Compare Euler vs. ODE45 Euler Solution ODE45 Solution Euler Solution to dy/dt = 3.9cos(4.2y)-ln(5.1t+6) 37.5 38 36 37 34 36.5 Y by ODE45 y(t) by Euler 32 30 36 28 35.5 26 35 24 22 0 1 2 3 4 5 t 6 7 8 9 10 34.5 0 1 2 3 4 6 5 T by ODE45 Euler is Much LESS accurate Engineering/Math/Physics 25: Computational Methods 20 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt 7 8 9 10 Compare Again with ∆t = 0.025 Euler Solution ODE45 Solution Euler Solution to dy/dt = 3.9cos(4.2y)-ln(5.1t+6) 37.5 37.2 37 37 36.8 Y by ODE45 y(t) by Euler 36.5 36.6 36.4 36 35.5 36.2 35 36 35.8 34.5 0 1 2 3 4 5 t 6 7 8 9 10 0 1 2 3 4 6 5 T by ODE45 Smaller ∆T greatly improves Result Engineering/Math/Physics 25: Computational Methods 21 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt 7 8 9 10 MatLAB Code for Euler % Bruce Mayer, PE % ENGR25 * 04Jan11 % file = Euler_ODE_Numerical_Example_1201.m % y0= 37; delt = 0.25; t= [0:delt:10]; n = length(t); yp(1) = y0; % vector/array indices MUST start at 1 tp(1) = 0; for k = 1:(n-1) % fence-post adjustment to start at 0 dydt = 3.9*cos(4.2*yp(k))^2-log(5.1*tp(k)+6); dydtp(k) = dydt % keep track of tangent slope tp(k+1) = tp(k) + delt; dely = delt*dydt delyp(k) = dely yp(k+1) = yp(k) + dely; end plot(tp,yp, 'LineWidth', 3), grid, xlabel('t'),ylabel('y(t) by Euler'),... title('Euler Solution to dy/dt = 3.9cos(4.2y)-ln(5.1t+6)') Engineering/Math/Physics 25: Computational Methods 22 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt MatLAB Command Window for ODE45 >> >> >> by dydtfcn = @(tf,yf) 3.9*(cos(4.2*yf))^2-log(5.1*tf+6); [T,Y] = ode45(dydtfcn,[0 10],[37]); plot(T,Y, 'LineWidth', 3), grid, xlabel('T by ODE45'), ylabel('Y ODE45') Engineering/Math/Physics 25: Computational Methods 23 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt All Done for Today Carl Runge Carl David Tolmé Runge Born: 1856 in Bremen, Germany Engineering/Math/Physics 25: Computational Methods 24 Died: 1927 in Göttingen, Germany Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.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 25 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt 2nd Order ODE SUMMARY-1 If NonHomogeneous Then find ANY Particular Solution The Soln to the Homog. Eqn Produces the Complementary d2y dy 5 2 (t ) 7 (t ) 3 y (t ) 18 Solution, yc dt dt y p 18 / 3 6 (a CONST) Assume yc take this Next HOMOGENIZE the ODE 2 d y dy 5 2 (t ) 7 (t ) 3 y (t ) 0 dt dt Engineering/Math/Physics 25: Computational Methods 26 form yc t Ae st y c t sAe st y c t s 2 Ae st Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt 2nd Order ODE SUMMARY-2 Subbing yc = Aest into the Homog. Eqn yields the Characteristic Eqn 5s 7 s 3 0 2 Find the TWO roots that satisfy the Char Eqn by Quadratic Formula s1, 2 If s1 & s2 → REAL & UNequal yc t G1e G2 e s1t • Decaying Contant(s) 7 72 4 5 3 25 Engineering/Math/Physics 25: Computational Methods 27 Check FORM of Roots Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt s 2t 2nd Order ODE SUMMARY-3 If s1 & s2 → REAL & Equal, then s1 = s2 =s st yc t e mt b m, b are constants • Decaying Line If s1 & s2 → Complex Conjugates then yc t e at Add Particlular & Complementary Solutions to yield the Complete Solution y t yc y p B1 cos t B2 sin t Engineering/Math/Physics 25: Computational Methods 28 • Decaying Sinusoid Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt 2nd Order ODE SUMMARY-4 To Find Constant Find Number-Values Sets: (G1, G2), (m, for the constants to b), (B1, B2) Take for complete the COMPLETE solution solution process y t 0 IC 0 y0 dy IC1 y 0 dt t 0 • Yields 2 eqns in 2 for the 2 Unknown Constants Engineering/Math/Physics 25: Computational Methods 29 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Finite Difference Methods - 1 Another way of thinking about numerical methods is in terms of finite differences. Use the Approximation y n 1 yn dy t dt n Engineering/Math/Physics 25: Computational Methods 30 And From the Differential Eqn dy dt f (tn , y n ) n From these two equations obtain: y n 1 y n f (t n , y n ) t Recognize as the Euler Method Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt Finite Difference Methods - 2 Could make More Accurate by Approximating dy/dt at the Half-Step as the average of the end pts y n 1 y n dy 1 dy dy t dt n 1 2 dt n dt n 1 2 Then Again Use the ODE to Obtain y n 1 y n 1 f n f n 1 t 2 Engineering/Math/Physics 25: Computational Methods 31 Recognize as the Predictor-Corrector Method Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Lec-21_Integ_Diff.ppt