Array dimensions not consistent

8 views (last 30 days)
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 Comments
Rik
Rik on 8 Dec 2020
Edited: Rik on 8 Dec 2020
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 on 8 Dec 2020
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.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 8 Dec 2020
%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.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!