For loop problem; could you help?

I wish to calculate w to plot for (T,w) for every thi values and T values, my goal is to plot psychrometric chart
do I need to use double for loop on this case??
eg for thi = 0,1 , T = 0:1:60 , find w ;; for thi = 0,2 , T = 0:1:60 find w2 ..... ;;; for thi = 1 , t = T = 0:1:60 find w(n)
pg(i) has been successfully computed but struggling on finding w vectors.
Could you please take a look into this?
%%% welxer formulation
g0 = -2.9912729*1000;
g1 = -6.0170128*1000;
g2 = 1.887643854*10;
g3 = -2.8354721*10^-2;
g4 = 1.7838301*10^-5;
g5 = -8.4150417*10^-10;
g6 = 4.4412543*10^-13;
g7 = 2.858487;
% T ; %%%
%%%
T = 273.15:1:273.15+60 %%% temperature
thi = 0.1 : 0.1 : 1
for i = 1:length(T)
Pg(i) = exp((g0/T(i)^2 + g1/T(i) + g2 + g3*T(i) + g4* T(i)^2 + g5 *T(i)^3 + g6*T(i)^4 ) + g7*log(T(i)))/1000;
%%%%% I would like save w vectors for for every thi = 0:0.1:1 , T = 0:1:60
w(i) = (0.622*thi*Pg(i)/101-thi*Pg(i); %%%???
end

 Accepted Answer

Stephen23
Stephen23 on 20 May 2020
Edited: Stephen23 on 22 May 2020
thi = 0.1:0.1:1;
T = 273.15:1:273.15+60; %%% temperature
N = numel(T);
w = nan(numel(thi),N); % preallocate!
Pg = nan(1,N); % preallocate!
for k = 1:N
Pg(k) = exp((g0/T(k)^2 + g1/T(k) + g2 + g3*T(k) + g4* T(k)^2 + g5 *T(k)^3 + g6*T(k)^4 ) + g7*log(T(k)))/1000;
w(:,k) = 0.622*thi*Pg(k)./101-thi*Pg(k);
end
Every column of w is one result vector for one temperature.

6 Comments

Young Lee
Young Lee on 20 May 2020
Edited: Young Lee on 20 May 2020
thank you sir, I come across a little problem that the figure were negative across all, so I made sure the equation is correct by putting brackets around them
w(k,:) = (0.622*thi*Pg(k)) / (101-thi*Pg(k))
and I now see a problem that the result now only relfect on thi = 1 , so every column vectors are identical for w. Can you see if u can fix this??
0.00704865544873522 0.00704865544873522 0.00704865544873522 0.00704865544873522 0.00704865544873522 0.00704865544873522 0.00704865544873522 0.00704865544873522 0.00704865544873522 0.00704865544873522
0.00750841790431392 0.00750841790431392 0.00750841790431392 0.00750841790431392 0.00750841790431392 0.00750841790431392 0.00750841790431392 0.00750841790431392 0.00750841790431392 0.00750841790431392
0.00799453559519796 0.00799453559519796 0.00799453559519796 0.00799453559519796 0.00799453559519796 0.00799453559519796 0.00799453559519796 0.00799453559519796 0.00799453559519796 0.00799453559519796
0.00850831460635720 0.00850831460635720 0.00850831460635720 0.00850831460635720 0.00850831460635720 0.00850831460635720 0.00850831460635720 0.00850831460635720 0.00850831460635720 0.00850831460635720
0.00905111687432828 0.00905111687432828 0.00905111687432828 0.00905111687432828 0.00905111687432828 0.00905111687432828 0.00905111687432828 0.00905111687432828 0.00905111687432828 0.00905111687432828
@Young Lee : you probably need to use array division (not matrix division):
0.622*thi*Pg(k)./101-thi*Pg(k);
% ^^ array division
Note that I edited my answer to change the orientation of w, so that you can now plot it very simply:
plot(T,w)
Young Lee
Young Lee on 21 May 2020
Edited: Young Lee on 21 May 2020
could you share the new code please? my plot is quite different to your given I copy and paste your solution
% Copied from your question:
g0 = -2.9912729*1000;
g1 = -6.0170128*1000;
g2 = 1.887643854*10;
g3 = -2.8354721*10^-2;
g4 = 1.7838301*10^-5;
g5 = -8.4150417*10^-10;
g6 = 4.4412543*10^-13;
g7 = 2.858487;
% copied from my answer:
thi = 0.1:0.1:1;
T = 273.15:1:273.15+60; %%% temperature
N = numel(T);
w = nan(numel(thi),N); % preallocate!
Pg = nan(1,N); % preallocate!
for k = 1:N
Pg(k) = exp((g0/T(k)^2 + g1/T(k) + g2 + g3*T(k) + g4* T(k)^2 + g5 *T(k)^3 + g6*T(k)^4 ) + g7*log(T(k)))/1000;
w(:,k) = 0.622*thi*Pg(k)/101-thi*Pg(k);
end
plot(T,w)
Giving:
sorry I am still having difficulty on this, if u could find the problem that would be great...
basically on this equation 0.622*thi*Pg(k)/101 - (Pg(k) *thi) the later Pg(k) is inputting wrong value for the iteration result in totally different w output
for at T = T(1,31), Pg(1,31) = 4.24520200417491
using calculator to find 0.622*thi*Pg(k)/101-(Pg(k)*thi) @ thi = 1 , should yield 0.02739 which matches with Psychrometric chart but instead on the array, w(1,31) = -4.21905828490168
I have been trying to work it out myself but just doesnt seem know why the equation is producing totally wrong answer...
so the final plot should look similar to this given ylim[ 0. 0.03] this was achieved by ploting
0.622*thi*Pg(k)/101 without having the problem code Pg(k)*thi) which was yielding totally wrong w value.
Could you see a problem?
I suspect that you need to add some parentheses to that line and use element-wise division:
w(:,k) = (0.622*thi*Pg(k)) ./ (101-thi*Pg(k));
% ^ ^ ^ ^ parentheses
% ^^ element-wise RDIVIDE
Giving:
>> (0.622*thi*Pg(31)) ./ (101-Pg(31)*thi)
ans =
0.0026254 0.0052731 0.0079433 0.010636 0.013352 0.016092 0.018855 0.021643 0.024454 0.027291
For thi=1 this gives a value of 0.027291. I have no idea how you got 0.02739.

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Asked:

on 20 May 2020

Edited:

on 22 May 2020

Community Treasure Hunt

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

Start Hunting!