Lecture 17_Chapter_23_initialValue(1)
Lecture 17_Chapter_23_initialValue(1)
with MATLAB®
for Engineers and Scientists
4th Edition
Steven C. Chapra
©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:
©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:
©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:
where:
©McGraw-Hill Education.
• Systems of Equations
Many practical problems require the solution of a
system of equations:
©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.
©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)
tspan = [0 5];
y0 = 0;
[t,y] = ode45(@(t,y) 3*t-3*y, tspan, y0);
©McGraw-Hill Education.
• Example - Predator-Prey
Solve:
©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',...)
©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)
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.