MATLAB: Array dimensions not consistent

array dimensionsMATLAB

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')

Best Answer

%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.