Error Using * (inner matrix dimensions must agree)

4 views (last 30 days)
I need to multiply 2 vectors x(:,1) and x(:,4) that are the solutions of an ode45 I previously used, but I get this error. I understand the reason but I can't find a solution to make it work... Below is my script:
clear
clc
close all
%%%dedomena
AU=15000; %kJ/(hm^2)
k0=5E+10; %1/h
ER=9020; %K
dh1=-92.820; %%%kJ/kmol
dh2=-424.2;
dh3=-241.0;
cp1=125; %%%kJ/(kmol*K)
cp2=250;
cp3=75;
d1=14.384; %%%kJ/m^3
d2=13.668;
d3=55.382;
Tref=298; %K
options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5 1e-5]);
[t,x]=ode45(@rigid1,[0 120],[10 0 20 333],options);
subplot(3,2,1)
plot(t,x(:,1),'-',t,x(:,2),'-.',t,x(:,3),'.')
title('Molarities')
V=x(:,1)/d1+x(:,2)/d2+x(:,3)/d3;
subplot(3,2,2)
plot(V,'g-')
title('Volume')
CA=x(:,1)/V(:,1);
subplot(3,2,3)
plot(CA(:,1),'b.')
title('C_A')
r=(k0)*exp(-ER/x(:,4))*x(:,1);
subplot(3,2,5)
plot(r,'b-')
title('r')
subplot(3,2,6)
plot(t,x(:,4),'r-.')
title('Temperature')
H=x(:,1)*(dh1+cp1*(x(:,4)-Tref))+x(:,2)*(dh2+cp2*(x(:,4)-Tref))+x(:,3)*(dh3+cp3*(x(:,4)-Tref));
subplot(2,2,4)
plot(H,'r-')
title('Enthalpy H')
And the function w/ the DEs:
function dx=rigid(t,x)
k0= 5e+10;
ER=9020;
dh1=-92.820; %%%kJ/kmol
dh2=-424.2;
dh3=-241.0;
cp1=125; %%%kJ/(kmol*K)
cp2=250;
cp3=75;
Tref=298; %K
dx=zeros(4,1);
dx(1)=-k0*exp(-ER/x(4))*x(1);
dx(2)=k0*exp(-ER/x(4))*x(1);
dx(3)=-k0*exp(-ER/x(4))*x(1);
dx(4)=k0*exp(-ER/x(4))*x(1)*(-dh1+dh2-dh3+cp1-cp2+cp3+x(4)*(cp1+cp2+cp3))/(-cp1*x(1)-cp2*x(2)-cp3*x(3));
The error shows in the main script in H. r doesn't seem to give me a result either though...
Any suggestions are welcome. Thanks :)

Accepted Answer

Star Strider
Star Strider on 25 Oct 2015
I’m not certain exactly what you’re doing, but you probably need to be using element-wise ‘dot’ operators. For your vector multiplication:
xp = x(:,1) .* x(:,4);
and for element-wise division here:
CA=x(:,1)./V(:,1);
See the documentation for Array vs. Matrix Operations for all the goodies.
  2 Comments
Peter P
Peter P on 25 Oct 2015
Hey thanks a lot! That actually made it work :)
Here are the changes I made
H=x(:,1).*(dh1+cp1.*(x(:,4)-Tref))+x(:,2).*(dh2+cp2.*(x(:,4)-Tref))+x(:,3).*(dh3+cp3.*(x(:,4)-Tref));
subplot(3,2,4)
plot(H,'r-')
title('Enthalpy H')
r=(k0)*exp(-ER./x(:,4)).*x(:,1);
subplot(3,2,5)
plot(r,'b-')
title('r')
Also FYI these are equations from energy and mass balance in a reactor.
Star Strider
Star Strider on 25 Oct 2015
My pleasure!
Thank you for the context. I recognised them as chemical kinetics equations, but not beyond that (undergraduate Chemistry major, but no ChemE experience).

Sign in to comment.

More Answers (0)

Categories

Find more on Chemistry in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!