You are on page 1of 12

Illustrative Example for ChE 471

These examples will show you how to use fsolve, ode45, ode15s, ode15s for DAE, bvp4c.
Example 1
Using Fsolve function
Nonlinear equation from Lecture 9 from course website is solved. You can find this equation on page
no.22 of this chapter.

314.5 817.8
14.9 14.9 9 21
11.043 (10 (1 ) 6.8 10 ) 0
A A
x x
A A A
x e x e x

+ +
=
Script file:
clc
clear all
x = 0.7 % initial guess
x=fsolve(@trialfsol,x) % calling fsolve to solve equation

Function:
function [f] = trialfsol(x)
% Nonlinear Equation from chapter 9 of class notes Page number 22
f(1)=11.043*(10^9*(1-x)*exp(-1*314.5/(14.9+x))-6.8*10^21*x*exp(-
817.8/(14.9+x)))-x;

end

Output:
x =
0.8798
Fsolve can be used to solve any number of nonlinear or linear equation provided number of equations and
numbers of unknowns are same.






Example 2
Solving Initial Value Problem:
Example number 3 from homework 3
Consider an isothermal, semi-batch reactor for the reaction in the liquid phase of stoichiometry
A + B = P. The reaction mixture can be considered to be of constant density. Initially we fill the volume
V
0
(L) with reactant A only to concentration C
Ai
(mole/ L). Then from time t=0 on we pump in a stream
containing pure reactant B at concentration C
B0
(mole/L) at a flow rate Q
B
(L/min).

Identify a computer program and present the listing of the code. Then compute and plot concentration
profiles for A, B, P as a function of time from t=0 to t= 500 minutes for the following data.
Reaction rate is second order: (-R
A
) = kC
A
C
B
(mole /L min)
Rate constant: k= 2.2 (L/ mole min).
Initial conditions: V
0
=5 (L), C
Ai
= 0.05 (mole/L), C
Bi
=C
Pi
=0
Stream of pure B is pumped in at Q
B
=0.05 (L/min) and has concentration C
B0
=0.025 (mole/L).
What are the concentrations o A, B, C at the end of the run at t=500 min?
What the conversions of A and B at the end of the run at t=500 n min?
You obtain following set of differential equations after setting up mass balance:
0
0 0
0
0 0
( )
A A
A B
B B B
A B
C C
A B
v C dC
kC C
dt V
v C C dC
kC C
dt V
dC v C
kC C
dt V
V V v t
=

= +
=
= +

These equations are solved with given initial conditions
v0=0.05
V0=5
K=2.2
Cb0=0
Ca0=0.05





Solving System of ODE
Script file
clc
clear all
figure(1); hold on;
tspan = [0:500]; % Defining time interval for solution
y0 = [0.05,0.0,0]; % initial condition
[t,y] = ode45(@sysode, tspan, y0) % calling function to solve ode
plot(t,y(:,1),'b') % Plotting concentration profile for first
component y1 (Ca)
plot(t,y(:,2),'r-') % Plotting concentration profile for
second component y2 (Cb)
plot(t,y(:,3),'c*') % Plotting concentration profile for first
component y3 (Cc)
title('Concentration Time Profile of Reactants');
legend('Ca','Cb','Cp');
xlabel('Time');
ylabel('Concentration');

Function
function yp = sysode(t,y)
yp = [-2.2*y(1)*y(2)-((0.05*y(1))/(5+0.05*t));
-2.2*y(1)*y(2)+0.05*(0.025-y(2))/(5+0.05*t);
2.2*y(1)*y(2)-0.05*y(3)/(5+0.05*t)];

Output

0 50 100 150 200 250 300 350 400 450 500
0
0.005
0.01
0.015
0.02
0.025
0.03
0.035
0.04
0.045
0.05
Concentration Time Profile of Reactants
Time
C
o
n
c
e
n
t
r
a
t
i
o
n


Ca
Cb
Cp
Example 3
Solving System of stiff differential equations:
Following system of stiff differential equations with initials conditions
0.75
dB k B S
dt K S
dS k B S
dt K S

=
+

=
+

Parameters
K=0.000001
k=0.3
Initial conditions, at t=0
B=0.05
S=5

Script File
clear all
clc
tspan = [0:15];
yo = [0.05 5];
options = odeset('RelTol',1e-10);
[t,y] = ode15s(@stiffode,tspan,yo,options);
plot(t,y(:,1),'-o');
title('Concentration Profile of Product');
xlabel('time');
ylabel('Concentration of species B');
figure(2)
title('Concentration Profile of Reactants');
plot(t,y(:,2),'-*')
xlabel('time');
ylabel('Concentration of species s');

Function file
function yp = stiffode (t, y, mu)
yp = [0.3*y(1)*y(2)/(0.000001+y(2)); -0.75*y(1)*0.3*y(2)/(0.000001+y(2))];




Output:


0 5 10 15
1.5
2
2.5
3
3.5
4
4.5
5
time
C
o
n
c
e
n
t
r
a
t
i
o
n

o
f

s
p
e
c
i
e
s

s
0 5 10 15
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
Concentration Profile of Product
time
C
o
n
c
e
n
t
r
a
t
i
o
n

o
f

s
p
e
c
i
e
s

B
Example 4
Boundary value problem:
Consider convective diffusion problem
2
2
0
d c dc
Pe
dx dx
=

Boundary conditions
C=1 at x=0
C=0 at x=1
Parameter
Pe=1

Script file
xlow=0; % lower limit
xhigh=1; % upper limit
solinit = bvpinit(linspace(xlow,xhigh,10),[0,0]); % generation of equally
space points
sol = bvp4c(@bvpode,@bvpbc,solinit); % Calling bvp4c function
xint=linspace(xlow,xhigh); %
Sxint=deval(sol,xint);
plot(xint,Sxint(1,:))
title('Concentration Time Profile for convextive diffusion equation');
xlabel('dimensionless length');
ylabel('dimensionless concentration');

Functions
Both Should be written separately
function res = bvpbc(ya,yb)
res = [ya(1)-1 yb(1)]; % function for boundary condition

function dydx = bvpode(x,y)
dydx =[y(2) y(2)]; % function for input of equation





Output:














0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Concentration Time Profile for convextive diffusion equation
dimensionless length
d
i
m
e
n
s
i
o
n
l
e
s
s

c
o
n
c
e
n
t
r
a
t
i
o
n
Solving explicit differential algebraic system of equations:
This is solved example from Essentials of chemical reaction engineering page number 564.
Parallel Reaction in PFR with heat effects:
Following reactions occur in PFR
1
2
1 1
2
2 2
2
k
A A A
k
A A A
A B r k C
A B r k C
=
=

Pure A is fed at a rate of 100 mol/s, a temperature of 150
0
C, and concentration of 0.1 mol/dm
3
.
Determine temperature and flow rate profiles down the reactor.
Data:
1
2
3
1
1
1
1
20000 / 1
60000 / 1
90 / .
90 / .
180 / .
4000 / . .
100
1 1
10exp 4000
300
1 1
0.09exp 9000
300
Rx A
Rx A
PA
PB
PC
a
a
A
A
H J mol of Ainreaction
H J mol of Ainreaction
C J mol C
C J mol C
C J mol C
U J m s C
T C
k s
T
k s
T

A =
A =
=
=
=
=
=
( | |
=
| (
\ .
( | |
=
| (
\ .

Solution:
Mass balance gives us:
2
1 2
1
2
2
(1)
(2)
0.5 (3)
A
A A A A
B
A A
C
A A
dF
k C k C
dV
dF
k C
dV
dF
k C
dV
=
=
=

From PFR energy balance we get;
1 1 2 2
( ) ( ) ( ) ( ) ( )
(4)
a A Rx A A Rx A
A PA B PB C PC
Ua T T r H r H dT
dV F C F C F C
+ A + A
=
+ +


From Stoichiometry
0
0
0
0
0
0
(5)
A
A T
T
B
B T
T
C
C T
T
T A B C
T F
C C
F T
T F
C C
F T
F T
C C
F T
F F F F
| |
| |
=
| |
\ .
\ .
| |
| |
=
| |
\ .
\ .
| |
| |
=
| |
\ .
\ .
= + +

Temperature Dependence of rate equation

1
1
1
1
1 1
10exp 4000 (6)
300
1 1
0.09exp 9000 (7)
300
A
A
k s
T
k s
T

( | |
=
| (
\ .
( | |
=
| (
\ .

Substituting given initial conditions these 7 equations can be solved in matlab using ode15s
Script File
clc
clear all

vspan = [0:1]; % Defining volume range for reactor
y0 = [100,0,0,423,100,482.8247014959528,553.0556595660398]; % initial
condition remember that these conditions should be consistant, this is very
important for this program to work ..

M=diag([1,1,1,1,0,0,0]'); %%%mass matrix which is singular in case of DAE
this matrix tell matlab that this is symtem is DAE diagonal elements for
fifth to seventh row are zero,
%they are nonzero and vrty small number is case of
stiff equations

options=odeset('Mass',M);
[v,y]=ode15s(@sysode,[0,1],y0,options); % calling ode15s for solution

plot(v,y(:,1),'b'); hold on; % Plotting concentration profile for first
component y1 Fa
plot(v,y(:,2),'r-') % Plotting concentration profile for
second component y2 Fb
plot(v,y(:,3),'c*') % Plotting concentration profile for first
component y3 Fc
title('Concentration Time Profile of Reactants');
legend('Fa','Fb','Fc');
xlabel('Volume');
ylabel('Flowrates');
figure(2)
plot(v,y(:,4))
xlabel('Volume');
ylabel('Temperature K');

Write this as a separate function
function yp = sysode(v,y) %this function is used to input system of equations
to ode15s
t0 = 423; % initial Temperature
Ct0 = 0.1; % Initial concentration

yp = [-y(6)*Ct0*(y(1)/y(5))*t0/y(4)-y(7)*(Ct0*(y(1)/y(5))*t0/y(4))^2; %
Equation for flowrate Fa
y(6)*Ct0*(y(1)/y(5))*t0/y(4); % Equation for flowrate Fb
0.5*y(7)*(Ct0*(y(1)/y(5))*t0/y(4))^2; % Equation for flowrate Fc
(4000*(373-y(4))+
y(6)*(Ct0*(y(1)/y(5))*t0/y(4))*20000+60000*y(7)*(Ct0*(y(1)/y(5))*t0/y(4))^2
)/(90*y(1)+90*y(2)+180*y(3)); % Equation for temprature change
% Next three equations are algebric equation, they need to be is residue
form i.e
-y(5)+y(1)+y(2)+y(3); % Equation for total flowrate
-y(6)+10*exp(4000*(1/300-1/y(4))); % Equation for rate constant of first
reaction
-y(7)+0.09*exp(9000*(1/300-1/y(4)))]; % Equation for rate constant of
second reaction

Output:



0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0
10
20
30
40
50
60
70
80
90
100
Concentration Time Profile of Reactants
Volume
F
l
o
w
r
a
t
e
s


Fa
Fb
Fc
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
400
450
500
550
600
650
700
750
800
850
Volume
T
e
m
p
e
r
a
t
u
r
e

K

You might also like