How to use ODE function for modelling tanks in series
Hi,
I’m learning how to use functions and ODEs and am trying to recreate a tanks in series model that i made in a programme called Berkeley Madona many years ago. It’s purpose is to model a series of cascaded stirred tank reactors where the output of one is passed to the next. Hence i need to use a series of ODEs and pass the output of one to the next. The cascade of CSTRs is used to approximate a PFR.
The basic is N tanks in series of identical volume V. A reacting to B and B reacting back to A.
The overall model is based on Octave Levinspiel’s Chemcial Reaction Engineering Text, tanks in series model, and some good starting point for a single tank on youtube.
looking forward to your hints and guidance for a matlab novice…many thanks.
For the first tank….the function cstr1.m
function xa_dot = cstr1(t,xa,xa_in)
xb = 1 – xa; % a + b = 1, no other components.
F= 0.1; %flow in m3/s
V = 0.1; %tank vol m3
k1 = 0.45; % rate constant 1
k2 = 0.1; % rate constant 2
xa_dot = (F/V * (xa_in-xa)) + k1*xa*xb – k2 * xa^2;
%function to model tanks in series 1..N
% Molar Balance
%Tank 1 d/dt(Ca[1] = (F/V)*(Ca_in – Ca[1]) – Ra[1]
%Tank 2..N d/dt(Ca[N] = (F/V*(Ca[N-1]-Ca[N]) – Ra[N]
% Ca = XaC, xa + xb = 1, If C = 1 then Ca = xa (X = molar fracctional conversion)
%hence dxa/dt = (F/V * (xa[N-1] – xa[N]) – Ra[N]
% ra = sum of making a (k1*xa*xb) – destroying a by reverse reaction (k2*xa^2).
% xa_dot = d(xa)/dt.
main.m to model 1 tank….
clear all; close all; clc
xa_in = 0.3; % inital concentration of A flowing in.
xa = 1.0; % inital concentration of A in the reactor 1.
run = 5; %runtime seconds
[t1,xa1] = ode15s(@(t,x)cstr1(t,x,xa_in),[0 run],xa); % (Odefun, timespan, y0)
%xa1 represents the conversion in the first tank….
The first tank works and I can calculate xa1, I now need to find xa in the tanks 2..N, how do I do this…?
Each tank N uses the input from the previous (N-1) tank for the concentration of A and B.
Do I create a second function, or nest it in the first? and I presume I need to initalise the values of xa(2..N)
I’m not sure how to set up a function for a matrix of outputs 2..N, and in the main function how to relate the CSTR1 with CSTR2..NHi,
I’m learning how to use functions and ODEs and am trying to recreate a tanks in series model that i made in a programme called Berkeley Madona many years ago. It’s purpose is to model a series of cascaded stirred tank reactors where the output of one is passed to the next. Hence i need to use a series of ODEs and pass the output of one to the next. The cascade of CSTRs is used to approximate a PFR.
The basic is N tanks in series of identical volume V. A reacting to B and B reacting back to A.
The overall model is based on Octave Levinspiel’s Chemcial Reaction Engineering Text, tanks in series model, and some good starting point for a single tank on youtube.
looking forward to your hints and guidance for a matlab novice…many thanks.
For the first tank….the function cstr1.m
function xa_dot = cstr1(t,xa,xa_in)
xb = 1 – xa; % a + b = 1, no other components.
F= 0.1; %flow in m3/s
V = 0.1; %tank vol m3
k1 = 0.45; % rate constant 1
k2 = 0.1; % rate constant 2
xa_dot = (F/V * (xa_in-xa)) + k1*xa*xb – k2 * xa^2;
%function to model tanks in series 1..N
% Molar Balance
%Tank 1 d/dt(Ca[1] = (F/V)*(Ca_in – Ca[1]) – Ra[1]
%Tank 2..N d/dt(Ca[N] = (F/V*(Ca[N-1]-Ca[N]) – Ra[N]
% Ca = XaC, xa + xb = 1, If C = 1 then Ca = xa (X = molar fracctional conversion)
%hence dxa/dt = (F/V * (xa[N-1] – xa[N]) – Ra[N]
% ra = sum of making a (k1*xa*xb) – destroying a by reverse reaction (k2*xa^2).
% xa_dot = d(xa)/dt.
main.m to model 1 tank….
clear all; close all; clc
xa_in = 0.3; % inital concentration of A flowing in.
xa = 1.0; % inital concentration of A in the reactor 1.
run = 5; %runtime seconds
[t1,xa1] = ode15s(@(t,x)cstr1(t,x,xa_in),[0 run],xa); % (Odefun, timespan, y0)
%xa1 represents the conversion in the first tank….
The first tank works and I can calculate xa1, I now need to find xa in the tanks 2..N, how do I do this…?
Each tank N uses the input from the previous (N-1) tank for the concentration of A and B.
Do I create a second function, or nest it in the first? and I presume I need to initalise the values of xa(2..N)
I’m not sure how to set up a function for a matrix of outputs 2..N, and in the main function how to relate the CSTR1 with CSTR2..N Hi,
I’m learning how to use functions and ODEs and am trying to recreate a tanks in series model that i made in a programme called Berkeley Madona many years ago. It’s purpose is to model a series of cascaded stirred tank reactors where the output of one is passed to the next. Hence i need to use a series of ODEs and pass the output of one to the next. The cascade of CSTRs is used to approximate a PFR.
The basic is N tanks in series of identical volume V. A reacting to B and B reacting back to A.
The overall model is based on Octave Levinspiel’s Chemcial Reaction Engineering Text, tanks in series model, and some good starting point for a single tank on youtube.
looking forward to your hints and guidance for a matlab novice…many thanks.
For the first tank….the function cstr1.m
function xa_dot = cstr1(t,xa,xa_in)
xb = 1 – xa; % a + b = 1, no other components.
F= 0.1; %flow in m3/s
V = 0.1; %tank vol m3
k1 = 0.45; % rate constant 1
k2 = 0.1; % rate constant 2
xa_dot = (F/V * (xa_in-xa)) + k1*xa*xb – k2 * xa^2;
%function to model tanks in series 1..N
% Molar Balance
%Tank 1 d/dt(Ca[1] = (F/V)*(Ca_in – Ca[1]) – Ra[1]
%Tank 2..N d/dt(Ca[N] = (F/V*(Ca[N-1]-Ca[N]) – Ra[N]
% Ca = XaC, xa + xb = 1, If C = 1 then Ca = xa (X = molar fracctional conversion)
%hence dxa/dt = (F/V * (xa[N-1] – xa[N]) – Ra[N]
% ra = sum of making a (k1*xa*xb) – destroying a by reverse reaction (k2*xa^2).
% xa_dot = d(xa)/dt.
main.m to model 1 tank….
clear all; close all; clc
xa_in = 0.3; % inital concentration of A flowing in.
xa = 1.0; % inital concentration of A in the reactor 1.
run = 5; %runtime seconds
[t1,xa1] = ode15s(@(t,x)cstr1(t,x,xa_in),[0 run],xa); % (Odefun, timespan, y0)
%xa1 represents the conversion in the first tank….
The first tank works and I can calculate xa1, I now need to find xa in the tanks 2..N, how do I do this…?
Each tank N uses the input from the previous (N-1) tank for the concentration of A and B.
Do I create a second function, or nest it in the first? and I presume I need to initalise the values of xa(2..N)
I’m not sure how to set up a function for a matrix of outputs 2..N, and in the main function how to relate the CSTR1 with CSTR2..N matlab, ode, reaction engineering, functions MATLAB Answers — New Questions