matlab ode solvers
DESCRIPTION
A brief introduction and tutorial to solving differential equations in Matlab.A good start for someone who wants to use Matlab just for solving complex equations.TRANSCRIPT
![Page 1: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/1.jpg)
Numerical Solution of Differential Equations
Matlab Tutorial
![Page 2: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/2.jpg)
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 2nd-order and ode45 uses 4th-order Runge-Kutta
integration.
![Page 3: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/3.jpg)
Integration by ode23 and ode45: Matlab Command
[t, x] = ode45(‘xprime’, [t0,tf], x0) where
xprime is a string variable containing the name of the m-file for the derivatives.
t0 is the initial time tf is the final time x0 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
![Page 4: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/4.jpg)
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-file
These objectives will be achieved through 2 examples: Example-1 on Single-Variable Differential Equation
Example-2 on Multi-Variable Differential Equation
![Page 5: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/5.jpg)
Differential Equation of a Single-Variable
![Page 6: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/6.jpg)
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.
Reaction: A → B Rate Kinetics: (-rA) = k⋅CA
Initial Condition: at t=0, CA = CA,initial = 2 mol/L
0Ao Cv ,
ACv ,
V
![Page 7: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/7.jpg)
Example 1 The following first-order differential equation in
single-variable (CA) is obtained from mole balance on A:
0A
oA
A CVvCk
Vv
dtdC
⋅+⋅+−=
)(
ovv =
Recall, that mass balance yields
![Page 8: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/8.jpg)
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;
![Page 9: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/9.jpg)
Script File: Common Syntax
![Page 10: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/10.jpg)
Purpose of function files
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 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 dx=cstr (t, x)
function output=function_name (input1, input2)
![Page 11: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/11.jpg)
Function File: Command Structure
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=…)
function output=function_name (input1, input2)
![Page 12: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/12.jpg)
File & Function Name
Example: m-file titled cstr.m
function dx=cstr (t, x) % define constants k=0.005; %mol/L-s V=10; % Reactor volume in L
Function name should match file name
![Page 13: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/13.jpg)
Inputs and Outputs
Example: m-file titled cstr.m
function dx=cstr (t, x) % define constants k=0.005; %mol/L-s V=10; % Reactor volume in L
Inputs are independent variable (t) and dependent variable (x=Ca)
Output is differential, dx = dCa/dt
![Page 14: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/14.jpg)
Writing Comments
Example: m-file titled cstr.m
function dx=cstr (t, x) % define constants k=0.005; %mol/L-s V=10; % Reactor volume in L
Any text written after “ % ” symbol is considered to be commented
![Page 15: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/15.jpg)
Semicolon at the end of an expression
Example: m-file titled cstr.m
function dx=cstr (t, x) % define constants k=0.005; %mol/L-s V=10; % Reactor volume in L
Semi-colon simply suppresses SCREEN printing of the expression.
![Page 16: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/16.jpg)
End of Script File: Common Syntax”
![Page 17: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/17.jpg)
Command for Integration of Differential Equation
![Page 18: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/18.jpg)
Example-1 enter the following MATLAB command
[t, x]=ode45(‘cstr’,[0 500],[2]’);
to see the transient responses, use plot function plot(t, x);
Refer to slide-3 for syntax of above command
![Page 19: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/19.jpg)
Example-2: Multi-variable Differential Equations
![Page 20: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/20.jpg)
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.
0Ao Cv ,
ACv ,
V
![Page 21: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/21.jpg)
Example 2 First-order differential equation in two-variables – V(t)
and CA(t): Equations (1) and (2) must be solved simultaneously.
AAAoA CkCC
Vv
dtdC
⋅−−⋅= )( 0
vvdtdV
o −=
(1)
(2)
![Page 22: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/22.jpg)
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;
![Page 23: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/23.jpg)
Script File: New Syntax
![Page 24: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/24.jpg)
Recognizing Multivariable System
function dx=cstr1 (t, x) % constant k=0.005; %mol/L-s vout=0.15; % L/s Ca0=10; %mol/L
The first important point to note is that x is a vector of 2 variables, x1 (=V) and x2(=Ca) Also, dx is a vector of two differential equations associated with the 2 variables
![Page 25: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/25.jpg)
Defining arrays
% define x1 and x2 V=x(1,:) Ca=x(2,:)
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
![Page 26: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/26.jpg)
Defining differential equations
% write the differential equation dx(1,:)=vin-vout; dx(2,:)=(vin/V)*(Ca0-Ca)-k*Ca;
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
![Page 27: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/27.jpg)
End of “Script File: New Syntax”
![Page 28: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/28.jpg)
Command for Integration of Differential Equation
![Page 29: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/29.jpg)
Example-2 enter the following MATLAB command [t, x]=ode45(‘cstr1’,[0 500],[10 7.5]’);
to see the transient responses, use plot function plot(t, x(:,1); plot(t, x(:,2);
Initial conditions for the two variables, i.e. V=10 L and CA=7.5 mol/L at time t=0
![Page 30: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/30.jpg)
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 7.5]',options)
Plot x1 and x2. (see command in previous slide)
![Page 31: Matlab ODE Solvers](https://reader034.vdocuments.mx/reader034/viewer/2022052223/55cf94cc550346f57ba47593/html5/thumbnails/31.jpg)
End of Matlab Tutorial