Download presentation
Presentation is loading. Please wait.
Published byBartholomew Heath 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 methods Given Pseudo code for Euler’s method
7
Higher-order Taylor series methods x’(t) has both explicit and implicit time dependence given Pseudo code for high-order Taylor-series method 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
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
9
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
10
Example of 4 th order Taylor-series method Main encoder-solver combination derivatives calculated in order nested form of Taylor series saves operations main solver encoder call encoder-solver
11
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
12
Develop MatLab’s approach for Euler’s method main solver encoder Start by developing the solver For singe ODE, encoder = inline function in main example: x’ = 1 + x 2 + t 3 for x(2) given x(1) = -4. Write main to get results you need similar to 4 th order Taylor example on slide 10 compare to “exact” value x(2)=4.371221866 (text p434)
13
A solver for Euler’s method x(t+h) = x(t) + hx’(t, x(t)) function [t,x]=euler(fh,t0,x0,tmax,npts) n=npts-1; h=(tmax-t0)/n; t(1)=t0; x(1)=x0; for k=1:n t(k+1)=t(k)+h; x(k+1)=x(k)+h*fh(t(k),x(k)); end
14
Runge-Kutta method: Given x’(t) = f(t, x(t)) and x(t 0 ) = x 0 Find method as accurate as Taylor series that does not involve higher order derivatives. More accurate than Euler and still an independent solver 2 nd order Runge-Kutta: Look for solution of the form x(t + h) = x(t) + w 1 F 1 + w 2 F 2 that is as accurate as 2 nd order Taylor series method where F 1 = h f(t, x(t))F 2 = h f(t + h, x + F 1 ). Note: if w 2 = 0, reduces to Euler’s method Parameters w 1, w 2, , and are independent of h
15
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 gives x(t+h) to 2 nd order in h
16
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. constraints are satisfied by
17
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
Pseudo code for RK4 text p443
19
A solver for Rk4 with same structure as solver for Euler’s method function [t,x]=RK4(fh,t0,x0,tmax,npts) n=npts-1; h=(tmax-t0)/n; t(1)=t0; x(1)=x0; for k=1:n K1=h*fh(t(k),x(k)); K2=h*fh(t(k)+h/2,x(k)+K1/2); K3=h*fh(t(k)+h/2,x(k)+K2/2); K4=h*fh(t(k)+h,x(k)+K3); t(k+1)=t(k)+h; x(k+1)=x(k)+(K1+2*K2+2*k3+k4)/6; end
20
Solve x’ = 1 + x 2 + t 3 for x(2) given x(1) = -4 by RK4 method with 100 points and compare to results with Euler method Start by developing the RK4 solver (done) For single ODE, encoder = inline function in main xp = inline(‘1 + x^2 + t^3’) for x(2) given x(1) = -4. Write main to get results you need call Euler solver call RK4 solver compare Euler to “exact” value x(2)=4.371221866 compare RK4 to exact value Euler4.23463.1251% error RK44.37122.4141x10 -5 % error
21
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
22
Estimate of the error in x(t)
23
Inside a loop to generate a table of values of X(t) Basic idea behind MatLabs ODE45
24
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
25
ODE45 337 points vs exact X(10) correct to 4 significant figures t x(t)
26
Use ode45 to solve x’ = 1 + x 2 + t 3 for x(2) given x(1) = -4. Write a solver for extended Euler (slide 16) using the same structure as the solver for Euler (slide 13). Use the same number of points as ode45 to solve for x(t) by Euler and extended Euler methods. In all 3 cases calculate the percent difference from the exact value x(2) = 4.371221866 on p434 of text. Hand in your Euler and extended Euler solvers and a copy of command window where all 3 methods were used. Assignment 4 due 11/19/15
27
Systems of ODEs Solved by Taylor, Euler, RK4 and ode45
28
Use the Taylor series method to solve the system x’ = -3y; y’ = x/3 with x(0)=3 and y(0)=0 to 4 th order in h for 0<t<4
29
Taylor series method applied to system x’ = -3yy’ = x/3
30
Set parameter t0,x0,tmax, and npts Call Taylor4 encoder-solver Display last time point and corresponding values of unknowns Plot solutions for both unknowns on the same axes as curves Select points where exact values will be shown Calculate exact values at selected points Plot exact values as discrete points on same axes as numerical solutions pseudo code for main that compares solution of x’ = -3y, y’ = x/3 with exact, x=3cos(t), y=sin(t), for 0<t<4 when x(0)=3 and y(0)=0
31
Compare Taylor solution to exact result
33
Euler, RK and ode45 applied to systems of ODEs main solver encoder Same structure applies but solver generates a matrix and encoder is a.m file that returns a vector. Start by writing the encoder that return x’(t,x(t)) for all the unknowns in the system Pseudo code for x’=-3y, y’=x/3
34
xpsys pseudo code to solve x’ = -3yy’ = x/3 f=xpsys(t,x) f(1)=-3*x(2); f(2)=x(1)/3;
35
[t,XM]=Eulersys(fh,t0,x0,tmax,neqs,npts) n=npts-1; h=(tmax-t0)/n; t(1)=t0; for i=1:neqs XM(1,i)=x0(i); %initial value all unknowns end for k=1:n for i=1:neqs x(i)=XM(k,i); %current value of all unknowns end f1=h*fh(t(k),x); %fh returns current slope of all unknowns for i=1:neqs XM(k+1,i)=XM(k,i)+f1(i); %new row added to solution matrix end t(k+1)=t(k)+h; end Solver for Eulersys using FOR loops
36
[t,XM]=Eulersys(fh, t0,x0,tmax,npts) n=npts-1; h=(tmax-t0)/n; t(1)=t0; XM(1,:)=x0; For k=1:n x=XM(k,:); f1=h*fh(t(k),x); XM(k+1,:)=XM(k,:)+f1 t(k+1)=t(k)+h; end Solver for Eulersys without FOR loop over neqs
37
Set parameter t0,x0,tmax, and npts Call Eulersys [t,XM]=Eulersys(@filename,t0,x0,tmax,npts); 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 Display last time point and corresponding values of unknowns Display exact values at tmax pseudo code for main that compares solution of x’ = -3y, y’ = x/3 with exact, x=3cos(t), y=sin(t), for 0<t<4 when x(0)=3 and y(0)=0
38
Find a numerical solution of x’ = -3y, y’ = x/3 for 0<t<4 by Euler’s method with 100 points when x(0)=3 and y(0)=0 Compare with exact solution, x=3cos(t), y=sin(t),
40
Eulersys solution of x’ = -3y, y’ = x/3 compared to exact As expected, Eulersys not as accurate as Taylor4sys2 (see slide 31)
41
ODE45 can use a similar main as Eulersys to solve x’ = -3y, y’ = x/3 when x(0)=3 and y(0)=0 and compare with exact: x=3cos(t), y=sin(t) but needs a slightly modified xpsys function f=xpsys(t,x) f1=-3*x(2); f2=x(1)/3; f=[f1;f2]; This function returns a column vector.
42
Sometimes ODE45 fails ODE applied to x’ = t +x 2 – y; y’ = t 2 – x+y 2 ; x(0) = 3; y(0) = 2
43
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
44
Assignment 5, Due 12/3/2015: Write a solver for extended Euler applied to a system of equations. Use your extended Eulersys 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 < 3, subject to the initial condition x(0)=1, y(0)=0 Use the same number of points for both methods. Exact solutions are x(t)=exp(t)cos(t) + t 2 and y(t)=exp(t)sin(t) - t 3 For each method, print out the values of x and y at t=3, there percent difference from the exact values at t=3, and make separate plots that compares your results to the exact solutions for each method.
45
[t,XM]=Eulersys(fh, t0,x0,tmax,npts) n=npts-1; h=(tmax-t0)/n; t(1)=t0; XM(1,:)=x0; For k=1:n x=XM(k,:); k1=h*fh(t(k),x); XM(k+1,:)=XM(k,:)+k1 t(k+1)=t(k)+h; end function f=xpsys(t,x) f(1)=-3*x(2); f(2)=x(1)/3; Solver for Eulersys without FOR loop over neqs [t, x]=RK2(fh, t0, x0, tmax, npts) n=npts-1; h=(tmax-t0)/n; t(1)=t0; x(1)=x0; for k=1:n k1=h*fh(t(k),x(k)); k2=h*fh(t(k)+h,x(k)+k1); x(k+1)=x(k)+(k1+k2)/2; t(k+1)=t(k)+h; end
46
RK4 method
47
[t, x]=RK4(xp, t0,x0, tmax, npts) n=npts-1; h=(tmax-t0)/n; t(1)=t0; x(1)=x0; for k=1:n f1=h*xp(t(k), x(k)); f2=h*xp(t(k)+h/2, x(k)+f1/2); f3=h*xp(t(k)+h/2, x(k)+f2/2); f4=h*xp(t(k)+h, x(k)+f3); x(k+1)=x(k)+(f1+2*f2+2*f3+f4)/6; t(k+1)=t(k)+h; end RK4 solver for 1 ODE
48
[t, XM]=RK4sys(xpsys, t0,x0,tmax, npts) n=npts-1; h=(tmax-t0)/n; t(1)=t0; XM(1,:)=x0; For k=1:n x=XM(k,:) f1=h*xpsys(t(k),x); f2=h*xpsys(t(k)+h/2, x+f1/2); f3=h*xpsys(t(k)+h/2, x+f2/2); f4=h*xpsys(t(k)+h, x+f3); XM(k+1,:)=XM(k,:)+(f1+2*f2+2*f3+f4)/6; t(k+1)=t(k)+h; end Solver for RK4sys without FOR loop over neqs
49
RK4sys can use the same xpsys as Eulersys and RK2sys (i.e. returns a row vector) Only ode45 requires xpsys to return a column vector Write xpsys for the system of ODEs x’=tx+y’-t 2 y’=ty+3t z’=tz-y’+6t 3
50
Wirte xpsys encoder for ode45 x’=tx+y’-t 2 y’=ty+3t z’=tz-y’+6t 3 function f=xpsys(t,xvec) x=xvec(1); y=xvec(2); z=xvec(3); yp=t*y+3*t; xp=t*x+yp-t^2; zp=t*z-yp+6*t^3; f=[xp;yp;xp];
51
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: single ODE Problems 1, 2, 5, 6 Computer problems 1, 2, 4, 6, 10, 12 Chapter 11.1 p475: system of ODEs Computer problems 3, 5, 6, 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.