Sei sulla pagina 1di 19

Chemical & Materials Engineering

Donadeo Innovation Centre for Engineering


Phone: 780-492-2881 | Email: cmeinfo@ualberta.ca

Website: www.cme.engineering.ualberta.ca

CHE-374: Computational Methods in Engineering, Winter 2016, UofA, Edmonton, Canada

CHE-374: Lecture 26: Section 7. Ordinary


Differentail Equations: Part I. One-Dimensional
Initial-Value ODEs
Dr. P.A. Nikrityuk
09.03.2016

Introduction
Math. Background

Initial Value - ODE

Finite Difference

Analytical Solution

Eulers Methods
ODE Classification

Differential equations arise in all fields of engineering and science. Most real
physical processes are governed by differential equations, e.g. unsteady heat
transfer in solids
2T
2T
T
Cp
= 2 + 2 + Q(t, x, y )
(1)
t
x
y
In general, most real physical processes involve more than one independent
variable, and the corresponding differential equations are partial differential
equations (PDEs). e.g. see Eq. (1).
In many cases, however, simplifying assumptions are made which reduce the
2
PDEs to ordinary differential equations (ODEs), e.g. ddxT
2 = Q(x) or
Cp T
= Q(t, T ).
t
An ordinary differential equation (ODE) is an equation stating a
relationship between a function of a single independent variable1 and the
total derivatives of this function with respect to the independent variable.

The dependent variable depends on the physical problem being modeled. In


most problems in engineering and science, the independent variable is either
time t or space x.
1

The variable y is used as a generic


dependent variable throughout this section.

Introduction

Initial Value - ODE

Math. Background

Finite Difference

Analytical Solution

Eulers Methods
ODE Classification

If x is the independent variable and y is the dependent variable, an ODE has


terms that contain:
dny
dy d 2 y
,
, ...,
(2)
x, y ,
dx dx 2
dx n
In general nth -order ODE for y (t) has the form
an

d n1 y
d 2y
dy
dny
+ an1 n1 + ... + a2 2 + a1
+ a0 y = F (x)
n
dx
dx
dx
dx

(3)

Eq. (3) represents linear ODE if the coefficients in Eq. (3) are all functions only
of the independent variable x, e.g.
dy
d 2x
dx
= 20x, 0.1 2 + 40
+ 5x = 0.
dx
dt
dt
An ODE written in the form of Eq. (3)is homogeneous if F (x) = 0 . Otherwise
ODE is nonhomogeneous.
The order of an ODE is determined by the order of the highest derivative that
appears in the equation.
When the independent variable is position and the constraints are specified at
two different positions, the constraints are called boundary conditions.
When the independent variable is time and the constraints are specified at a
single instant of time, the constraints are called initial conditions.

Introduction

Initial Value - ODE

Math. Background

Finite Difference

Analytical Solution

Eulers Methods
ODE Classification

General solution to a homogeneous second-order linear ODE with constant


coefficients
A homogeneous, second-order, linear ODE with constant coefficients can be
written in the form:
dy
d 2y
+b
+ cy = 0
(4)
dx 2
dx
where b and c are constants.
The general solution to this equation is obtained by substituting y = e sx . The
resulting equation is called the characteristic equation:
s2 + b s + c = 0

(5)

The solution of this equation is given by the quadratic formula:


s=

b 2 4c
2

(6)

The general solution of this equation has the form:





x
x
2
2
y (x) = e bx/2 C1 e 2 b 4c + C2 e 2 b 4c
where C1 and C2 are integration constants that are determined from the
problem-dependent constraints (i.e., boundary or initial conditions).

(7)

Introduction
Math. Background

Initial Value - ODE

Finite Difference

Analytical Solution

Eulers Methods
ODE Classification

There are two different types, or classes, of ODE, depending on the type of
auxiliary conditions specified.
If all the auxiliary conditions are specified at the same value of the independent
variable and the solution is to be marched forward from that initial point, the
differential equation is an initial-value ODE and problems are called
initial-value problem (IVP).
Basically almost all first order ODE can be considered as initial-value ODE.
However, high-order initial-value ODEs exist too.
Initial-value ODEs govern propagation (change in time) problems, which are
IVP in open domains.
If the auxiliary conditions are specified at two different values of the
independent variable, the end points or boundaries of the domain of interest, the
differential equation is a boundary-value ODE. Very often second-order ODEs
relate to the boundary-value ODEs.

Introduction

Initial Value - ODE

Finite Difference

Eulers Methods

First order Initial Value - ODE

A classical example of an initial value ODE is the general nonlinear first-order


ODE:
dy
= f (x, y ),
where
y (x0 ) = y0
(8)
dx
Here y is the dependent variable, x is the independent variable and y (x0 ) is
the initial condition for the dependent variable.

Example
Consider the lumped spherical hot particle (with mass m) which is placed in
cold air. The heat transfer from the lumped particle to its surroundings by
radiation is governed by the Stefan-Boltzmann law of radiation:
q = S B T 4 Ta4

(9)

where q is the heat transfer rate (J/s), S is the surface area of a particle (m2 ),
is the Stefan-Boltzmann constant (5.67 108 J/(m2 K4 s)), B is the
emissivity of the body (dimensionless), which is the ratio of the actual radiation
to the radiation from a black body ( 1), T is the internal temperature of the
lumped mass (K), and Ta is the ambient temperature (K) (i.e., the temperature
of the air).

Introduction

Initial Value - ODE

Finite Difference

Eulers Methods

First order Initial Value - ODE

The thermal energy E stored in the lumped particle is given by


E = m Cp T

(10)

where m is the mass of the lumped mass (kg) and Cp is the specific heat of the
material (J/kg-K).
An energy balance states that the rate at which the energy stored in the lumped
mass changes is equal to the rate at which heat is transferred to the
surroundings. Thus,2
d
(m Cp T ) = q = S B (T 4 Ta4 )
(11)
dt
If m and Cp are constants, then
dT
= (T 4 Ta4 ),
dt

where

S B
m Cp

(12)

Consider the case where the temperature of the surroundings is constant and
the initial temperature of the lumped mass is T (t = 0) = T0 . The initial-value
problem is stated as follows:
dT
= (T 4 Ta4 ) = f (t, T ),
T (t = 0) = T0
(13)
dt
Equation (13) is a nonlinear first-order initial-value ODE. The solution of
Eq. (13) is the function T (t), which describes the temperature history of the
lumped particle corresponding to the initial condition, T (t = 0) = T0 .
2
The minus sign in Eq. (11) is required so that the rate of change of stored energy is negative
when T is greater than Ta .
7

Introduction

Initial Value - ODE

Finite Difference Grids

Finite Difference

Finite Difference Approximation

Eulers Methods

Numerical Methods

Errors

Many ODEs that arise in practical applications cannot be solved analytically and
instead require the use of numerical methods for their solution.
One of such methods is Finite Difference Method.
In finite difference methods, the derivatives in the differential equation are
replaced with finite difference approximations.
This replacement requires discrete points . Thus, basically the independent
variable (t or x) interval, where we want to determine dependent variable (y ), is
divided into N subintervals of equal length t (time) or x (space).
The process of approximation a differential equation by finite differences at each
discrete point of independent variable xi is called discretization.
Vot IVP problem the value of the dependent variable at the beginning of
interval is known. This value is used as a start point.
The objective of a finite difference method for solving an ordinary differential equation
(ODE) is to transform a calculus problem into an algebra problem using the flowing
steps:
1. Discretizing the continuous physical domain into a discrete finite difference
grid.
2. Approximating the exact derivatives in the ODE by algebraic finite difference
approximations (FDAs).
3. Substituting the FDAs into the ODE to obtain an algebraic finite difference
equation (FDE).
4. Solving the resulting algebraic FDE.
8

Introduction

Initial Value - ODE

Finite Difference Grids

Finite Difference

Finite Difference Approximation

Eulers Methods

Numerical Methods

Errors

When a differential equation


dy
= f (x, y ),
dx

where

y (x1 ) = y1

(14)

is solved numerically, the problem statement also includes the domain of the
solution.
For example, a solution is required for values of the independent variable from
x = a to x = b (the domain is [a, b] ). Depending on the numerical method
used to solve the equation, the number of points between a and b at which the
solution is obtained has to be set in advance.
The domain can be divided into N subintervals of equal width defined by
N + 1 values of the independent variable from x1 = a to xN+1 = b. The
solution consists of values of the dependent variable that are determined at each
value of the independent variable xi .
The solution then is a set of points (x1 , y1 ), (x2 , y2 ), ..., (xN+1 , yN+1 ) that
define the function y (x) .
Domain

x1
a

x2

x3

x N2 x N1 x N

x N+1
b

Solution domain and discrete finite difference grid.

Introduction

Initial Value - ODE

Finite Difference Grids

Finite Difference

Finite Difference Approximation

Eulers Methods

Numerical Methods

Errors

Now that the finite difference grid has been specified, finite difference
approximations (FDAs) of the exact derivatives in the ODE must be developed.
FDA step is accomplished using the approaches explored in Section 5, where
approximations of various types (i.e., forward, backward, and centered) of
various orders (i.e., first order and second order) are developed for various
derivatives (i.e., first derivative, second derivative).
For example using two-point forward difference formula:
dy
yi +1 yi
+

dx i
xi +1 xi

(15)

O(x)
| {z }

Truncation error

Using this approximation first-order ODE can be written as follows:


dy
= f (x, y )
dx

yi +1 yi
= f (xi , yi )
x

= yi +1 = yi + x f (xi , yi )
{z
}
|
F (xi ,yi )

where xi +1 xi = x.

10

Introduction

Initial Value - ODE

Finite Difference Grids

Finite Difference

Finite Difference Approximation

Eulers Methods

Numerical Methods

Errors

The numerical solution is incremental process, which means that it is


determined in steps.
Applied to Initial Value-ODE, numerical solution always starts at the point
where the initial value is given.
Then, using the known solution at the first point, a solution is determined at a
second nearby point. This is followed by a solution at a third point, and so on.
All numerical methods for Initial Value-ODE can be divided on single-step and
multistep approaches.
In a single-step approach, the solution at the next point, xi +1 , is calculated
from the already known solution at the present point, xi .
In a multi-step approach, the solution at xi +1 is calculated from the known
solutions at several previous points, e.g. xi +1/2 . The idea is that the value of
the function at several previous points can give a better approximation of the
solution.
Applied to the calculation of f (x, y ) in finite-difference approximation of the
first-order ODE, see Eq. (14), two types of methods, explicit , and implicit ,
can be used for calculating the solution at each step.

11

Introduction

Initial Value - ODE

Finite Difference Grids

Finite Difference

Finite Difference Approximation

Eulers Methods

Numerical Methods

Errors

Explicit & Implicit


Explicit methods are those methods that use an explicit formula for calculating
the value of the dependent variable at the next value of the independent
variable, e.g.
yi +1 yi
= f (xi , yi )
x

= yi +1 = yi + x f (xi , yi )
|
{z
}
F (xi ,yi )

In an explicit formula, the right-hand side of the equation only has known
quantities.
Explicit methods have basically limitations for step size due to numerical
solution stability problems.
In implicit methods, the discretized first-order ODE equation used for
calculating yi +1 has the form:
yi +1 yi
= f (xi +1 , yi +1 )
x

= yi +1 = yi + x f (xi +1 , yi +1 )
|
{z
}
F (xi +1 ,yi +1 )

Here, the unknown yi +1 appears on both sides of the equation. Thus, in order
to calculate yi +1 we have to solve nonlinear equation using iterative-based
methods described in previous sections.
Implicit methods provide improved stability over explicit methods, but require
more effort at each step. Implicit methods do not have any limitation on step
size.
12

Introduction

Initial Value - ODE

Finite Difference Grids

Finite Difference

Finite Difference Approximation

Eulers Methods

Numerical Methods

Errors

EXAMPLE: Explicit & Implicit


Let us consider the following ODE:
dy
= 1.2y + 7e 0.3x
dx

Explicit finite-difference (FD) formulation is:



yi+1 yi
= 1.2yi +7e 0.3xi ; = yi+1 = yi +x 1.2yi + 7e 0.3xi
x
x
|i+1{z }i
x

Implicit finite-difference (FD) formulation is:


yi+1 yi
= 1.2yi+1 + 7e 0.3xi +1 ; =
xi+1 xi

yi+1 = yi + x 1.2yi+1 + 7e 0.3xi +1
13

Introduction

Initial Value - ODE

Finite Difference Grids

Finite Difference

Finite Difference Approximation

Eulers Methods

Numerical Methods

Errors

Two types of errors, round-off errors and truncation errors, occur when ODEs
are solved numerically.
Round-off errors are due to the way that computers carry out calculations.
Truncation errors are due to the approximate nature of the method used to
calculate the solution.
Since the numerical solution of a differential equation is calculated in increments
(steps), the truncation error at each step of the solution consists of two parts.
1. The first is so called local truncation error. It is due to the application of
the numerical method in a single step.
2. The second part, called propagated, or accumulated , truncation error, is
due to the accumulation of local truncation errors from previous steps.
Together, the two parts are the global (total) truncation error in the solution.

14

Introduction

Initial Value - ODE

Finite Difference

Eulers Explicit Method

Eulers Methods
R
MATLAB

Example

Eulers explicit method (also called the forward Euler method) is a single-step,
numerical technique for solving a first-order ODE of the form:
dy
= f (x, y ),
dx

where

y (x1 ) = y1

(16)

After the discretization of this equation the solution in i + 1 point has the form:
yi +1 = yi + x f (xi , yi )

(17)

where x = xi +1 xi
Deriving Eulers method by using numerical integration
Eq. (16) can be written as an integration problem by multiplying both sides by
dx:
Z x
Z y
i +1

i +1

f (x, y ) dx

dy =
yi

xi

Carrying out the integration on the left-hand side and solving for yi +1 gives:
Z x
i +1
f (x, y ) dx
yi +1 = yi +
xi

Evaluation of the second term on the right-hand side is an integral using the
rectangle method gives:
Z x
i +1
yi +1 = yi +
f (x, y ) dx = yi + f (xi , yi ) (xi +1 xi )
|
{z
}
xi
x

15

Introduction

Initial Value - ODE

Eulers Explicit Method

Finite Difference

Eulers Methods

Example

R
MATLAB

Solving a first-order ODE using Eulers explicit method


Use Eulers explicit method to solve the ODE
dy
= 1.2y + 7e 0.3x
dx

(18)

from x = 0 to x = 2.5 with the initial condition y = 3 at x = 0.


Solve by hand using x = 0.5.
The first point of the solution is x1 = 0, y1 = 3, which is initial solition point. To get
solution in each point we use the following equation:
yi +1 = yi + f (xi , yi ) x

Step 1. x2 = x1 + x = 0 + 0.5 = 0.5


y2 = y1 + (1.2y1 + 7e 0.3x1 )0.5 = 3 + (1.2(3) + 7e 0.3(0) )0.5 = 4.7
The second point is x2 = 0.5, y2 = 4.7.

16

Introduction

Initial Value - ODE

Eulers Explicit Method

Finite Difference
Example

Eulers Methods
R
MATLAB

Step 2. x3 = x2 + x = 0.5 + 0.5 = 1.


y3 = y2 + (1.2y2 + 7e 0.3x2 )0.5 = 4.7 + (1.2(4.7) + 7e 0.3(0.5) )0.5 = 4.893
The third point is x3 = 1., y3 = 4.893.
Step 3. x4 = x3 + x = 1. + 0.5 = 1.5
y4 = y3 +(1.2y3 +7e 0.3x3 )0.5 = 4.893+(1.2(4.893)+7e 0.3(1.) )0.5 = 4.550
The fourth point is x4 = 1.5, y4 = 4.550
Step 4. x5 = x4 + x = 1.5 + 0.5 = 2.
y5 = y4 +(1.2y4 +7e 0.3x4 )0.5 = 4.550+(1.2(4.550)+7e 0.3(1.5) )0.5 = 4.052
The fifth point is x5 = 2., y5 = 4.052
Step 5. x6 = x5 + x = 2. + 0.5 = 2.5
y6 = y5 +(1.2y5 +7e 0.3x5 )0.5 = 4.052+(1.2(4.052)+7e 0.3(2.) )0.5 = 3.542
The sixth point is x6 = 2.5, y6 = 3.542

17

Introduction

Initial Value - ODE

Eulers Explicit Method

Finite Difference
Example

Eulers Methods
R
MATLAB

R
MATLAB
-Example UDF Function
function [x, y] = ode1Eulerexplicit(ODE,a,b,h,yinit)
x(1) = a; y(1) = yinit;
N = (b-a)/h;
for i = 1:N
x(i+1) = x(i) + h;
y(i+1) = y(i) + ODE(x(i),y(i))*h;
end
%end of UDF function ode1Eulerexplicit
end

UDF Function
function dydx = ODE1example(x,y)
dydx = -1.2*y + 7*exp(-0.3*x);
%end of UDF function ODE1example
end
master script
clear all
a=0; b=2.5; h=0.1; yinit = 3;
[x, y] = ode1Eulerexplicit(@ODE1example,a,b,h,yinit);
xp=a:0.1:b;
% exact (analytical) solution
yp=70/9*exp(-0.3*xp)-43/9*exp(-1.2*xp);
plot(x,y,b,xp,yp)
xlabel(x); ylabel(y)

18

Introduction

Initial Value - ODE

Eulers Explicit Method

Finite Difference
Example

Eulers Methods
R
MATLAB

Numerical and analytical solution. Here o - h = x = 0.5 and * - h = x = 0.1.

19

Potrebbero piacerti anche