How can I get a loop within a loop to return a value as a matrix?

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

 Réponse acceptée

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)
Columns 1 through 19 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 Columns 20 through 38 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 Columns 39 through 50 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464 0.4464
function V9 = mvolume9(B,C,P,T,R)
gV = ones(size(T)); % make gV an array of ones the same size as T
step = 0.0001;
for count = 1:numel(T)
% use gV(count), i.e., calculate gP for one element of gV at a time:
gP = R*T(count)*((1/gV(count))+(B/(gV(count)^2))+(C/(gV(count)^3)));
while gP > P
gV(count) = gV(count) - step;
gP = R*T(count)*((1/gV(count))+(B/(gV(count)^2))+(C/(gV(count)^3)));
end
end
V9 = gV + step; % now V9 is the same size as gV and T
end

Plus de réponses (1)

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

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by