Getting multiple answers for a for loop of three iterations
2 views (last 30 days)
Show older comments
Syed Muhammad Hassan Mehdi
on 28 Oct 2017
Commented: Syed Muhammad Hassan Mehdi
on 29 Oct 2017
I have the following function being iterated and recalled in another m-file. I am getting multiple answers of 'a' and 'b' for i=1:3
function f=partb2(V)
T=573;
P=100;
R=82.057;
Tc1=304.1;
Tc2=33.09;
Tc3=513;
Pc1=72.8;
Pc2=12.8;
Pc3=79.9;
T1=[Tc1 Tc2 Tc3];
P1=[Pc1 Pc2 Pc3];
y1=0.25; y2=0.5; y3=0.25;
y=[y1 y2 y3];
for i=1:3
a=0.42748*(R^2)*(T1(i)^2)/P1(i);
disp(a)
b=0.08664*((R*T1(i))*P1(i));
end
a1=sum((a^0.5)*y);
b1=sum(b*y);
X=(R*T)/(V-b1);
Y=a1/(V*(V-b1));
f=X-Y-P;
clear clc T=573; P=100; R=82.057; V=R*T/P; disp('The value of V from part(a)=') disp(V) s1=@partb2; f1=fzero(s1,V); disp('The Value of V from part(b)=') disp(f1)
0 Comments
Accepted Answer
KL
on 28 Oct 2017
I'd guess you'd want to store the output of a and b from each iteration in that for loop. Nevertheless you could do it simply,
a = 0.42748*R^2*(T1.^2./P1.^2);
b = 0.08664*R*T1.*P1;
a1=sum((a.^0.5).*y);
b1=sum(b.*y);
now, you have 3 values for a as well as b and then calculate a1 and b1.
I haven't paid attention to the rest of your code, if this is not what you're looking for you have to be specific about what do you expect as your output.
(also, please format your entire code, it helps a lot!)
More Answers (0)
See Also
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!