How can I get a loop within a loop to return a value as a matrix?
Show older comments
I only started matlab about a month ago. I wrote a function that contains a while loop inside a for loop to return a vector however it only outputs a single value. How can I get my output to be given as a matrix? The value for V9 should be a 1x50 matrix.
B = -0.388;
C = -0.026;
P = 1.2;
T = 305:5:550;
R = 8.314;
V9 = mvolume9(B,C,P,T,R);
disp(V9)
function V9 = mvolume9(B,C,P,T,R)
gV = 1;
gP = R*T*((1/gV)+(B/(gV^2))+(C/(gV^3)));
step = 0.0001;
for count = 1:length(T)
gP(count) = R*T(count)*((1/gV)+(B/(gV^2))+(C/(gV^3)));
while gP > P
gV(count) = gV(count) - step;
gP = R*T(count)*((1/gV)+(B/(gV^2))+(C/(gV^3)));
end
end
V9 = gV + step;
end
Accepted Answer
More Answers (1)
Torsten
on 10 Nov 2023
Maybe you mean
B = -0.388;
C = -0.026;
P = 1.2;
T = 305:5:550;
R = 8.314;
V9 = mvolume9(B,C,P,T,R);
disp(V9)
function V9 = mvolume9(B,C,P,T,R)
gV = ones(size(T));
gP = R*T.*((1./gV)+(B./(gV.^2))+(C./(gV.^3)));
step = 0.0001;
for count = 1:length(T)
while gP(count) > P
gV(count) = gV(count) - step;
gP(count) = R*T(count)*((1/gV(count))+(B/(gV(count)^2))+(C/(gV(count)^3)));
end
end
V9 = gV + step;
end
Categories
Find more on Loops and Conditional Statements 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!