Sunteți pe pagina 1din 31
Numerical Solution of Differential Equations Matlab Tutorial
Numerical Solution of Differential Equations Matlab Tutorial
Numerical Solution of Differential Equations
Numerical Solution of Differential Equations

Numerical Solution of Differential Equations

Numerical Solution of Differential Equations Matlab Tutorial
Numerical Solution of Differential Equations Matlab Tutorial

Matlab Tutorial

Numerical Solution of Differential Equations Matlab Tutorial
Numerical Solution of Differential Equations Matlab Tutorial
Introduction  MATLAB has several routines for numerical integration ode45, ode23, ode113, ode15s, ode23s, etc.
Introduction  MATLAB has several routines for numerical integration ode45, ode23, ode113, ode15s, ode23s, etc.
Introduction  MATLAB has several routines for numerical integration ode45, ode23, ode113, ode15s, ode23s, etc.
Introduction  MATLAB has several routines for numerical integration ode45, ode23, ode113, ode15s, ode23s, etc.

Introduction

MATLAB has several routines for numerical integration ode45, ode23, ode113, ode15s, ode23s, etc.

Here we will introduce two of them: ode45 and ode23

ode23 uses 2 nd -order and ode45 uses 4 th -order Runge-Kutta integration.

Integration by ode23 and ode45: Matlab Command [t, x] = ode45( ‘xprime’ , [t 0
Integration by ode23 and ode45: Matlab Command [t, x] = ode45( ‘xprime’ , [t 0
Integration by ode23 and ode45: Matlab Command [t, x] = ode45( ‘xprime’ , [t 0
Integration by ode23 and ode45: Matlab Command [t, x] = ode45( ‘xprime’ , [t 0

Integration by ode23 and ode45:

Matlab Command

[t, x] = ode45(‘xprime’, [t 0 ,t f ], x 0 )

where

xprime

is a string variable containing the name of the

m-file for the derivatives.

t

t

x 0

f

0

is the initial time is the final time is the initial condition vector for the state variables

t a (column) vector of time

x an array of state variables as a function of time

Note  We need to generate a m-file containing expressions for differential equations first. We’ll
Note  We need to generate a m-file containing expressions for differential equations first. We’ll
Note  We need to generate a m-file containing expressions for differential equations first. We’ll
Note  We need to generate a m-file containing expressions for differential equations first. We’ll

Note

We need to generate a m-file containing expressions for differential equations first.

We’ll examine common syntax employed in generating the script or m-filecontaining expressions for differential equations first. These objectives will be achieved through 2 examples: 

These objectives will be achieved through 2 examples:common syntax employed in generating the script or m-file  Example-1 on Single-Variable Differential Equation 

Example-1 on Single-Variable Differential Equation

Example-2 on Multi-Variable Differential Equation

Differential Equation of a Single-Variable
Differential Equation of a Single-Variable
Differential Equation of a Single-Variable
Differential Equation of a Single-Variable
Differential Equation of a Single-Variable

Differential Equation of a Single-Variable

Differential Equation of a Single-Variable
Differential Equation of a Single-Variable
Differential Equation of a Single-Variable
Differential Equation of a Single-Variable
Differential Equation of a Single-Variable
Example 1: Start-up time of a CSTR Objective : Solve differential mole balance on a
Example 1: Start-up time of a CSTR Objective : Solve differential mole balance on a
Example 1: Start-up time of a CSTR Objective : Solve differential mole balance on a
Example 1: Start-up time of a CSTR Objective : Solve differential mole balance on a

Example 1: Start-up time of a CSTR

Objective: Solve differential mole balance on a CSTR using MATLAB integration routine. Problem description: A CSTR initially filled in 2mol/L of A is to be started up with specified conditions of inlet concentration, inlet and outlet flow rates. The reactor volume and fluid density is considered to be constant.

volume and fluid density is considered to be constant. v  o , C A 0

v

o

,C

A0

V
V
density is considered to be constant. v  o , C A 0 V v 

v,C

A

Reaction: A → B Rate Kinetics: (-r A ) = kC A Initial Condition: at t=0, C A = C A,initial = 2 mol/L

Example 1 The following first-order differential equation in single-variable (C A ) is obtained from
Example 1 The following first-order differential equation in single-variable (C A ) is obtained from
Example 1 The following first-order differential equation in single-variable (C A ) is obtained from
Example 1 The following first-order differential equation in single-variable (C A ) is obtained from

Example 1

The following first-order differential equation in single-variable (C A ) is obtained from mole balance on A:

dC

A

dt

v

V

v

o

V

V

=−

(

+

k

)

C

A +

C

A0

Recall, that mass balance yields

v= v

o

generating a m-file titled cstr.m function dx=cstr (t, x) % define constants k=0.005; %mol/L-s V=10;
generating a m-file titled cstr.m function dx=cstr (t, x) % define constants k=0.005; %mol/L-s V=10;
generating a m-file titled cstr.m function dx=cstr (t, x) % define constants k=0.005; %mol/L-s V=10;
generating a m-file titled cstr.m function dx=cstr (t, x) % define constants k=0.005; %mol/L-s V=10;

generating a m-file titled cstr.m

function dx=cstr (t, x)

% define constants

k=0.005; %mol/L-s V=10; % Reactor volume in L vin=0.15; % Inlet volumetric flow rate in L/s Ca0=10; % Inlet concentration of A in mol/L

%For convenience sake, declaring that variable x is Ca Ca=x

%define differential equation

dx=(vin/V)*Ca0-(vin/V+k)*Ca;

Script File: Common Syntax
Script File: Common Syntax
Script File: Common Syntax
Script File: Common Syntax

Script File: Common Syntax

Script File: Common Syntax
Script File: Common Syntax
Script File: Common Syntax
Script File: Common Syntax
Script File: Common Syntax
Purpose of function files function output=function_name (input1, input2) As indicated above, the function file generates
Purpose of function files function output=function_name (input1, input2) As indicated above, the function file generates
Purpose of function files function output=function_name (input1, input2) As indicated above, the function file generates
Purpose of function files function output=function_name (input1, input2) As indicated above, the function file generates

Purpose of function files

function output=function_name (input1, input2)

As indicated above, the function file generates the value of outputs every time it called upon with certain sets of inputs of dependent and independent variables

function dx=cstr (t, x)

For instance the cstr.m file generates the value of output (dx), every time it is called upon with inputs of independent variable time (t) and dependent variable (x)

NOTE: For cstr.m file, the output dx is actually dCa/dt and x is equal to Ca.

Function File: Command Structure function output=function_name (input1, input2) function dx = CSTR (t, x) Define
Function File: Command Structure function output=function_name (input1, input2) function dx = CSTR (t, x) Define
Function File: Command Structure function output=function_name (input1, input2) function dx = CSTR (t, x) Define
Function File: Command Structure function output=function_name (input1, input2) function dx = CSTR (t, x) Define

Function File: Command Structure

function output=function_name (input1, input2)

function dx = CSTR (t, x)

Define constants (e.g. k, Ca0, etc.)

(Optional) Write equations in terms of constants

Define differential equations that define outputs (dx=…)

File & Function Name Example: m-file titled cstr.m Function name should match file name function
File & Function Name Example: m-file titled cstr.m Function name should match file name function
File & Function Name Example: m-file titled cstr.m Function name should match file name function
File & Function Name Example: m-file titled cstr.m Function name should match file name function

File & Function Name

Example: m-file titled cstr.m

Function name should match file name

m-file titled cstr.m Function name should match file name function dx=cstr (t, x) % define constants

function dx=cstr (t, x)

% define constants

k=0.005; %mol/L-s V=10; % Reactor volume in L

Inputs and Outputs Example: m-file titled cstr.m Output is differential, dx = dCa/dt Inputs are
Inputs and Outputs Example: m-file titled cstr.m Output is differential, dx = dCa/dt Inputs are
Inputs and Outputs Example: m-file titled cstr.m Output is differential, dx = dCa/dt Inputs are
Inputs and Outputs Example: m-file titled cstr.m Output is differential, dx = dCa/dt Inputs are

Inputs and Outputs

Example: m-file titled cstr.m

Output is differential, dx = dCa/dt

m-file titled cstr.m Output is differential, dx = dCa/dt Inputs are independent variable (t) and dependent

Inputs are independent variable (t) and dependent variable (x=Ca)

are independent variable (t) and dependent variable (x=Ca) function dx=cstr (t, x) % define constants k=0.005;

function dx=cstr (t, x)

% define constants

k=0.005; %mol/L-s V=10; % Reactor volume in L

Writing Comments Example: m-file titled cstr.m Any text written after “ % ” symbol is
Writing Comments Example: m-file titled cstr.m Any text written after “ % ” symbol is
Writing Comments Example: m-file titled cstr.m Any text written after “ % ” symbol is
Writing Comments Example: m-file titled cstr.m Any text written after “ % ” symbol is

Writing Comments

Example: m-file titled cstr.m

Any text written after “ % ” symbol is considered to be commented

function dx=cstr (t, x)

is considered to be commented function dx=cstr (t, x) % define constants k=0.005; %mol/L-s V=10; %

% define constants

k=0.005; %mol/L-s V=10; % Reactor volume in L

Semicolon at the end of an expression Example: m-file titled cstr.m function dx=cstr (t, x)
Semicolon at the end of an expression Example: m-file titled cstr.m function dx=cstr (t, x)
Semicolon at the end of an expression Example: m-file titled cstr.m function dx=cstr (t, x)
Semicolon at the end of an expression Example: m-file titled cstr.m function dx=cstr (t, x)

Semicolon at the end of an expression

Example: m-file titled cstr.m

function dx=cstr (t, x)

% define constants

titled cstr.m function dx=cstr (t, x) % define constants k=0.005; %mol/L-s V=10; % Reactor volume in

k=0.005; %mol/L-s V=10; % Reactor volume in L

Semi-colon simply suppresses SCREEN printing of the expression.

End of Script File: Common Syntax”
End of Script File: Common Syntax”
End of Script File: Common Syntax”
End of Script File: Common Syntax”
End of Script File: Common Syntax”
End of Script File: Common Syntax”
End of Script File: Common Syntax”
End of Script File: Common Syntax”
End of Script File: Common Syntax”

End of Script File: Common Syntax”

End of Script File: Common Syntax”
Command for Integration of Differential Equation
Command for Integration of Differential Equation
Command for Integration of Differential Equation
Command for Integration of Differential Equation

Command for Integration of Differential Equation

Command for Integration of Differential Equation
Command for Integration of Differential Equation
Command for Integration of Differential Equation
Command for Integration of Differential Equation
Command for Integration of Differential Equation
Example-1  enter the following MATLAB command [t, x]=ode45(‘cstr’,[0 500],[2]’); Refer to slide-3 for syntax
Example-1  enter the following MATLAB command [t, x]=ode45(‘cstr’,[0 500],[2]’); Refer to slide-3 for syntax
Example-1  enter the following MATLAB command [t, x]=ode45(‘cstr’,[0 500],[2]’); Refer to slide-3 for syntax
Example-1  enter the following MATLAB command [t, x]=ode45(‘cstr’,[0 500],[2]’); Refer to slide-3 for syntax

Example-1

enter the following MATLAB command

[t, x]=ode45(‘cstr’,[0 500],[2]’);

Refer to slide-3 for syntax of above command

to see the transient responses, use plot function

plot(t, x);

Example-2: Multi-variable Differential Equations
Example-2: Multi-variable Differential Equations
Example-2: Multi-variable Differential Equations
Example-2: Multi-variable Differential Equations
Example-2:

Example-2:

Multi-variable Differential Equations

Multi-variable Differential Equations

Example-2: Multi-variable Differential Equations
Example-2: Multi-variable Differential Equations
Example-2: Multi-variable Differential Equations
Example-2: Multi-variable Differential Equations
Example-2: Multi-variable Differential Equations
Example-2: Multi-variable Differential Equations
Example 2: CSTR Response to change in volumetric flow rate.  Objective : Solve differential
Example 2: CSTR Response to change in volumetric flow rate.  Objective : Solve differential
Example 2: CSTR Response to change in volumetric flow rate.  Objective : Solve differential
Example 2: CSTR Response to change in volumetric flow rate.  Objective : Solve differential

Example 2: CSTR Response to change in volumetric flow rate.

Objective: Solve differential mole balance on a CSTR using MATLAB integration routine.

Problem description: CSTR operating at SS is subjected to a small disturbance in inlet volumetric flow rate while the outlet volumetric flow rate is kept constant. Both total mass balance and species mole balance must be solved simultaneously.

constant. Both total mass balance and species mole balance must be solved simultaneously. v  o

v

o

,C

A0

V
V
constant. Both total mass balance and species mole balance must be solved simultaneously. v  o

v,C

A

Example 2 First-order differential equation in two-variables – V(t) and C A (t): dC A
Example 2 First-order differential equation in two-variables – V(t) and C A (t): dC A
Example 2 First-order differential equation in two-variables – V(t) and C A (t): dC A
Example 2 First-order differential equation in two-variables – V(t) and C A (t): dC A

Example 2

First-order differential equation in two-variables – V(t) and C A (t):

dC

A v

=

o

(

C A

 

C

 

)

k

C

 

0

A

A

V

dt

) − k ⋅ C   0 A A V dt dV dt =  −

dV

dt

=v v

o

(1)

(2)

Equations (1) and (2) must be solved simultaneously.

Generating the script file function dx=cstr1 (t, x) %constant k=0.005; %mol/L-s vout=0.15; % L/s Ca0=10;
Generating the script file function dx=cstr1 (t, x) %constant k=0.005; %mol/L-s vout=0.15; % L/s Ca0=10;
Generating the script file function dx=cstr1 (t, x) %constant k=0.005; %mol/L-s vout=0.15; % L/s Ca0=10;
Generating the script file function dx=cstr1 (t, x) %constant k=0.005; %mol/L-s vout=0.15; % L/s Ca0=10;

Generating the script file

function dx=cstr1 (t, x) %constant k=0.005; %mol/L-s vout=0.15; % L/s Ca0=10; %mol/L % The following expression describe disturbance in input flow rate if((t >0)&(t <=2))

vin=0.15+.05*(t)

elseif((t>2)&(t<=4))

vin=0.25-0.05*(t-2);

else

vin=0.15;

end

% define x1 and x2

V=x(1,:)

Ca=x(2,:)

% write the differential equation

dx(1,:)=vin-vout;

dx(2,:)=(vin/V)*(Ca0-Ca)-k*Ca;

Script File: New Syntax
Script File: New Syntax
Script File: New Syntax
Script File: New Syntax

Script File: New Syntax

Script File: New Syntax
Script File: New Syntax
Script File: New Syntax
Script File: New Syntax
Script File: New Syntax
Recognizing Multivariable System The first important point to note is that x is a vector
Recognizing Multivariable System The first important point to note is that x is a vector
Recognizing Multivariable System The first important point to note is that x is a vector
Recognizing Multivariable System The first important point to note is that x is a vector

Recognizing Multivariable System

The first important point to note is that x is a vector of 2 variables, x1 (=V) and x2(=Ca)

is that x is a vector of 2 variables, x1 (=V) and x2(=Ca) Also, dx is

Also, dx is a vector of two differential equations associated with the 2 variables

two differential equations associated with the 2 variables function dx=cstr1 (t, x) % constant k=0.005; %mol/L-s

function dx=cstr1 (t, x) % constant k=0.005; %mol/L-s vout=0.15; % L/s Ca0=10; %mol/L

Defining arrays The value of these variables change as a function of time. This aspect
Defining arrays The value of these variables change as a function of time. This aspect
Defining arrays The value of these variables change as a function of time. This aspect
Defining arrays The value of these variables change as a function of time. This aspect

Defining arrays

The value of these variables change as a function of time. This aspect is denoted in MATLAB syntax by defining the variable as an array.

Thus variable 1 can be indicated as x(1,:) and variable 2 as x(2,:)

For bookkeeping purposes or convenience sake, the two variables are re-defined as follows

% define x1 and x2

V=x(1,:)

Ca=x(2,:)

purposes or convenience sake, the two variables are re-defined as follows % define x1 and x2
Defining differential equations There are two differential equations – dV/dt and dCa/dt – that must
Defining differential equations There are two differential equations – dV/dt and dCa/dt – that must
Defining differential equations There are two differential equations – dV/dt and dCa/dt – that must
Defining differential equations There are two differential equations – dV/dt and dCa/dt – that must

Defining differential equations

There are two differential equations – dV/dt and dCa/dt – that must be solved. These two equations are represented in vector form as “dx”

Two differential equations must be defined. The syntax is shown below

equations must be defined. The syntax is shown below % write the differential equation dx(1,:)=vin-vout;

% write the differential equation

dx(1,:)=vin-vout;

dx(2,:)=(vin/V)*(Ca0-Ca)-k*Ca;

End of “Script File: New Syntax”
End of “Script File: New Syntax”
End of “Script File: New Syntax”
End of “Script File: New Syntax”

End of “Script File: New Syntax”

End of “Script File: New Syntax”
End of “Script File: New Syntax”
End of “Script File: New Syntax”
End of “Script File: New Syntax”
End of “Script File: New Syntax”
Command for Integration of Differential Equation
Command for Integration of Differential Equation
Command for Integration of Differential Equation
Command for Integration of Differential Equation

Command for Integration of Differential Equation

Command for Integration of Differential Equation
Command for Integration of Differential Equation
Command for Integration of Differential Equation
Command for Integration of Differential Equation
Command for Integration of Differential Equation
Example-2  enter the following MATLAB command 7.5]’); [t, x]=ode45(‘cstr1’,[0 500],[10 Initial conditions for
Example-2  enter the following MATLAB command 7.5]’); [t, x]=ode45(‘cstr1’,[0 500],[10 Initial conditions for
Example-2  enter the following MATLAB command 7.5]’); [t, x]=ode45(‘cstr1’,[0 500],[10 Initial conditions for
Example-2  enter the following MATLAB command 7.5]’); [t, x]=ode45(‘cstr1’,[0 500],[10 Initial conditions for

Example-2

enter the following MATLAB command

7.5]’);
7.5]’);

[t, x]=ode45(‘cstr1’,[0 500],[10

Initial conditions for the two variables, i.e. V=10 L and CA=7.5 mol/L at time t=0
Initial conditions for the two variables, i.e. V=10 L and CA=7.5 mol/L at time t=0
Initial conditions for the two variables, i.e. V=10 L and CA=7.5 mol/L at time t=0

Initial conditions for the two variables, i.e. V=10 L and CA=7.5 mol/L at time t=0

to see the transient responses, use plot function

plot(t, x(:,1); plot(t, x(:,2);

Example-2  Did you spot any problems in the plots?  Do you see any
Example-2  Did you spot any problems in the plots?  Do you see any
Example-2  Did you spot any problems in the plots?  Do you see any
Example-2  Did you spot any problems in the plots?  Do you see any

Example-2

Did you spot any problems in the plots?

Do you see any transient response at all? Likely not.

It’s all to do with the “integration step-size”

Type the following Matlab commands

options=odeset('Initialstep',.1)

[t, x]=ode45('cstr1',[0 300],[10

 [t, x]=ode45('cstr1',[0 300],[10 7.5]',options)  Plot x1 and x2. (see command in

7.5]',options)

Plot x1 and x2. (see command in previous slide)

End of Matlab Tutorial
End of Matlab Tutorial
End of Matlab Tutorial
End of Matlab Tutorial

End of Matlab Tutorial

End of Matlab Tutorial
End of Matlab Tutorial
End of Matlab Tutorial
End of Matlab Tutorial
End of Matlab Tutorial