How do I store Iteration Results
1 次查看(过去 30 天)
显示 更早的评论
Hi, Please I need to get values for Z, Za, V, Va at every value of T. The code is as follows:
z=[0.2 0.2 0.2 0.4];
W_PR=0.245;
C=4;
omega=[0.344 0.467 0.578 0.789];
Tc=[600 700 500 570];
Pc=[50 70 58 76];
R=8.314;
P=20;
Z_store = ones(C,C,5);
V_store = ones(5,C);
K_PR=[1.546e-2, 0.456, 1.432e2, 14.32];
iter = 0;
for k=40:10:80
T(k)=273.15+k;
for c=1:C
x_PR(1,c)=z(c)/(1+W_PR*(K_PR(c)-1));
x_PR(2,c)=K_PR(c)*x_PR(1,c);
kappa_PR=0.37464+1.54226.*omega(c)-0.26992.*omega(c).^2;
alpha_PR=(1+kappa_PR.*(1-sqrt(T(k)./Tc(c)))).^2;
a_PR(c,c)=0.45724.*R.^2.*Tc(c).^2./Pc(c).*alpha_PR;
b_PR(c)=0.07780*R.*Tc(c)./Pc(c);
end
for c=2:C
for n=1:(c-1)
a_PR(c,n)=sqrt(a_PR(c,c).*a_PR(n,n));
a_PR(n,c)=a_PR(c,n);
end
end
for c=1:C
A_PR(c,c)=a_PR(c,c).*P./(R.*T(k)).^2;
B_PR(c)=b_PR(c).*P./(R.*T(k));
Z(c,c)=A_PR(c,c)./5;
V(c)=B_PR(c).*6;
end
for c=1:C
Aa_PR(c,c)=a_PR(c,c).*P./(R.*T(k)).^3;
Ba_PR(c)=b_PR(c).*P./(R.*T(k));
Za(c,c)=A_PR(c,c)./8;
Va(c)=B_PR(c).*9;
end
end
Thanks
Isa
[EDITED, Jan, code formatted]
3 个评论
回答(3 个)
Azzi Abdelmalek
2012-9-3
z=[0.2 0.2 0.2 0.4]; W_PR=0.245; C=4;
omega=[0.344 0.467 0.578 0.789];
Tc=[600 700 500 570]; Pc=[50 70 58 76]; R=8.314; P=20;
Z_store = ones(C,C,5); V_store = ones(5,C);
K_PR=[1.546e-2, 0.456, 1.432e2, 14.32]; iter = 0;
for k=40:10:80
T(k)=273.15+k;
for c=1:C
x_PR(1,c)=z(c)/(1+W_PR*(K_PR(c)-1));
x_PR(2,c)=K_PR(c)*x_PR(1,c);
kappa_PR=0.37464+1.54226.*omega(c)-0.26992.*omega(c).^2;
alpha_PR=(1+kappa_PR.*(1-sqrt(T(k)./Tc(c)))).^2;
a_PR(c,c)=0.45724.*R.^2.*Tc(c).^2./Pc(c).*alpha_PR;
b_PR(c)=0.07780*R.*Tc(c)./Pc(c);
end
for c=2:C
for n=1:(c-1)
a_PR(c,n)=sqrt(a_PR(c,c).*a_PR(n,n));
a_PR(n,c)=a_PR(c,n);
end
end
for c=1:C
A_PR(c,c)=a_PR(c,c).*P./(R.*T(k)).^2;
B_PR(c)=b_PR(c).*P./(R.*T(k));
Z(c,c)=A_PR(c,c)./5; V(c)=B_PR(c).*6;
end
for c=1:C
Aa_PR(c,c)=a_PR(c,c).*P./(R.*T(k)).^3;
Ba_PR(c)=b_PR(c).*P./(R.*T(k));
Za(c,c)=A_PR(c,c)./8; Va(c)=B_PR(c).*9;
end
result{k,1}=Z;result{k,2}= Za; result{k,3}= V;result{k,4}= Va
end
5 个评论
Jan
2012-9-11
@Isa Isa, I still do not get the problem. Does the code you have posted calculate Z, Za, V and Va correctly? If not, why? Did you set some breakpoints in the code to inspect what's going on? What kind of help do you expect from us now?
Jan
2012-9-11
This looks strange:
for c=2:C
for n=1:(c-1)
a_PR(c,n)=sqrt(a_PR(c,c).*a_PR(n,n));
a_PR(n,c)=a_PR(c,n);
end
end
The values of a_Pr are overwritten by its own transposed. Please check if this is really doing what you want.
I do not see a chance that we can find the bugs of your program, because we cannot know what you are wanting to achieve. I suggest to use the debugger to find out, what's going on.
3 个评论
Jan
2012-9-11
Hi Isa, and what I ask is, if the posted code is correct, because it is a frequently implemented bug (FIB) to overwrite values in a matrix at an inplace-transpose.
Jan
2012-9-11
编辑:Jan
2012-9-12
Your code has this problem:
for k = 40:10:80
T(k) = 273.15+k;
...
result{k,1}=Z;result{k,2}= Za; result{k,3}= V;result{k,4}= Va
end
Now the first value written to the index k=40! Improvement:
kList = 40:10:80;
for ik = 1:length(kList)
k = kList(ik); % [EDITED, "iK" -> "ik"]
T(ik) = 273.15 + k; % Not T(k)!
...
result{ik, 1} = Z;
result{ik, 2} = Za;
result{ik, 3} = V;
result{ik, 4} = Va
end
BTW. your code would be cleaner and faster, if it is vectorized:
% Instead of: for c=1:C
x_PR = z ./ (1 + W_PR .* (K_PR.' - 1));
x_PR(2, :) = K_PR .* x_PR.';
kappa_PR = 0.37464+1.54226.*omega-0.26992.*omega.^2;
alpha_PR = (1+kappa_PR.*(1-sqrt(T(ik)./Tc))).^2;
a_PR = diag(0.45724.*R.^2.*Tc .^ 2 ./ Pc .* alpha_PR);
b_PR = 0.07780 * R .* Tc ./ Pc;
3 个评论
Jan
2012-9-12
The error message contain "iK" with an uppercase "K", while the variable is called "ik" with a lowercase "k". Is it really too complicated to fix this by your own?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!