Error using / Matrix dimensions must agree.

2 views (last 30 days)
Karina Reynolds
Karina Reynolds on 23 Jan 2016
Commented: Star Strider on 23 Jan 2016
Script is: P1(i)=((R*T1)/(V-b))-(a/(V^2));
I am using the loop function to solve for V. Everything else besides P is a given constant.
Message is: Error using / Matrix dimensions must agree. Please help!

Answers (1)

Star Strider
Star Strider on 23 Jan 2016
I don’t understand ‘using a loop function to solve for V’. Is ‘V’ a vector?
That ‘everything else besides P is a given constant’ would imply that they are scalars.
If you vectorise your ‘P1’ assignment, either the problem will disappear, or your will get a ‘Subscripted assignment dimension mismatch.’ error:
P1(i)=((R.*T1)./(V-b))-(a./(V.^2));
  4 Comments
Karina Reynolds
Karina Reynolds on 23 Jan 2016
Edited: Karina Reynolds on 23 Jan 2016
Heres the full code:
T1 = 310; %@K
T2 = 510; %@K
T3 = 810; %@K
a = 1.337;
b= 0.03200;
R=0.0821;
for i = 1:10
V(i)=1.5*i;
end
for i = 1:10
P1(i)=((R*T1)/(V(i)-b))-(a/(V(i)^2));
P2(i)=((R*T2)/(V(i)-b))-(a/(V(i)^2));
P3(i)=((R*T3)/(V(i)-b))-(a/(V(i)^2));
end
plot(P1,V,P2,V,P3,V)
xlabel('Vm (dm^3mol^-1')
ylabel('Pressure (atm)')
title('p-Vm function/KR')
legend('VDW_1','VDW_2', 'VDW_3')
Star Strider
Star Strider on 23 Jan 2016
One of MATLAB’s strengths is its ability to use vectorised code.
This works:
T1 = 310; %@K
T2 = 510; %@K
T3 = 810; %@K
a = 1.337;
b= 0.03200;
R=0.0821;
i = 1:10;
V = 1.5*i;
P1 = ((R*T1)./(V-b))-(a./(V.^2));
P2 = ((R*T2)./(V-b))-(a./(V.^2));
P3 = ((R*T3)./(V-b))-(a./(V.^2));
figure(1)
plot(P1,V,P2,V,P3,V)
xlabel('Vm (dm^3mol^-1')
ylabel('Pressure (atm)')
title('p-Vm function/KR')
legend('VDW_1','VDW_2', 'VDW_3')
My variation of your code uses the dot-operator to do element-wise calculations. See Array vs. Matrix Operations for the fascinating details!

Sign in to comment.

Categories

Find more on Programming 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!