Download presentation
Presentation is loading. Please wait.
Published byIsabella Goodwin Modified over 9 years ago
1
Chapter 10 ordinary differential equations (ODEs) Chapter 11 systems of ODEs (6 th edition)
2
System of ODEs
3
Initial value problems Regardless of problem statement, identify f(x,t), a, b, and x(a)
4
Initial-value problem: x’ = f(x,t); x(t 0 ) = x 0 Solved by “separation of variables”
5
Usually a “boot strap” method is required
6
Taylor-series method Given
7
Higher-order Taylor series methods x’(t) has both explicit and implicit time dependence given Given the value of x at t = t 0 evaluate x’(t 0 ), x’’(t 0 ), x’’’(t 0 ), etc. → x(t 0 +h) = x(t 0 )+hx’(t 0 )+h 2 x’’(t 0 )/2… then evaluate x’(t 0 +h), x’’(t 0 +h), x’’’(t 0 +h), etc. → x(t 0 +2h) = x(t 0 +h)+…
8
Solver: advances table X(t) → X(t+h), estimates local truncation error, controls propagation of error, etc. Is independent of a particular ODE Encoder: Tells solver how to calculate X’(t) for a particular ODE To solve a single ODE, encoder passed to solver as function handle For systems of ODEs, encoder passed to solver as an m-file Main: Defines initial conditions and domain of solution Sets solver parameters (number of points, error tolerance, etc. Calls solver for the particular ODE, analyze the results Exception: In higher-order Taylor method, encoder is part of solver Components of MatLab code to solve initial value problems main solver encoder
9
Solver: can be developed by criterion of table accuracy only, no concern for particular ODE or user objectives Encoder: only function is to tells the solver how to calculate X’(t) for all of the unknown functions in the problem. Encoder can be as simple as an inline function definition (one unknown) or as complex as an m-file describing a system being modeled by thousands of coupled ODEs Main: function restricted to problem setup (introduce encoder to solver) and downstream processing. Small changes for different problems Advantages of MatLab’s decomposition of problem: functions are specialized main solver encoder
10
Develop approach for Euler method x(t+h) = x(t) + hx’(t, x(t)) main solver encoder Start by developing the solver For singe ODE, encoder = inline function Write main to get results you need
11
MatLab code 4 th order Taylor-series method Command window function definition specific to problem derivatives calculated in order nested form saves operations No reason why driver and function should be separate See pseudo-code text p434 main solver encoder
12
Problems 10.1-10 and 10.1-11 p437 x’ = t 2 + x 3 5 equations needed for 4 th order Taylor x’ = x + e x 5 equations needed for 4 th order Taylor x’ = x 2 – cos(x) 6 equations needed for 5 th order
13
Runge-Kutta method: x’(t) = f(t, x(t))x(t 0 ) = x 0 Find method as accurate as Taylor series that does not involve higher order derivatives. 2nd order Runge-Kutta: Look for solution of the form x(t + h) = x(t) + w 1 F 1 + w 2 F 2 where F 1 = h f(t, x(t))F 2 = h f(t + h, x + F 1 ). Note: h in definition of F 1 and F 2 so they have the same units as x. All 4 parameters, w 1, w 2, , and are dimensionless
14
Look for solution of the form x(t + h) = x(t) + w 1 F 1 + w 2 F 2 where F 1 = h f(t, x(t))F 2 = h f(t + h, x + F 1 ) Expanding f(t + h, x + F 1 ) to 1 st order in h give x(t+h) to 2 nd order in h
15
Note: F 1 alone is Euler’s method Pseudo-code text p443 This result also called “ extended Euler” method. In the 3 rd term on the RHS, Euler’s approximation to x(t+h) is used in calculating an estimate of the slope of x(t) between t and t+h. The 2 estimates of the slope between t and t+h are averaged with equal weight. RK2 derivation continued
16
4 th order Runge-Kutta The same method used for RK2 yields a result that is as accurate as Taylor 4 th order. See text p443 for pseudo code.
18
Runge-Kutta-Fehlberg Method Evaluate both 4 th and 5 th order Runge Kutta Use difference as estimate of error Adjust h to keep error in bounds
19
Estimate of the error in x(t)
20
Inside a loop to generate a table of values of X(t) Basic idea behind MatLabs ODE45
21
MatLab’s ODE45 solver Encoder is inline function. Specify domain of solution. ODE45 chooses points Plot results Display number of points Compare exact to last value in table to exact
22
ODE45 337 points vs exact X(10) correct to 4 significant figures t x(t)
23
main solver encoder Use the design above to implement Euler, RK2, RK4, and ODE45 methods to solve x’ = 1 + t 3 +x 2 for x(2) given x(1) = -4. Use the same number of points as ODE45 for Euler, RK2 and RK4. Like ODE45, design Euler, RK2 and RK4 so that {t(1),x(1)} are the initial conditions. Compare, by percent difference, your values with the accurate value on p434 of text. Hand in your solvers and copy of command window where solver is called. Assignment 6 due 3/3/15
24
Predictor-Corrector Methods: At every value of t, predict x p (t+h) Use x p (t+h) to get more accurate x(t+h) Example: Improved Euler (p10.1-15 p437) x p (t+h) = x(t) + hx’(t,x(t)) (normal Euler) x(t+h) = x(t) + h[x’(t,x(t))+x’(t+h, x p (t+h))]/2 Is this the same as RK2?
25
Higher-order predictor - corrector
26
Use same approach to derive a corrector
27
Systems of ODEs Solved by Taylor, Euler, RK and ODE45
28
Use the Taylor series method to solve the the system x’ = -3yy’ = x/3
29
Taylor series method applied to system x’ = -3yy’ = x/3
30
Compare Taylor solution to exact result
32
Euler, RK and ODE45 applied to systems of ODEs
33
Pseudo-code for Euler method applied to 1 ODE [t, x]=Euler(xp, t 0, x 0, h, npts) t(1)=t 0 ; x(1)=x 0 ; For k=2,npts x(k)=x(k-1)+h*xp(t(k-1),x(k-1)); t(k)=t(k-1)+h; end
34
Pseudo-code for vectorised Eulersys [t,XM]=Eulersys(xpsys, t0,x0,h,npts) t(1)=t0; XM(1,:)=x0; For k=2,npts x=XM(k-1,:); f1=h*xpsys(t(k-1),x); XM(k,:)=XM(k-1,:)+f1 t(k)=t(k-1)+h; end
35
xpsys pseudo code to solve x’ = -3yy’ = x/3 f=xpsys(t,x) f(1)=-3*x(2); f(2)=x(1)/3;
36
main pseudo code to compare solution of x’ = -3yy’ = x/3 with exact: x=3cos(t), y=sin(t) Set parameter t0,x0,h, and npts Call Eulersys Display last time point and corresponding values of unknowns Plot solutions for both unknowns on the same axes Select points where exact values will be shown Calculate exact values at selected points Plot exact values on same axes as numerical solutions
37
Eulersys solution of x’ = -3y, y’ = x/3 compared to exact As expected, Eulersys not as accurate as Taylor4sys2 (see slide 31)
38
Pseudo-code for RK2 method applied to 1 ODE [t, x]=RK2(xp, t 0, x 0, h, npts) t(1)=t 0 ; x(1)=x 0 ; For k=2,npts f1=h*xp(t(k-1),x(k-1)); f2=h*xp(t(k-1)+h,x(k-1)+f1); x(k)=x(k-1)+(f1+f2)/2; t(k)=t(k-1)+h;
39
Pseudo-code for vectorised RK2sys [t, XM]=RK2sys(xpsys, t 0, x 0, h, npts) t(1)=t 0 ; XM(1,:)=x 0 ; For k=2,npts x=XM(k-1,:) f1=h*xpsys(t(k-1),x); t(k)=t(k-1)+h; f2=h*xpsys(t(k),x+f1); XM(k)=XM(k-1)+(f1+f2)/2;
40
Vectorised RK2sys can use the same xpsys and similar main as vectorised Eulersys to solve x’ = -3yy’ = x/3 and compare with exact: x=3cos(t), y=sin(t)
41
Pseudo code for RK4 applied to 1 ODE; just more F’s
42
Pseudo-code for RK4 method applied to 1 ODE [t, x]=RK4(xp, t 0, x 0, h, npts) t(1)=t 0 ; x(1)=x 0 ; For k=2,npts f1=h*xp(t(k-1), x(k-1)); f2=h*xp(t(k-1)+h/2, x(k-1)+f1/2); f3=h*xp(t(k-1)+h/2, x(k-1)+f2/2); f4=h*xp(t(k-1)+h, x(k-1)+f3); x(k)=x(k-1)+(f1+2f2+2f3+f4)/6; t(k)=t(k-1)+h;
43
Vectorised RK4sys Pseudo code for vectorised RK4sys is similar to vectorised RK2sys; just more F’s
44
Pseudo-code for vectorised RK4sys [t, XM]=RK2sys(xpsys, t 0, x 0, h, npts) t(1)=t 0 ; XM(1,:)=x 0 ; For k=2,npts x=XM(k-1,:) f1=h*xpsys(t(k-1),x); f2=h*xpsys(t(k-1)+h/2, x+f1/2); f3=h*xpsys(t(k-1)+h/2, x+f2/2); f4=h*xpsys(t(k-1)+h, x+f3); XM(k,:)=XM(k-1,:)+(f1+2f2+2f3+f4)/6; t(k)=t(k-1)+h;
45
Vectorised RK4sys can use the same xpsys and similar main as vectorised Eulersys to solve x’ = -3yy’ = x/3 and compare with exact: x=3cos(t), y=sin(t)
46
ODE45 can use a similar main as vectorised Eulersys to solve x’ = -3yy’ = x/3 and compare with exact: x=3cos(t), y=sin(t) but needs a slightly modified xpsys f=xpsys(t,x) f1=-3*x(2); f2=x(1)/3; f=[f1;f2]; xpsys returns a column vector in this case
47
Sometimes ODE45 fails ODE applied to x’ = t +x 2 – y; y’ = t 2 – x+y 2 ; x(0) = 3; y(0) = 2
48
Very rapid change in solution → ODE45 solution for system x’ = t +x 2 – yy’ = t 2 – x+y 2 x(0) = 3, y(0) = 2
49
Assignment 7 Due 3/10/2015: Use Euler, RK4, and ode45 to solve the system of equations x’=x – y + 2t - t 2 - t 3 y’=x + y - 4t 2 + t 3 for 0 < t < 1 subject to the initial condition x(0)=1, y(0)=0 Design Euler and RK4 so that {t(1),XM(1,:)} are the initial conditions. Use the same number of points in all 3 methods. For each method, print out the values of x and y at t=1, there percent difference from the exact values at t=1, and make separate plots that compares your results to the exact solutions x(t)=exp(t)cos(t) + t 2 y(t)=exp(t)sin(t) - t 3 for 0 < t < 1. Each plot must distinguish numerical from exact solutions.
50
Suggested problems from the text on ODEs Chapter 10.1 p436: Taylor series Problems 1a, 1b, 1e, 2a, 2b, 5, 10, 11a Computer problems 1, 2, 8, 9, 10 Chapter 10.2 p445: RK4 single ODE Problems 1, 2, 5, 6 Computer problems 1, 2, 4, 6, 10, 12 Chapter 11.1 p475: RK4 system of ODEs Computer problems 3, 5, 6, 7
51
Write xpsys functions for the systems on pp474-476 CK 6 th edition: P(1) x’ = y, y’ = x P(2) x 1 ’ = x 1 2 + exp(t) - t 2, x 2 ’ = x 2 - cos(t) CP((4) x’ = x + 2t – t 2 – t 3, y’ = y - 4t 2 + t 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.