Array dimensions not consistent

조회 수: 11 (최근 30일)
Najeem Afolabi
Najeem Afolabi 2020년 12월 8일
댓글: Najeem Afolabi 2020년 12월 8일
Could some one please help me spot how my array dimensions are not consistent?
function dcdt = diffcstrvalid2(t, C)
k1 = 10;
k2 = 5;
CA0 = 0.3;
CR0 = 0;
CS0 = 0;
tau = 0.3;
%Rate Equations
rA = -k1 * C(1)-k2* C(1);
rR = k1 * C(1);
rS = k2* C(1);
%differential equations
dcdt = [ (CA0-C(1))/tau -(-rA);
(CR0-C(2))/tau+rR ;
(CS0-C(3))/tau + rS];
end
initCon = [0.3, 0, 0];
% Residence times
t = [0, 0.3]; %s
%ODE SOLVER
[t2, C2] = ode45(@diffcstrvalid2, t, initCon, []);
%PLOT CSTR
figure (3)
plot(t2, C2 )
grid on
xlabel('tau [s]')
ylabel('concentration [mol/L]')
legend('c_A', 'c_R', 'c_S')
crcao2 = C2(:,2)./0.3;
cscao2 = C2(:,3)./0.3;
xa2 = 1 - C2(:,1)/0.3;
k1tau2 = 10.*t2;
figure (4)
plot( k1tau2, crcao2,'x', k1tau2, cscao2,'x', k1tau2, xa2, 'x')
grid on
xlabel('k1tau [s]')
ylabel('C/CAO, XA' )
legend('c_R', 'c_S', 'XA')
  댓글 수: 2
Rik
Rik 2020년 12월 8일
편집: Rik 2020년 12월 8일
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
This is an invalid syntax. You can put functions in a script file, but you have to put them at the bottom.
I doubt this is the actual solution, so I won't put it in an answer yet:
%a few extra parentheses solve the error
dcdt = [ (CA0-C(1))/(tau -(-rA));
(CR0-C(2))/(tau+rR) ;
(CS0-C(3))/(tau + rS)];
Also note that mlint is warning you that t isn't used in diffcstrvalid2. Is this intentional?
Najeem Afolabi
Najeem Afolabi 2020년 12월 8일
I just copied the function from the function file and added it to my code so that you could see it. I'll put the function at the bottom next time.

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2020년 12월 8일
%differential equations
dcdt = [ (CA0-C(1))/tau -(-rA);
(CR0-C(2))/tau+rR ;
(CS0-C(3))/tau + rS];
The space before the second - sign on the first line is likely the culprit. It makes MATLAB think you have two elements in the first row where the second and third rows only have one element.
If that doesn't resolve the problem set an error breakpoint and run your code. When you reach the debug prompt check the line of code on which you're stopped and the dimensions of all the variables used on that line.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by