Reducing run time for vpasolve

16 views (last 30 days)
Samuel Dixon
Samuel Dixon on 2 Apr 2020
Commented: Ameer Hamza on 3 Apr 2020
Hello, I am a very poor coder and need to create a program that solves for the concentration of 3 different chemical species in a set of CSTR reactors either in series or parallel. The concentration depends on the residence time of the reactor. To do this I am using vpasolve to solve my set of kinetic equations, however my program takes too long to run to completetion (not sure on the exact time but I have let it run for 30+ minutes and it has yet to produce a graph. How can I reduce the time it takes to solve the set of equations? Attached is my MATLAB file and below is the text of my code:
function main
CSTRseries
CSTRparallel
end
function CSTRseries
%variables
global k1 k2 k3 k4 Cao Cro Cso
k1=.002777778; %Rate constant (mol/(L*s))
k2=.0002777778; %Rate constant (mol/(L*s))
k3=.0002777778; %Rate constant (mol/(L*s))
k4=.0002777778; %Rate constant (mol/(L*s))
Cao=5; %Initial concentration of A (mol/L)
Cro=0; %Initial concentration of R (mol/L)
Cso=0; %Initial concentration of S (mol/L)
%Solving using the numerical ode solver (numerical solution)
syms Ca Cr Cs
tau=zeros(1,3600);
C=zeros(3,3600);
for i=1:3600
tau(i)=i;
[x,y,z] = vpasolve([(Ca-Cao)==(-k1*Ca + k2*Cr)*tau(i),(Cr-Cro)==(k1*Ca + -k2*Cr + -k3*Cr + k4*Cs)*tau(i),(Cs-Cso)==(k3*Cr + -k4*Cs)*tau(i)],[Ca,Cr,Cs],[0.0000001 Inf;0.00000001 Inf;0.0000001 Inf]);
C(1,i)=x;
C(2,i)=y;
C(3,i)=z;
end
%Plot data
figure(1)
plot(tau,C)
title('CSTRs in Series')
xlabel('tau')
ylabel('Concentration')
legend('A','R','S')
end
function CSTRparallel
%variables
global k1 k2 k3 k4 Cao Cro Cso
k1=.000833333; %Rate constant (mol/(L*s))
k2=.000833333; %Rate constant (mol/(L*s))
k3=.0002777778; %Rate constant (mol/(L*s))
k4=.0002777778; %Rate constant (mol/(L*s))
Cao=5; %Initial concentration of A (mol/L)
Cro=0; %Initial concentration of R (mol/L)
Cso=0; %Initial concentration of S (mol/L)
%Solving using the numerical ode solver (numerical solution)
syms Ca Cr Cs
tau=zeros(1,3600);
C=zeros(3,3600);
for i=1:3600
[x,y,z] = vpasolve([(Ca-Cao)==(-k1*Ca + k2*Cr + -k3*Ca + k4*Cs)*tau(i),(Cr-Cro)==(k1*Ca + -k2*Cr)*tau(i),(Cs-Cso)==(k3*Ca + -k4*Cs)*tau(i)],[Ca,Cr,Cs],[0.0000001 5;0.00000001 5;0.0000001 5]);
C(1,i)=x;
C(2,i)=y;
C(3,i)=z;
end
%Plot data
figure(2)
plot(tau,C)
title('CSTRs in Parallel')
xlabel('tau')
ylabel('Concentration')
legend('A','R','S')
end
  4 Comments
Ameer Hamza
Ameer Hamza on 2 Apr 2020
Are you solving exactly same equation 3600 times?
[x,y,z] = vpasolve([(Ca-Cao)==(-k1*Ca + k2*Cr + -k3*Ca + k4*Cs)*tau(i),(Cr-Cro)==(k1*Ca + -k2*Cr)*tau(i),(Cs-Cso)==(k3*Ca + -k4*Cs)*tau(i)],[Ca,Cr,Cs],[0.0000001 5;0.00000001 5;0.0000001 5]);
tau(i) are all zeros so nothing change in each iteration.
Samuel Dixon
Samuel Dixon on 2 Apr 2020
I believe thats what my problem is, but I am unsure how to fix it. Tau should be [1,2,3...3600] not an array of all zeros. But I do not know how to prevent MATLAB from solving the entire array again every time it moves to the next iterations. Basically I want it to solve for Ca at each value of tau from 1 to 3600 and then graph those Ca values against the corresponding tau value.

Sign in to comment.

Answers (1)

Ameer Hamza
Ameer Hamza on 2 Apr 2020
You can get some speed improvement by using the following method. I pre-solved the equation in terms of tau variable, and inside the for loop, I substituted the value of tau. I changed one of the functions and ran for tau_=1:100 (100 elements). I observed a speed improvement of about 2x. You can similarly change other function following this method.
function CSTRseries
%variables
global k1 k2 k3 k4 Cao Cro Cso
k1=.002777778; %Rate constant (mol/(L*s))
k2=.0002777778; %Rate constant (mol/(L*s))
k3=.0002777778; %Rate constant (mol/(L*s))
k4=.0002777778; %Rate constant (mol/(L*s))
Cao=5; %Initial concentration of A (mol/L)
Cro=0; %Initial concentration of R (mol/L)
Cso=0; %Initial concentration of S (mol/L)
%Solving using the numerical ode solver (numerical solution)
syms Ca Cr Cs tau
sol = solve([(Ca-Cao)==(-k1*Ca + k2*Cr)*tau, (Cr-Cro)==(k1*Ca + -k2*Cr + -k3*Cr + k4*Cs)*tau,(Cs-Cso)==(k3*Cr + -k4*Cs)*tau], [Ca,Cr,Cs]);
tau_=1:100;
C=zeros(3,100);
for i=1:100
C(1,i) = subs(sol.Ca, tau, tau_(i));
C(2,i) = subs(sol.Cr, tau, tau_(i));
C(3,i) = subs(sol.Cs, tau, tau_(i));
end
%Plot data
figure(1)
plot(tau_,C)
title('CSTRs in Series')
xlabel('tau')
ylabel('Concentration')
legend('A','R','S')
end
  6 Comments
Samuel Dixon
Samuel Dixon on 3 Apr 2020
I managed to figure it out, although I'm not quite sure what I did to fix it either. Thank you for your help though!
Ameer Hamza
Ameer Hamza on 3 Apr 2020
Glad to be of help.

Sign in to comment.

Categories

Find more on Chemistry in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!