Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10 ordinary differential equations (ODEs)

Similar presentations


Presentation on theme: "Chapter 10 ordinary differential equations (ODEs)"— Presentation transcript:

1 Chapter 10 ordinary differential equations (ODEs)
Chapter 11 systems of ODEs (6th 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(t0) = x0
Solved by “separation of variables”

5 Usually a “boot strap” method is required
An approximate value of x(t1) was used to calculate x’(t1) Solution: use small values of h

6 With small h, we can use Taylor-series for x(tk+h)
Given

7 Pseudo code for Euler’s method to solve
x’=f(x,t), x(t0)=x0 t0<t<tmax Advance the table using x(t+h) = x(t) + hx’(t, x(t)) where h is the common step size Use the following conventions: initial condition is 1st table entry last table entry is at tmax npts=number of table entries

8 Pseudo code for Euler’s method
function [t,x]=euler(fh,t0,x0,tmax,npts) % fh is a function handle for x’=f(t,x(t)) Calculate number of steps Calculate the step size = h Assign values to t(1) and x(1) In a For-Loop from 1 to number of steps t(k+1)=t(k)+h x(k+1)=x(k)+h*fh(t(k),x(k)); % note the order of arguments in fh % …; in command window end

9 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

10 Example: Use Euler’s method to solve
x’ = 1 + x2 + t3 for x(t=2) given x(t=1) = -4 Use 10 points, plot result, and compare to “exact” value x(t=2)= (text p434) Write a script for this problem.

11 Example: Use Euler’s method to solve
x’ = 1 + x2 + t3 for x(t=2) given x(t=1) = -4 Use 10 points, plot result, and compare to “exact” value x(t=2)= (text p434) 1 + x^2 + t^3; exact= ; [t,x]=Euler(myxp,1,-4,2,10); plot(t,x) PD=100*abs((x(npts)-exact)/exact); disp([ t(npts), x(npts), PD])

12 Taylor-series methods
We can use analytical expressions for x’’(t), x’’’(t), etc., to develop a Taylor-series method of any order we choose; however, the accuracy of Euler can be improved by a simpler approach. Given

13 Predictor-Corrector Methods:
At every value of t, predict xp(t+h) Use xp(t+h) to get more accurate x(t+h) Example: extended Euler xp(t+h) = x(t) + hx’(t,x(t)) (normal Euler) x’(t,x(t)) is slope at t using x(t) x’(t+h,xp(t+h)) is slope at t+h using xp(t+h) Average these slopes to get a better x(t+h) x(t+h) = x(t) + h[x’(t,x(t))+x’(t+h, xp(t+h))]/2

14 Write MatLab code for Extended Euler’s method
xp(t+h) = x(t) + hx’(t, x(t)) x(t+h) = x(t) + h[x’(t, x(t))+x’(t+h, xp(t+h))]/2

15 Solver for Extended Euler method
function [t, x]=ex_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; dx1=h*fh(t(k),x(k)); dx2=h*fh(t(k+1),x(k)+dx1); x(k+1)=x(k)+(dx1+dx2)/2; end

16 Use Euler’s method to solve
Assignment 11, Due 3/21/19: Use Euler’s method to solve x’ = 1 + x2 + t3 for x(t=2) given x(t=1) = -4 using 10 points. Plot result. Display t(npts) and x(npts). Calculate percent difference of x(npts) from “exact” value x(t=2) = (text p434) Repeat with Extended Euler method

17 Taylor-series methods
We will show later that extended Euler is equivalent to Taylor-series with n=2. We can use analytical expressions for x’’(t), x’’’(t), etc., to develop a Taylor-series method of any order we choose How do we get these analytical expressions for x’’(t), x’’’(t), etc? Given

18 given etc. Higher-order derivatives for use in Taylor series methods
x’(t) has both explicit and implicit time dependence given etc. Use these analytical expressions for higher-order derivatives in the Taylor series to relate x(t+h) to x(t).

19 Pseudo code for high-order Taylor-series method
Given the value of x(t0) by initial conditions Evaluate x’(t0), x’’(t0), x’’’(t0), etc. Calculate x(t0+h) = x(t0)+hx’(t0)+h2x’’(t0)/2… Evaluate x’(t0+h), x’’(t0+h), x’’’(t0+h), etc. Calculate x(t0+2h) = x(t0+h)+hx’(t0+h)+h2x’’(t0+h)/2…

20 Example: Use 4th order Taylor series to solve x’ = 1 + x2 + t3 for x(t=2) given x(t=1) = -4. Use10 points. Plot result. Display t(npts) and x(npts). Calculate percent difference of x(npts) from “exact” value x(t=2) = (text p434) Compare percent difference with 4th order Taylor values obtained using Euler and extended Euler methods? First task, derive x”, x’”, and x(4) from x’(t,x(t))

21 x’ = 1 + x2 + t3 x” = 2xx’ + 3t2 x’’’ = 2xx’’ + 2x’x’ +6t x(4) = 2xx’’’ + 6x’x’’ + 6 By substitution, we can make x”, x’”, and x(4) functions of t and x(t) This is not necessary if we calculate x”, x’”, and x(4) in sequence. (i.e. use the value of x’ in calculation of x’’, use the value of x’’ in calculations of x’”, etc.)

22 Note: no function handle
This code is for a specific problem

23 Assignment 12, Due 3/21/19: Given that unknown function x has both explicit and implicit dependence on independent variable t and that x’ = t2 + x3 , Calculate by hand x’’(t,x), x’’’(t,x) and x(4)(t,x). Show all steps

24 Components of MatLab code to solve initial value problems
main solver encoder Solver: advances table x(t) → x(t+h), controls propagation of error, etc. is independent of a particular ODE Encoder: tells solver how to calculate x’(t) for all of the unknowns in the system. For single ODE, encoder can … in main For systems of ODEs, encoder must be a separate m-file Main: Defines initial conditions and domain of solution Sets solver parameters (number of points, error tolerance, etc,) Calls solver for the particular system of ODEs defined by encoder, Analyzes the results. Taylor-series method: solver and encoder are coupled; hence we need different solver for every problem

25 Runge-Kutta: another methods to increase accuracy without
higher order derivatives Given x’(t) = f(t, x(t)) and x(t0) = x0 Find method as accurate as Taylor series that does not involve higher order derivatives so that encoder can be separate from the solver. 2nd order Runge-Kutta: Look for solution of the form x(t + h) = x(t) + w1F1 + w2F2 where F1 = h f(t, x(t)) and F2 = h f(t +ah, x + bF1), that is as accurate as 2nd order Taylor series method If w2 = 0, reduces to Euler’s method if w1 = w2 = 1/2 and a = b = 1 reduces to extended Euler’s method

26 Look for solution of the form x(t + h) = x(t) + w1F1 + w2F2
where F1 = h f(t, x(t)), F2 = h f(t +ah, x + bF1), and equivalent to Taylor 2 Expanding f(t +ah, x + bF1) to 1st order in h gives x(t+h) to 2nd order in h

27 Constraints are satisfied by w1 = w2 = 1/2 and a = b = 1, which is choice that gives extended Euler.
Hence 2nd order Runge-Kutta is the same as extender Euler and both are equivalent to 2nd order Taylor series. Note: F1 alone is Euler’s method Pseudo-code text p443

28 4th order Runge-Kutta The same approach used to derive RK2 yields a result that is as accurate as Taylor 4th order. Write a solver for RK4 with same structure as solver for Euler’s method

29 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

30 Runge-Kutta-Fehlberg Method
Evaluate both 4th and 5th order Runge Kutta Use difference as estimate of error Adjust h to keep error in bounds

31 Estimate of the error in x(t)

32 Inside a loop to generate a table of values of x(t)
Basic idea behind MatLabs ODE45

33 MatLab’s ode45 solver (single ODE)
fh t+2*x*t Encoder is inline function. Specify domain of solution. ODE45 chooses points Initial values of x(t) Plot results Display number of points Compare exact to last value in table to exact

34 x(t) t ODE45 (solid curve) vs exact (points)
x(10) correct to 4 significant figures x(t) t

35 Assignment 14 due 3/26/19 Use ode45 to solve x’ = 1 + x2 + t3 for x(t=2) given x(t=1) = -4. 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) =

36 Components of MatLab codes to solve systems of differential equations
main solver encoder Solver: advances matrix XM(t) → XM(t+h), with columns that are the solutions to a system of differential equations. Independent of any particular system of ODEs. Encoder: m-file that tells solver how to calculate x’(t) for all of the unknowns in the system. Main: Defines initial conditions and domain of solution Sets solver parameters (number of points, error tolerance, etc, Calls solver for the particular system of ODEs defined by encoder, Analyzes the results.

37 Develop a solver for systems of ODEs using
Euler’s method

38 Euler’s method for a single ODE
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 Modify this function to return XM, matrix with columns that are solutions of unknowns in the problem For a system with 2 unknowns, x(t) and y(t), XM(:,1) can be x(t) and XM(:,2) can be y(t).

39 Solver for Eulersys compared on Euler
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 function [t,XM]=Eulersys(fh,t0,x0,tmax,npts) n=npts-1; h=(tmax-t0)/n; t(1)=t0; XM(1,:)=x0; % x0 is a row vector for k=1:n t(k+1)=t(k)+h; XM(k+1,:)=XM(k,:)+h*fh(t(k),XM(k,:)); % vector relationship end

40 Given a solver, next task is to write an encoder for the problem
Encoder is a .m file Inputs: t and a vector of values of all unknowns at t. Output: vector of values of the 1st derivative of all unknowns at t Example: 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 main solver encoder

41 Example: Write an Encoder for the system x’ = -3y, y’ = x/3
In a problem of this type, it may be helpful to rename the unknowns. x(t) -> x1(t) and y(t) -> x2(t) to indicate that x(t) is the 1st component of the vector passed to the encoder and y(t) is the second component. Similarly, x’(t) is the 1st component of the vector returned to the solver and y’(t) is the second component. function f=xpsys(t,x) f1=-3*x(2); f2=x(1)/3; f=[f1,f2]; % returns a row vector In the call to Eulersys, to show that the encoder is a .m file in the current directory

42 Write xpsys functions for the following systems
x’ = y y’ = x x’ = x2 + exp(t) - t2 y’ = y - cos(t) z’ = x - sin(t)

43 main solver encoder Driver code: 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 At t=4, calculate the percent difference from the exact solution, x=3cos(t), y=sin(t) at t=4 Make a plot that compares your solution (curve) with exact solution (points) for 0<t<4 If you use the same set of t-values for these plots, likely that numerical and exact solutions cannot be distinguished on the plot. To avoid this choose a different and smaller set of t-values to plot the exact solution.

44 E PD x(t=4) = 8.68% PD y(t=4) = 8.21%

45 eulersys solution of x’ = -3y, y’ = x/3 compared to exact

46 Given ex_Euler, write a function for ex_Eulersys
function [t, x]=ex_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; k1=h*fh(t(k),x(k)); k2=h*fh(t(k+1),x(k)+k1); x(k+1)=x(k)+(k1+k2)/2; end

47 write ex_Eulersys with no additional for loops
Given ex_Euler, write ex_Eulersys with no additional for loops [t,XM]=ex_Eulersys(fh,t0,x0,tmax,npts) n=npts-1; h=(tmax-t0)/n; t(1)=t0; XM(1,:)=x0; for k=1:n t(k+1)=t(k)+h; x=XM(k,:); k1=h*fh(t(k),x); k2=h*fh(t(k+1),x+k1): XM(k+1,:)=XM(k,:)+(k1+k2)/2; end [t, x]=ex_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; k1=h*fh(t(k),x(k)); k2=h*fh(t(k+1),x(k)+k1); x(k+1)=x(k)+(k1+k2)/2; end

48 MAIN to use ode45 similar to that used to run Eulersys and
ex_Eulersys except that ode45 requires tspan instead of t0, tmax and npts. ode45 also requires that the encoder return a column vector For the system x’=3y and y’=x/3 encoder could be function f=xpsys(t,x) f1=-3*x(2); f2=x(1)/3; f=[f1;f2];

49 apply ode45 to x’ = t +x2 – y; y’ = t2 – x+y2; x(0) = 3; y(0) = 2 Write the encoder

50 apply ode45 to x’ = t +x2 – y; y’ = t2 – x+y2; x(0) = 3; y(0) = 2
Write the encoder

51 apply ode45 to x’ = t +x2 – y; y’ = t2 – x+y2; x(0) = 3; y(0) = 2 Write the main for 0<t<0.38

52 tspan = [0,0.38]; x0=[3,2];

53 Apply ode45 to x’ = t +x2 – y; y’ = t2 – x+y2; x(0) = 3; y(0) = 2
Sometimes ode45 fails

54 ODE45 solution for system
x’ = t +x2 – y y’ = t2 – x+y2 x(0) = 3, y(0) = 2 Very rapid change in solution →

55 Assignment 14, Due 4/2/2019: Solve the system of equations x’=x – y + 2t – t2 – t3 y’=x + y – 4t2 + t3 for 0 < t < 3, subject to the initial condition x(0)=1, y(0)=0 Use Eulersys, ex_Eulersys, and ode45 with the same number of points Exact solutions are x(t)=exp(t)cos(t) + t2 and y(t)=exp(t)sin(t) - t3 For each method: Print out the values of x and y at t=3, Calculate the percent difference from the exact values at t=3 Make separate plots for each method that compare your results to the exact solution Make sure your plots can distinguish exact from numerical results)

56 Quiz #3: 4/4/19 Floating point number systems Loss of significance Text Chapter 2 Single and systems of ODEs Text Chapters 10,11 Covers material in HW11-15

57 Review of Floating Point Number Systems and Loss of Significance

58 Definition of a normalized floating point number system
b = base p = precision (number of digits: d0, d1,…dp-1) L = lower limit of exponent U = upper limit of exponent All floating point numbers in the system can be written as fl(d0,d1…dp-1) = (d0 + d1/b + d2/b dp-1/bp-1)bE In a normalized system d0  0 0 < di < b -1 for i = 1, ..., p-1 E is any integer such that L < E < U. A period is commonly placed between d0 and d1 The base is commonly indicated by a subscript on dp-1 Example: 1.112x21 = (3.5)10

59 Typical quiz question:
b = base = 2 p = precision (number of digits: d0, d1,…dp-1) =3 L = lower limit of exponent = -1 U = upper limit of exponent = 1 Total number of floating point numbers?, UFL?, OFL?, e mach?

60 Floating-point number systems are finite and discrete:
number of normalized floating-point numbers = 1+2(b-1)bp –1 (U – L + 1) explain smallest possible = UFL = bL explain largest possible = OFL = bU +1 (1-b-p) prove e mach = b1-p derive

61 What is emach in this system?
Note gap between 0 and bL (UFL) Filled be allowing d0 to be zero when exponent has its smallest value Called “sub-normals” What are the sub-normals in this system? How do sub-normal change the UFL?

62 All numbers of the form (0.b1b2b3)2 x 2k k = 0,+1
Which are excluded from a normalized system when b=2, p=3, L=-1, U=1? Which ones come back when sub-normals are allowed?

63 Loss of significance: when round-off error matters
Subtraction of two numbers of same sign and nearly the same magnitude results in the loss of the most significant (i.e. leading) digits of the operands Example: if 0 < e < emach then fl(1 + e) – fl(1 – e) = 1 – 1 = 0  2e Note that the subtraction operation is exact for its operands The rounding error prior to subtraction left the operands with inadequate precision. Subtraction has simply enhanced the implications of rounding error. Example: Calculating standard deviation (xi - <x>)2 safe method (2 passes) xi2 – n<x>2) sensitive to round-off error (single pass) s2 =


Download ppt "Chapter 10 ordinary differential equations (ODEs)"

Similar presentations


Ads by Google