Using ODE45 for chemical reactions: Error using VERTCAT
이전 댓글 표시
I have a system of ODEs for a system of chemical reactions and I can't seem to find the route of this error: Error using VERTCAT. Simply can't work out why the dimensions are not constant.
Here is the code that defines the reactions:
function dcdt = reactions(t,c)
global k1 k2 k3 k4 k5 k6 k7
% REACTIONS
% 1. [A] + hv -k1-> [A] + [B]
% 2. [B] + [C] -k2-> [D]
% 3. [D] + hv -k3-> [C] + [E]
% 4. [C] + [E] -k4-> [D]
% 5. [B] + [B] -k5-> [B]
% 6. [B] + [D] -k6-> [F]
% 7. [B] + [D] -k7-> [G]
% ODE
% d[A]/dt = -k1[A][hv] + k1[A][hv]
% d[B]/dt = k1[A][hv] - k2[B][C] - k5[B][B] - k5[B][B] + k5[B][B] - k6[B][D] - k7[B][D]
% d[C]/dt = -k2[B][C] + k3[D][hv] - k4[C][E]
% d[D]/dt = k2[B][C] - k3[D][hv] + k4[C][E] - k6[B][D] - k7[B][D]
% d[E]/dt = k3[D][hv] - k4[C][E]
% d[F]/dt = k6[B][D]
% d[G]/dt = k7[B][D]
dcdt = [-k1*c(1)*1 + k1*c(1); ...
k1*c(1)*1 - k2*c(2)*c(3) - k5*c(2)*c(2) - k5*c(2)*c(2) + k5*c(2)*c(2) - k6*c(2)*c(4) -k7*c(2)*c(4); ...
-k2*c(2)*c(3) + k3*c(4)*1 - k4*c(3)*c(5); ...
k2*c(2)*c(3) -k3*c(4)*1 + k4*c(3)*c(5) - k6*c(2)*c(4) - k7*c(2)*c(4); ...
k3*c(4)*1 - k4*c(3)*c(5); ...
k6*c(2)*c(4); ...
k7*c(2)*c(4)];
The main file reads:
% globals
global k1 k2 k3 k4 k5 k6 k7;
% Set rates
k1 = 1;
k2 = 2;
k3 = 3;
k4 = 4;
k5 = 5;
k6 = 6;
k7 = 7;
% Set duration
tspan = [0 100];
% Initial concentrations
c0 = [1 2 3 4 5 6 7];
% define method
[t,c] = ode45('grs', tspan, c0);
Most probably something silly but it has me stumped. I know the ODE can be slightly simplified but I'm not sure why this would cause the problem. Does anyone have any insight? Or a link to a good tutorial that will put me back on the right path?
Thanks in advance for any help you can provide, Dan
답변 (1개)
Walter Roberson
2011년 7월 15일
Change your line
k1*c(1)*1 - k2*c(2)*c(3) - k5*c(2)*c(2) - k5*c(2)*c(2) + k5*c(2)*c(2) - k6*c(2)*c(4) -k7*c(2)*c(4)
to
k1*c(1)*1 - k2*c(2)*c(3) - k5*c(2)*c(2) - k5*c(2)*c(2) + k5*c(2)*c(2) - k6*c(2)*c(4) - k7*c(2)*c(4)
Notice the extra space after the last negative sign. With the spacing you had, the parser was interpreting the line as designating a row with two values, the first one being (k1*c(1)*1 - k2*c(2)*c(3) - k5*c(2)*c(2) - k5*c(2)*c(2) + k5*c(2)*c(2) - k6*c(2)*c(4)) and the second one being (-k7*c(2)*c(4))
Unary minus binds more tightly than subtraction.
댓글 수: 3
Dan Pearce
2011년 7월 15일
Walter Roberson
2011년 7월 15일
4th line, second term, "-k3*c(4)*1" with no space after the "-"
Dan Pearce
2011년 7월 15일
카테고리
도움말 센터 및 File Exchange에서 Chemistry에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!