Chapter 10 ordinary differential equations (ODEs)

Slides:



Advertisements
Similar presentations
Chapter 6 Differential Equations
Advertisements

MATLAB EXAMPLES Initial-value problems
Numerical Methods for Partial Differential Equations CAAM 452 Spring 2005 Lecture 9 Instructor: Tim Warburton.
Roundoff and truncation errors
ECIV 201 Computational Methods for Civil Engineers Richard P. Ray, Ph.D., P.E. Error Analysis.
The Islamic University of Gaza Faculty of Engineering Civil Engineering Department Numerical Analysis ECIV 3306 Chapter 3 Approximations and Errors.
Dr. Jie Zou PHY Chapter 9 Ordinary Differential Equations: Initial-Value Problems Lecture (II) 1 1 Besides the main textbook, also see Ref.: “Applied.
NUMERICAL SOLUTION OF ORDINARY DIFFERENTIAL EQUATIONS
Chapter 10 ordinary differential equations (ODEs) Chapter 11 systems of ODEs (6 th edition)
Differential Equations and Boundary Value Problems
Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential.
Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers Daniel Baur ETH Zurich, Institut.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. by Lale Yurttas, Texas A&M University Chapter 31.
Ch 8.1 Numerical Methods: The Euler or Tangent Line Method
Introduction and Analysis of Error Pertemuan 1
Chapters 5 and 6: Numerical Integration
EE3561_Unit 8Al-Dhaifallah14351 EE 3561 : Computational Methods Unit 8 Solution of Ordinary Differential Equations Lesson 3: Midpoint and Heun’s Predictor.
Integration of 3-body encounter. Figure taken from
MECN 3500 Inter - Bayamon Lecture 3 Numerical Methods for Engineering MECN 3500 Professor: Dr. Omar E. Meza Castillo
Differential Equations Copyright © Cengage Learning. All rights reserved.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Chapter 3.
Sensitivity derivatives Can obtain sensitivity derivatives of structural response at several levels Finite difference sensitivity (section 7.1) Analytical.
Today’s class Numerical differentiation Roots of equation Bracketing methods Numerical Methods, Lecture 4 1 Prof. Jinbo Bi CSE, UConn.
Chapter 10 ordinary differential equations (ODEs) Chapter 11 systems of ODEs (6 th edition)
Intro to Simulink April 15, Copyright , Andy Packard. This work is licensed under the Creative Commons.
Please remember: When you me, do it to Please type “numerical-15” at the beginning of the subject line Do not reply to my gmail,
Ch 8.2: Improvements on the Euler Method Consider the initial value problem y' = f (t, y), y(t 0 ) = y 0, with solution  (t). For many problems, Euler’s.
Dr. Mujahed AlDhaifallah ( Term 342)
ENGR-25_Lec-21_Integ_Diff.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
ECE 576 – Power System Dynamics and Stability Prof. Tom Overbye Dept. of Electrical and Computer Engineering University of Illinois at Urbana-Champaign.
ACSL, POSTECH1 MATLAB 입문 CHAPTER 8 Numerical Calculus and Differential Equations.
1/14  5.2 Euler’s Method Compute the approximations of y(t) at a set of ( usually equally-spaced ) mesh points a = t 0 < t 1
Intro to Simulink Modified by Gary Balas 20 Feb 2011 Copyright , Andy Packard. This work is licensed under.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 6 - Chapters 22 and 23.
Chapters 5 and 6: Numerical Integration Code development trapezoid rule Simpson’s rule Gauss quadrature Laguerre quadrature Analysis changing the variable.
Numerical Analysis CC413 Propagation of Errors. 2 In numerical methods, the calculations are not made with exact numbers. How do these inaccuracies propagate.
Assignment 1: due 1/19/16 Estimate all of the zero of x3-x2-2x+1 graphically. Write a MatLab code for Newton’s method. Use your code to refine the graphical.
1 Week 11 Numerical methods for ODEs 1.The basics: finite differences, meshes 2.The Euler method.
Announcements Please read Chapters 11 and 12
1.1 Basic Concepts. Modeling
Chapter 4 Dynamical Behavior of Processes Homework 6 Construct an s-Function model of the interacting tank-in-series system and compare its simulation.
Ordinary Differential Equations
Engineering Problem Solution
Modeling and Simulation Dr. Mohammad Kilani
Part 7 - Chapter 25.
Chapter 4 Dynamical Behavior of Processes Homework 6 Construct an s-Function model of the interacting tank-in-series system and compare its simulation.
Ordinary Differential Equations
ECE 576 – Power System Dynamics and Stability
Mechanical Engineering at Virginia Tech
525602:Advanced Numerical Methods for ME
Class Notes 18: Numerical Methods (1/2)
Numerical Solutions of Ordinary Differential Equations
SE301: Numerical Methods Topic 8 Ordinary Differential Equations (ODEs) Lecture KFUPM (Term 101) Section 04 Read , 26-2, 27-1 CISE301_Topic8L4&5.
Approximations and Round-Off Errors Chapter 3
Chapter 26.
Part 7 - Chapter 25.
Numerical Analysis Lecture 37.
Numerical Solutions of Ordinary Differential Equations
SE301: Numerical Methods Topic 8 Ordinary Differential Equations (ODEs) Lecture KFUPM (Term 101) Section 04 Read , 26-2, 27-1 CISE301_Topic8L2.
Numerical Analysis Lecture 38.
Numerical solution of first-order ordinary differential equations
Assignment 1: due 1/16/19 Estimate all of the zero of x3-x2-2x+1 graphically. Write a MatLab code for Newton’s method. Use your code to refine the graphical.
Chapter 2: Floating point number systems and Round-off error
Assignment 1: due 1/17/19 Estimate all of the zero of x3-x2-2x+1 graphically. Write a MatLab code for Newton’s method. Use your code to refine the graphical.
Differential equations
Numerical Analysis Lecture 36.
Numerical solution of first-order ordinary differential equations 1. First order Runge-Kutta method (Euler’s method) Let’s start with the Taylor series.
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM CISE301_Topic1.
Modeling and Simulation: Exploring Dynamic System Behaviour
Presentation transcript:

Chapter 10 ordinary differential equations (ODEs) Chapter 11 systems of ODEs (6th edition)

System of ODEs

Initial value problems Regardless of problem statement, identify f(x,t), a, b, and x(a)

Initial-value problem: x’ = f(x,t); x(t0) = x0 Solved by “separation of variables”

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

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

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

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 % myxp=@(t,x) …; in command window end

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

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)=4.371221866 (text p434) Write a script for this problem.

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)=4.371221866 (text p434) myxp=@(t,x) 1 + x^2 + t^3; exact=4.371221866; [t,x]=Euler(myxp,1,-4,2,10); plot(t,x) PD=100*abs((x(npts)-exact)/exact); disp([ t(npts), x(npts), PD])

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

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

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

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

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) = 4.371221866 (text p434) Repeat with Extended Euler method

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

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).

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…

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) = 4.371221866 (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))

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.)

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

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

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 be @(t,x) … 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

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

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

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

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

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

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

Estimate of the error in x(t)

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

MatLab’s ode45 solver (single ODE) fh = @(t,x) 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

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

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) = 4.371221866.

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.

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

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).

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

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

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, use @xpsys to show that the encoder is a .m file in the current directory

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

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.

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

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

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

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

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];

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

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

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

tspan = [0,0.38]; x0=[3,2]; [t,x]=ode45(@xpsys_cp2_p475_CK6,tspan,x0);

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

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

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)

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

Review of Floating Point Number Systems and Loss of Significance

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/b2 + ... + 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

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?

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

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?

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?

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 =