Error using / Matrix dimensions must agree.
2 views (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
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
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!
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!