Getting multiple answers for a for loop of three iterations
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Syed Muhammad Hassan Mehdi
le 28 Oct 2017
Commenté : Syed Muhammad Hassan Mehdi
le 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 commentaires
Réponse acceptée
KL
le 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!)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!