Vectorizing a Large Function with a Command
13 views (last 30 days)
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,d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*dP*K1*K2*P*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
Jan
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 = K2^2;
P_2 = P^2;
dy = [(dP*K1^3*K2*P_2*y1_5*y2_4 - 4*dP*K1^3*K2*P_2*y1_5*y2_3 + ...
6*dP*K1^3*K2*P_2*y1_5*y2_2 - 4*dP*K1^3*K2*P_2*y1_5*y(2) + ...
dP*K1^3*K2*P_2*y1_5 + dP*K1^3*K2*P_2*y1_4*y2_4 - ...
4*dP*K1^3*K2*P_2*y1_4*y2_3 + 6*dP*K1^3*K2*P_2*y1_4*y2_2 - ...
4*dP*K1^3*K2*P_2*y1_4*y(2) + dP*K1^3*K2*P_2*y1_4 - ...
2*dK2*K1^3*P^3*y1_5*y2_4 + 8*dK2*K1^3*P^3*y1_5*y2_3 - ...
12*dK2*K1^3*P^3*y1_5*y2_2 + 8*dK2*K1^3*P^3*y1_5*y(2) - ...
2*dK2*K1^3*P^3*y1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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.
Accepted Answer
Torsten
on 21 Dec 2022
Edited: Torsten
on 21 Dec 2022
See how you can use the below code for your purpose.
The Runge-Kutta code is made to deal with systems of differential equations of arbitrary size (thus with vector inputs).
tstart = 0.0;
tend = 1.0;
nt = 100;
h = (tend - tstart)/nt;
T = (tstart:h:tend).';
Y0 = [1 -1];
B = 4;
Y = runge_kutta_RK4(@(t,y)f(t,y,B),T,Y0);
plot(T,Y)
function Y = runge_kutta_RK4(f,T,Y0)
N = numel(T);
n = numel(Y0);
Y = zeros(N,n);
Y(1,:) = Y0;
for i = 2:N
t = T(i-1);
y = Y(i-1,:);
h = T(i) - T(i-1);
k0 = f(t,y);
k1 = f(t+0.5*h,y+k0*0.5*h);
k2 = f(t+0.5*h,y+k1*0.5*h);
k3 = f(t+h,y+k2*h);
Y(i,:) = y + h/6*(k0+2*k1+2*k2+k3);
end
end
function dy = f(t,y,B)
dy = zeros(1,2);
dy(1) = y(2);
dy(2) = -exp(-B*t)-y(1)+5*exp(-2*t)-2*exp(-(B+2)*t)+exp(-B*t)+t;
end
13 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







