Vectorizing a Large Function with a Command
Show older comments
Hello, everyone. I am trying to run this program, however, I have run into a big problem. As you can see below, the code is used to solve a 2DOF system of differential equations for the dissociation reaction of CO2. However, the differential equations are much too large to tediously sit through and change each operation in order to allow for solving the system as an array. I am wondering, there has to be a way to automate this process. To automatically "Vectorize" a function so it can be solved with matrices. In engineering, there are countless complex equations that would take considerable effort to change each operation to suite the array solver needs. I have also been running into a strange issue where the command window gives a "Too many input arguments" error for the function of dy1, when the variables chosen for the runge kutta integration for loop are all in the function itself.
I am very very close to solving this reaction, and doing so would help me move further along in my internal combustion engine research. I am open to any suggestions, and any help would be greatly appreciated. Thanks.
P
Code:
% Chemical Equilibrium
% Solve the following chemical equation:
% aCO2 -> n1[CO2]+n2[O2]+n3[CO]+n4[O]
% Goal: We want to be able to solve the chemical reaction above for all
% products assuming chemical equilibrium
% Assume a fraction of CO2 and O2 dissociates. We will call these parameters
% "y1" and "y2"
% Write out the mass balance:
% a -> a(1-y1)[CO2]+(1/2)ay1(1-y2)[O2]+ay1[CO]+ay1[CO]+ay1y2[O]
% From this, we can derive the total number of products:
% np = a[1-y1+(1/2)y1(1-y2)+y1+y1y2]
% np = (1/2)a[2+y1(1+y2)]
% From this, we define each mole fraction
% x1 = 2(1-y1)/(2+y1(1+y2))
% x2 = y1(1-y2)/(2+y1(1+y2))
% x3 = 2y1/(2+y1(1+y2))
% x4 = 2y1y2/(2+y1(1+y2))
% From here, we can plug these values into the equilibrium constant
% equation and solve for the time derivate of y. To solve for the
% differential equation, initialization parameters xij are used.
clearvars
syms K1 dK1 K2 dK2 y1 dy1 y2 dy2 P dP
% Initialization Parameters: These mole fraction and dissociation constant
% equations are being used to solve for the time derivatives of y1 and y2
x1j = 2*(1-y1)/(2+y1*(1+y2));
x2j = y1*(1-y2)/(2+y1*(1+y2));
x3j = y1*(1-y2)/(2+y1*(1+y2));
x4j = y1*(1-y2)/(2+y1*(1+y2));
% Differential Initialization Parameters
dx1j = diff(x1j,y1)*dy1+diff(x1j,y2)*dy2;
dx2j = diff(x2j,y1)*dy1+diff(x2j,y2)*dy2;
dx3j = diff(x3j,y1)*dy1+diff(x3j,y2)*dy2;
dx4j = diff(x4j,y1)*dy1+diff(x4j,y2)*dy2;
% Dissociation Constant Initialization Parameters
x2jk = (x4j^2*P)/(K2^2);
x3jk = (x1j^2)/(x2j^2*K1^2*P);
%solving the combined equations for dy1 and dy2
eqn = [diff(x2j,y1)*dy1+diff(x2j,y2)*dy2 == diff(x2jk,y1)*dy1 + diff(x2jk,y2)*dy2 + diff(x2jk,P)*dP + diff(x2jk,K1)*dK1+diff(x2jk,K2)*dK2, diff(x3j,y1)*dy1+diff(x3j,y2)*dy2 == diff(x3jk,y1)*dy1 + diff(x3jk,y2)*dy2 + diff(x3jk,P)*dP + diff(x3jk,K1)*dK1+diff(x3jk,K2)*dK2];
sol = solve(eqn,[dy1 dy2]);
dy1 = simplify(sol.dy1,"Steps",50);
dy2 = simplify(sol.dy2,"Steps",50);
%Now that we have equations for dy1 and dy2, we can proceed with the
%numerical analysis of the system
clearvars
h = 0.01;
tf = 60;
N = tf/h;
t = linspace(0,tf,N+1);
y1 = zeros(1,N+1);
y2 = zeros(1,N+1);
y1(1) = 0;
y2(1) = 0;
dK1 = 1;
dK2 = 1;
K1 = 1;
K2 = 1;
dP = 1;
P = 1;
for n=1:N
k1y1 = h*f(y1(n),K1,K2,dK1,dK2,dP,P,y1,y2);
k2y1 = h*f(y1(n)+1/2*k1y1,K1,K2,dK1,dK2,dP,P,y1,y2);
k3y1 = h*f(y1(n)+1/2*k2y1,K1,K2,dK1,dK2,dP,P,y1,y2);
k4y1 = h*f(y1(n)+k3y1,K1,K2,dK1,dK2,dP,P,y1,y2);
y1(n+1) = y1(n)+(1/6)*(k1y1+2*(k2y1+ k3y1)+ k4y1);
end
for n=1:N
k1y2 = h*g(y2(n),K1,K2,dK1,dK2,dP,P,y1,y2);
k2y2 = h*g(y2(n)+1/2*k1y2,K1,K2,dK1,dK2,dP,P,y1,y2);
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*y1^3*y2^3 - 4*dP*K1*K2*P*y1^3*y2^2 + 20*dP*K1*K2*P*y1^3*y2 - 12*dP*K1*K2*P*y1^3 - 8*dP*K1*K2*P*y1^2*y2^2 - 16*dP*K1*K2*P*y1^2*y2 + 24*dP*K1*K2*P*y1^2 - 8*dK2*K1*P^2*y1^4*y2^3 - 8*dK2*K1*P^2*y1^4*y2^2 + 8*dK2*K1*P^2*y1^4*y2 + 8*dK2*K1*P^2*y1^4 + 8*dK2*K1*P^2*y1^3*y2^3 - 24*dK2*K1*P^2*y1^3*y2^2 - 8*dK2*K1*P^2*y1^3*y2 + 24*dK2*K1*P^2*y1^3 + 32*dK2*K1*P^2*y1^2*y2^2 - 32*dK2*K1*P^2*y1^2*y2 + 32*dK2*K1*P^2*y1*y2 - 32*dK2*K1*P^2*y1 + 8*dK1*K2^3*P*y1^4*y2^2 + 16*dK1*K2^3*P*y1^4*y2 + 8*dK1*K2^3*P*y1^4 - 16*dK1*K2^3*P*y1^3*y2^2 + 16*dK1*K2^3*P*y1^3 + 8*dK1*K2^3*P*y1^2*y2^2 - 48*dK1*K2^3*P*y1^2*y2 - 24*dK1*K2^3*P*y1^2 + 32*dK1*K2^3*P*y1*y2 - 32*dK1*K2^3*P*y1 + 32*dK1*K2^3*P + 16*dK1*K2*P^2*y1^4*y2^2 - 16*dK1*K2*P^2*y1^4 - 32*dK1*K2*P^2*y1^3*y2^2 + 32*dK1*K2*P^2*y1^3*y2 + 16*dK1*K2*P^2*y1^2*y2^2 - 64*dK1*K2*P^2*y1^2*y2 + 48*dK1*K2*P^2*y1^2 + 32*dK1*K2*P^2*y1*y2 - 32*dK1*K2*P^2*y1))/(16*K1*K2*P*y1*(y1 - 1)*(y1 + y1*y2 + 2)*(K2^2*y1 - 2*P*y1 + 2*K2^2 + 2*P*y1*y2 + K2^2*y1*y2));
end
13 Comments
@Jason Louison: "where the command window gives a "Too many input arguments" error for the function of dy1" - Do not improve a failing function, but fix the error at first. Whenever you mention an error in the forum, attach a copy of the complete message. Then the readers do not have to guess the details.
For the vectorization, replacing all * by .* and ^ by .^ should be sufficient already.
Jason Louison
on 20 Dec 2022
Torsten
on 20 Dec 2022
You can't solve for y1 and y2 separately - the differential equations are not independent from each other.
Jason Louison
on 20 Dec 2022
Edited: Jason Louison
on 20 Dec 2022
Torsten
on 21 Dec 2022
Yes, but for the two-equation case, you might have noticed that dy1 depends on y2 and dy2 depends on y1. Thus the two equations cannot be solved independently from each other.
Jason Louison
on 21 Dec 2022
Yes. Imitate the user interface of the MATLAB ode solvers.
function dy = fun(t,y,pars)
Here, y = [y(1);y(2)] , time t and aybe additional parameters pars are inputs and [dy(1);dy(2)] are outputs.
Your Runge-Kutta code should address this functional interface. Thus in your updates
k1 = h*f(y(n),dK,dP,P,K);
k2 = h*f(y(n)+1/2*k1,dK,dP,P,K);
k3 = h*f(y(n)+1/2*k2,dK,dP,P,K);
k4 = h*f(y(n)+k3,dK,dP,P,K);
f,y(:,n) and ki will all be vectors.
Jason Louison
on 21 Dec 2022
Jason Louison
on 21 Dec 2022
Jan
on 21 Dec 2022
@Jason Louison: A general hint: Whenever you mention an error in the forum, post a copy of the complete message. Then the readers do not have to guess, which line is failing.
"Is there a way to do this in one step or command?" - seriously? The Replace dialog in the editor should do this sufficiently already. Even for really large functions with tenthousands of equations this takes fractions of a second only.
Using a fixed step Runge Kutta solver of order 4 solve a chemical reaction is a very bad idea. If the equation is stiff, the final value is dominated by the discretization error and more or less random. To reduce the noise, you have to run it with a tiny step size, such that the runtime will explode and the accumulated rounding errors will be the main part of the final value, such that you should not call it "result" anymore.
If the equation is stiff, you need a specific solver like ODE15s. Otherwise a solver with step-size control is the way to go, e.g. ODE45.
Solving an equation for a set of different parameters should not be done by vectorizing the function to be integrated, but by starting the integration in a loop, in which the parameters are varied. If multiple trajectories are calculated in the same integration, the component with the highest discretization and rounding errors determine the step-size. This means, that all other components are calculated too frequently and this reduces the accuracy due to the accumulation of rounding errors. The total run-time will increase because the system is integrated with a too small step-size in average.
If you have a really good reason to integrate a system with parameters provided as vectors, the variables to be integrated are not scalars anymore:
dy(1) = (dP.*K1.^3.*K2.*P.^2.*y(1).^5.*y(2).^4 - 4.*dP.*K1.^3.*K2.*P.^2.*y(1).^5.*y(2).^3 + ...
dy(1) is a scalar, but the right hand side is a vector. Of course this cannot work. You have to blow up dy to a 2*(N+1) vector instead.
I still do not see the reason to vectorize the equations. This does not offer an advantage for integrating the system. The shown Runge-Kutta method is not sufficient to calculate the trajectories reliably in addition. This is not state of the art even in lessons for beginners, but an expensive random number generator with a poor entropy.
Use on of the built-in solvers with a step-size control and check the stiffness of the system (a cheap check: Try, if ODE45 or ODE15s is much faster).
Jason Louison
on 21 Dec 2022
Edited: Jason Louison
on 21 Dec 2022
@Jason Louison: Again: "the program kept giving me an “incorrect dimension error”." - If you mention an error, post a copy of the complete message and the relevant lines of code.
My comments are not harsh. I try to show you, how to solve the problem and how to avoid the typical errors made by beginners. Numerical maths is a demanding field of science. Feel free to ask questions about Matlab, because this is the purpose of this forum.
What is the actual problem you want to solve? You have a 2 DoF system of equations. Why do you want to solve the system "as an array"? What do you expect from vectorizing the function to be integrated?
One of your posted codes contain:
dK = zeros(1,N+1);
dP = zeros(1,N+1);
P = zeros(1,N+1);
K = zeros(1,N+1);
%This allows us to vary the above parameters as functions of time. The
%parameters are assigned constant values below
dK = 0.34;
dP = 1;
P = 1;
K = 1;
After creating dK etc. as vectors, you overwrite all of them by scalars. What is the underlying intention?
An example for a proper integration:
dK1 = 0;
dK2 = 0;
K1 = 0.5;
K2 = 0.7;
dP = 0;
P = 1;
[T, Y] = ode45(@(t,y) f(t,y, K1,K2,dK1,dK2,dP,P), [0, 60], [1, 0.5]);
plot(T, Y)
function dy = f(t,y, K1,K2,dK1,dK2,dP,P)
y1_5 = y(1)^5;
y1_4 = y(1)^4;
y1_3 = y(1)^3;
y1_2 = y(1)^2;
y2_4 = y(2)^4;
y2_3 = y(2)^3;
y2_2 = y(2)^2;
K2_3 = K2^3;
K2_2 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1_3*y2_2 + 20*dP*K1*K2*P*y1_3*y(2) - ...
12*dP*K1*K2*P*y1_3 - 8*dP*K1*K2*P*y1_2*y2_2 - ...
16*dP*K1*K2*P*y1_2*y(2) + 24*dP*K1*K2*P*y1_2 - ...
8*dK2*K1*P_2*y1_4*y2_3 - 8*dK2*K1*P_2*y1_4*y2_2 + ...
8*dK2*K1*P_2*y1_4*y(2) + 8*dK2*K1*P_2*y1_4 + ...
8*dK2*K1*P_2*y1_3*y2_3 - 24*dK2*K1*P_2*y1_3*y2_2 - ...
8*dK2*K1*P_2*y1_3*y(2) + 24*dK2*K1*P_2*y1_3 + ...
32*dK2*K1*P_2*y1_2*y2_2 - 32*dK2*K1*P_2*y1_2*y(2) + ...
32*dK2*K1*P_2*y(1)*y(2) - 32*dK2*K1*P_2*y(1) + ...
8*dK1*K2_3*P*y1_4*y2_2 + 16*dK1*K2_3*P*y1_4*y(2) + ...
8*dK1*K2_3*P*y1_4 - 16*dK1*K2_3*P*y1_3*y2_2 + ...
16*dK1*K2_3*P*y1_3 + 8*dK1*K2_3*P*y1_2*y2_2 - ...
48*dK1*K2_3*P*y1_2*y(2) - 24*dK1*K2_3*P*y1_2 + ...
32*dK1*K2_3*P*y(1)*y(2) - 32*dK1*K2_3*P*y(1) + ...
32*dK1*K2_3*P + 16*dK1*K2*P_2*y1_4*y2_2 - ...
16*dK1*K2*P_2*y1_4 - 32*dK1*K2*P_2*y1_3*y2_2 + ...
32*dK1*K2*P_2*y1_3*y(2) + 16*dK1*K2*P_2*y1_2*y2_2 - ...
64*dK1*K2*P_2*y1_2*y(2) + 48*dK1*K2*P_2*y1_2 + ...
32*dK1*K2*P_2*y(1)*y(2) - 32*dK1*K2*P_2*y(1))) / ...
(16*K1*K2*P*y(1)*(y(1) - 1)*(y(1) + y(1)*y(2) + 2)*(K2_2*y(1) - ...
2*P*y(1) + 2*K2_2 + 2*P*y(1)*y(2) + K2_2*y(1)*y(2)))];
end
The output is empty, because the calculated trajectory contains NaN values only. I assume it is divided by zero.
So a vectorization is not meaningful at all. It is not clear yet, what you want to achieve and the posted functions does not have a useful result.
Jason Louison
on 21 Dec 2022
Accepted Answer
More Answers (0)
Categories
Find more on Chemistry 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!







