How to use ODE function for modelling tanks in series
Show older comments
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
Accepted Answer
More Answers (1)
Jaime Diaz
on 23 May 2024
Edited: Torsten
on 23 May 2024
Dear Torsten
I have a problem usin the pdepe of Matlab...it says toomany input arguments using pdepe
m=0;
x=linspace(0,3,25);
tend=0.7;
t=linspace(0,tend,25);
sol=pdepe(m,@ecuacion1pde,@ecuacion1ic,@ecuacion1bc,x,t);
C=sol(:,:,1);
plot (x,C(end,:));
xlabel('x(m)');
ylabel('C(g/m^3)');
hold on
function [g,f,s]=ecuacion1pde(x,t,C,DcDx)
E=3;
v=10;
kd=10;
g=1;
f=E*DcDx;
s=-v*DcDx-kd*C;
end
function C0=ecuacion1ic(x)
C0=0;
end
function [pl,ql,pr,qr]=ecuacion1bc(xl,cl,xr,cr,t)
Ce=4;
pl=cl-Ce;
ql=0;
E=3;
pr=0;
qr=1/E;
end
Could you give some indication about the problem?
12 Comments
Torsten
on 23 May 2024
Works under R2024a (see above).
Jaime Diaz
on 23 May 2024
Sorry still the same error
Jaime Diaz
on 23 May 2024
I did it....Matlab 2024a...it is strange...i put the code in Matlab on line and it works
Steven Lord
on 23 May 2024
Please show us the full and exact text of any warning and/or error messages you received when you ran the code (all the text displayed in orange and/or red in the Command Window.) The exact text (no paraphrasing, no summarizing, no trimming, just a direct copy and paste) may be useful in determining what's going on and how to avoid the warning and/or error.
Jaime Diaz
on 23 May 2024
Error using pdepe
Too many input arguments.
Error in example (line 6)
sol=pdepe(m,@ecuacion1pde,@ecuacion1ic,@ecuacion1bc,x,t);
Torsten
on 23 May 2024
Maybe you created your own .m-file with the name "pdepe.m" on your MATLAB path ?
What do you get if you type
which pdepe
Jaime Diaz
on 23 May 2024
Moved: Torsten
on 23 May 2024
example
Error using pdepe
Too many input arguments.
Error in example (line 6)
sol=pdepe(m,@ecuacion1pde,@ecuacion1ic,@ecuacion1bc,x,t);
This is not an answer to my question.
What do you get if you type
which -all pdepe
? More than this path ?
Jaime Diaz
on 23 May 2024
which -all pdepe
C:\Users\Jaime Díaz\Documents\MATLAB\pdepe.m
C:\Program Files\MATLAB\R2024a\toolbox\matlab\funfun\pdepe.m % Shadowed
Jaime Diaz
on 24 May 2024
Great!! It was solved!....many thanks
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



