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

Preview:

Citation preview

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

Taylor-series method

Given

Higher-order Taylor series methods

x’(t) has both explicit and implicit time dependencegiven

Given the value of x at t = t0

evaluate x’(t0), x’’(t0), x’’’(t0), etc. → x(t0+h) = x(t0)+hx’(t0)+h2x’’(t0)/2…

then evaluate x’(t0+h), x’’(t0+h), x’’’(t0+h), etc. → x(t0+2h) = x(t0+h)+…

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 ODETo solve a single ODE, encoder passed to solver as function handleFor systems of ODEs, encoder passed to solver as an m-file

Main: Defines initial conditions and domain of solutionSets 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

solverencoder

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 asan 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

solverencoder

Develop approach for Euler methodx(t+h) = x(t) + hx’(t, x(t))

main

solverencoder

Start by developing the solver

For singe ODE, encoder = inline function

Write main to get results you need

MatLab code 4th 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 separateSee pseudo-code text p434

main

solverencoder

Problems 10.1-10 and 10.1-11 p437

x’ = t2 + x3 5 equations needed for 4th order Taylor

x’ = x + ex 5 equations needed for 4th order Taylor

x’ = x2 – cos(x) 6 equations needed for 5th order

Runge-Kutta method:

x’(t) = f(t, x(t)) x(t0) = x0

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) + w1F1 + w2F2

where F1 = h f(t, x(t)) F2 = h f(t +ah, x + bF1).

Note: h in definition of F1 and F2 so they have the same units as x.

All 4 parameters, w1, w2, a, and b are dimensionless

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)

Expanding f(t +ah, x + bF1) to 1st order in h give x(t+h) to 2nd order in h

Note: F1 alone is Euler’s method

Pseudo-code text p443

This result also called “ extended Euler” method.

In the 3rd 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

4th order Runge-Kutta

The same method used for RK2 yields a result that is as accurate as Taylor 4th order. See text p443 for pseudo code.

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

Encoder is inline function.

Specify domain of solution. ODE45 chooses points

Plot results

Display number of pointsCompare exact to last value in table to exact

ODE45 337 points vs exactX(10) correct to 4 significant figures

t

x(t)

main

solverencoder

Use the design above to implement Euler, RK2, RK4, and ODE45

methods to solve x’ = 1 + t3 +x2 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

Predictor-Corrector Methods:

At every value of t, predict xp(t+h)

Use xp(t+h) to get more accurate x(t+h)

Example: Improved Euler (p10.1-15 p437)

xp(t+h) = x(t) + hx’(t,x(t)) (normal Euler)

x(t+h) = x(t) + h[x’(t,x(t))+x’(t+h, xp(t+h))]/2

Is this the same as RK2?

Higher-order predictor - corrector

Use same approach to derive a corrector

Systems of ODEsSolved by Taylor, Euler, RK and ODE45

Use the Taylor series method to solve the the system x’ = -3y y’ = x/3

Taylor series method applied to system x’ = -3y y’ = x/3

Compare Taylor solution to exact result

Euler, RK and ODE45 applied to systems of ODEs

Pseudo-code for Euler method applied to 1 ODE

[t, x]=Euler(xp, t0, x0, h, npts)t(1)=t0;x(1)=x0;For k=2,npts

x(k)=x(k-1)+h*xp(t(k-1),x(k-1));t(k)=t(k-1)+h;

end

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,:)+f1t(k)=t(k-1)+h;

end

xpsys pseudo code to solve x’ = -3y y’ = x/3

f=xpsys(t,x)f(1)=-3*x(2);f(2)=x(1)/3;

main pseudo code to compare solution of x’ = -3y y’ = x/3with exact: x=3cos(t), y=sin(t)

Set parameter t0,x0,h, and nptsCall EulersysDisplay last time point and corresponding values of unknownsPlot solutions for both unknowns on the same axesSelect points where exact values will be shownCalculate exact values at selected pointsPlot exact values on same axes as numerical solutions

Eulersys solution of x’ = -3y, y’ = x/3 compared to exactAs expected, Eulersys not as accurate as Taylor4sys2 (see slide 31)

Pseudo-code for RK2 method applied to 1 ODE

[t, x]=RK2(xp, t0, x0, h, npts)t(1)=t0;x(1)=x0;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;

Pseudo-code for vectorised RK2sys

[t, XM]=RK2sys(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);t(k)=t(k-1)+h;f2=h*xpsys(t(k),x+f1);XM(k)=XM(k-1)+(f1+f2)/2;

Vectorised RK2sys can use the same xpsys and similar main as vectorised Eulersys to solve x’ = -3y y’ = x/3and compare with exact: x=3cos(t), y=sin(t)

Pseudo code for RK4 applied to 1 ODE; just more F’s

Pseudo-code for RK4 method applied to 1 ODE

[t, x]=RK4(xp, t0, x0, h, npts)t(1)=t0;x(1)=x0;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;

Vectorised RK4sys

Pseudo code for vectorised RK4sys is similar to vectorised RK2sys; just more F’s

Pseudo-code for vectorised RK4sys

[t, XM]=RK2sys(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);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;

Vectorised RK4sys can use the same xpsys and similar main as vectorised Eulersys to solve x’ = -3y y’ = x/3and compare with exact: x=3cos(t), y=sin(t)

ODE45 can use a similar main as vectorised Eulersys to solve x’ = -3y y’ = x/3and 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

Sometimes ODE45 failsODE applied to x’ = t +x2 – y; y’ = t2 – x+y2; x(0) = 3; y(0) = 2

Very rapid change in solution →

ODE45 solution for systemx’ = t +x2 – y y’ = t2 – x+y2 x(0) = 3, y(0) = 2

Assignment 7 Due 3/10/2015:

Use Euler, RK4, and ode45 to solve the system of equations x’=x – y + 2t - t2 - t3 y’=x + y - 4t2 + t3 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, andmake separate plots that compares your results to the exact solutions x(t)=exp(t)cos(t) + t2 y(t)=exp(t)sin(t) - t3for 0 < t < 1.

Each plot must distinguish numerical from exact solutions.

Suggested problems from the text on ODEs

Chapter 10.1 p436: Taylor seriesProblems 1a, 1b, 1e, 2a, 2b, 5, 10, 11aComputer problems 1, 2, 8, 9, 10

Chapter 10.2 p445: RK4 single ODEProblems 1, 2, 5, 6Computer problems 1, 2, 4, 6, 10, 12

Chapter 11.1 p475: RK4 system of ODEsComputer problems 3, 5, 6, 7

Write xpsys functions for the systems on pp474-476 CK 6th edition:

P(1) x’ = y, y’ = x

P(2) x1’ = x12 + exp(t) - t2, x2’ = x2 - cos(t)

CP((4) x’ = x + 2t – t2 – t3, y’ = y - 4t2 + t3

Recommended