0% found this document useful (0 votes)
8 views

Lecture 17_Chapter_23_initialValue(1)

This document is a chapter from 'Applied Numerical Methods with MATLAB® for Engineers and Scientists' focusing on initial-value problems and methods for solving ordinary differential equations (ODEs). It covers various numerical methods including Euler's method, Heun's method, and Runge-Kutta methods, along with error analysis and MATLAB implementations. Additionally, it discusses MATLAB's built-in functions for solving ODEs and provides examples for practical applications.

Uploaded by

kan nelson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 17_Chapter_23_initialValue(1)

This document is a chapter from 'Applied Numerical Methods with MATLAB® for Engineers and Scientists' focusing on initial-value problems and methods for solving ordinary differential equations (ODEs). It covers various numerical methods including Euler's method, Heun's method, and Runge-Kutta methods, along with error analysis and MATLAB implementations. Additionally, it discusses MATLAB's built-in functions for solving ODEs and provides examples for practical applications.

Uploaded by

kan nelson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Applied Numerical Methods

with MATLAB®
for Engineers and Scientists
4th Edition
Steven C. Chapra

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and


Prof. Steve Chapra, Tufts University

©McGraw-Hill Education. All rights reserved. Authorized only for instructor use in the classroom. No reproduction or further distribution permitted without the prior written consent of McGraw-Hill Education.
Part 6
Chapter 22
Initial-Value Problems

©McGraw-Hill Education. All rights reserved. Authorized only for instructor use in the classroom. No reproduction or further distribution permitted without the prior written consent of McGraw-Hill Education.
• Chapter Objectives
Understanding the meaning of local and global truncation
errors and their relationship to step size for one-step
methods for solving ODEs.
Knowing how to implement the following Runge-Kutta (RK)
methods for a single ODE:
• Euler
• Heun
• Midpoint
• Fourth-Order RK
Knowing how to iterate the corrector of Heun’s method.
Knowing how to implement the following Runge-Kutta
methods for systems of ODEs:
• Euler
• Fourth-order RK
©McGraw-Hill Education.
• Ordinary Differential Equations
Methods described here are for solving differential
equations of the form:

The methods in this chapter are all one-step


methods and have the general format:

where  is called an increment function, and is


used to extrapolate from an old value yi to a new
value yi+1.
©McGraw-Hill Education.
• Euler’s Method
The first derivative
provides a direct estimate
of the slope at ti:

and the Euler method


uses that estimate as the
increment function:

©McGraw-Hill Education.
• Error Analysis for Euler’s Method,
1
The numerical solution of ODEs involves two types
of error:
• Truncation errors, caused by the nature of the techniques
employed
• Roundoff errors, caused by the limited numbers of
significant digits that can be retained
The total, or global truncation error can be further
split into:
• local truncation error that results from an application
method in question over a single step, and
• propagated truncation error that results from the
approximations produced during previous steps.
©McGraw-Hill Education.
• Error Analysis for Euler’s Method,
2
The local truncation error for Euler’s method
is O(h2) and proportional to the derivative of
f(t,y) while the global truncation error is O(h).
This means:
• The global error can be reduced by decreasing
the step size, and
• Euler’s method will provide error-free
predictions if the underlying function is linear.
Euler’s method is conditionally stable,
depending on the size of h.
©McGraw-Hill Education.
• MATLAB Code for Euler’s Method

©McGraw-Hill Education.
• Heun’s Method
One method to improve Euler’s method is to determine derivatives at
the beginning and predicted ending of the interval and average them:

This process relies on making a prediction of the new value of y, then


correcting it based on the slope calculated at that new value.
This predictor-corrector approach can be iterated to convergence:

©McGraw-Hill Education.
• Midpoint Method
Another improvement to Euler’s method is similar
to Heun’s method, but predicts the slope at the
midpoint of an interval rather than at the end:

This method has a local truncation error of O(h3)


and global error of O(h2)
©McGraw-Hill Education.
• Runge-Kutta Methods
Runge-Kutta (RK) methods achieve the accuracy of a Taylor
series approach without requiring the calculation of higher
derivatives.
For RK methods, the increment function  can be generally
written as:

where the a’s are constants and the k’s are

where the p’s and q’s are constants.


©McGraw-Hill Education.
• Classical Fourth-Order Runge-
Kutta Method
The most popular RK methods are fourth-order, and
the most commonly used form is:

where:

©McGraw-Hill Education.
• Systems of Equations
Many practical problems require the solution of a
system of equations:

The solution of such a system requires that n initial


conditions be known at the starting value of t.

©McGraw-Hill Education.
• Solution Methods
Single-equation methods can be used to
solve systems of ODE’s as well; for
example, Euler’s method can be used on
systems of equations - the one-step method
is applied for every equation at each step
before proceeding to the next step.
Fourth-order Runge-Kutta methods can also
be used, but care must be taken in
calculating the k’s.
©McGraw-Hill Education.
• MATLAB RK4 Code

©McGraw-Hill Education.
• MATLAB Functions
MATLAB’s ode23 function uses second- and third-
order RK functions to solve the ODE and adjust
step sizes.

MATLAB’s ode45 function uses fourth- and fifth-


order RK functions to solve the ODE and adjust
step sizes. This is recommended as the first
function to use to solve a problem.

MATLAB’s ode113 function is a multistep solver


useful for computationally intensive ODE functions.

©McGraw-Hill Education.
• Using ode Functions
The functions are generally called in the same way;
ode45 is used as an example:
[t, y] = ode45(odefun, tspan, y0)
• y: solution array, where each column represents one of
the variables and each row corresponds to a time in the t
vector
• odefun: function returning a column vector of the right-
hand-sides of the ODEs
• tspan: time over which to solve the system
• If tspan has two entries, the results are reported for those times
as well as several intermediate times based on the steps taken by
the algorithm
• If tspan has more than two entries, the results are reported only
for those specific times
• y0: vector of initial values
©McGraw-Hill Education.
• Using ode45 Function
[t,y] = ode45(odefun,tspan,y0,options)

Solve: dY/dt -3Y=3t , Y(0)=0

tspan = [0 5];
y0 = 0;
[t,y] = ode45(@(t,y) 3*t-3*y, tspan, y0);

©McGraw-Hill Education.
• Example - Predator-Prey
Solve:

with y1(0)=2 and y2(0)=1 for 20 seconds


predprey.m M-file:
function yp = predprey(t, y)
yp = [1.2*y(1)-0.6*y(1)*y(2);-0.8*y(2)+0.3*y(1)*y(2)];
Script:
tspan = [0 20];
y0 = [2, 1];
[t, y] = ode45(@predprey, tspan, y0);
figure(1); plot(t,y); figure(2); plot(y(:,1),y(:,2));

©McGraw-Hill Education.
• ODE45 Example

©McGraw-Hill Education.
• ODE Solver Options
Options to ODE solvers may be passed as an
optional fourth argument, and are generally created
using the odeset function:
options=odeset('par1','val1','par2','val2',...)

Commonly used parameters are:


• 'RelTol': adjusts relative tolerance
• 'AbsTol': adjusts absolute tolerance
• 'InitialStep': sets initial step size
• 'MaxStep': sets maximum step size (default: one tenth of
tspan interval)
©McGraw-Hill Education.
• Stiffness
A stiff system is one involving rapidly changing components
together with slowly changing ones.
An example of a single stiff ODE is:

whose solution if y(0) = 0 is:

©McGraw-Hill Education.
• MATLAB Functions for Stiff
Systems
MATLAB has a number of built-in functions
for solving stiff systems of ODEs, including
ode15s, ode23, ode23t, and ode23tb.
The arguments for the stiff solvers are the
same as those for previous solvers.

©McGraw-Hill Education.
• DSOLVE Command
S = dsolve(eqn,cond,Name,Value)
Solve ODE : 2y’=y

dsolve(‘2*Dy=y’)

ans =

C1*exp(t/2)

©McGraw-Hill Education.
cond = [y(0)==b, Dy(0)==1];

• DSOLVE Command
S = dsolve(eqn,cond,Name,Value)

Solve : y’’=x , y(0)=5, y’(0)=1

dsolve('D2y=t','y(0) == 5,Dy(0)=1')

©McGraw-Hill Education.
• DSOLVE vs ODE45
S = dsolve(eqn,cond,Name,Value)
Solve: 3y”+xy’+3x=sin(x)

syms y(x)
eqn=3*diff(y,x,2)+x*diff(y,x)
+3*x==sin(x)
dsolve(eqn)

©McGraw-Hill Education.

You might also like