How to store "for loop" values?

1 回表示 (過去 30 日間)
thebasher
thebasher 2014 年 1 月 30 日
コメント済み: Image Analyst 2014 年 1 月 30 日
Hi, I'm running the following code:
h=11:-1:-12
for i = 1:length(h)
beta=asind(cosd(Lat)*cosd(delta)*cosd(15*h)+sind(Lat)*sind(delta))
phi_s=asind((cosd(delta)*sind(15*h))/cosd(beta));
theta=acosd(cosd(beta)*cos(phi_s-phi_c)*sind(tilt)+sind(beta)*cosd(tilt));
I_bc=DNI*cosd(theta)
I_rc=GHI*reflectance*((1-cosd(tilt))/2);
I_dc=DHI*((1+cosd(tilt))/2);
I_c(i)=I_bc+I_dc+I_rc
end
figure(1)
plot(I_c)
hold on
Basically, I want h to decrement by 1 each time. I want the loop to calculate beta, phi_s, theta and calculate I_bc. I want this value of I_bc added to I_rc and I_dc, plot that sum, and repeat the loop.
What am I doing wrong? I realize I'm supposed to index each loop somehow, but I'm not quite sure how to do so...Can you help me?
Thanks

採用された回答

Amit
Amit 2014 年 1 月 30 日
編集済み: Amit 2014 年 1 月 30 日
h=11:-1:-12
for i = 1:length(h)
beta=asind(cosd(Lat)*cosd(delta)*cosd(15*h(i))+sind(Lat)*sind(delta))
phi_s=asind((cosd(delta)*sind(15*h(i)))/cosd(beta));
theta=acosd(cosd(beta)*cos(phi_s-phi_c)*sind(tilt)+sind(beta)*cosd(tilt));
I_bc=DNI*cosd(theta)
I_rc=GHI*reflectance*((1-cosd(tilt))/2);
I_dc=DHI*((1+cosd(tilt))/2);
I_c(i)=I_bc+I_dc+I_rc
end
figure(1)
plot(I_c)
hold on
Even though you were looping for each value of h, you were using the whole h vector. I replaced h with h(i) for each loop. Now you'll get a scalar for each loop and store it.
  2 件のコメント
thebasher
thebasher 2014 年 1 月 30 日
編集済み: thebasher 2014 年 1 月 30 日
I accidentally deleted my question, whoops. So, this is what I have right now:
h=11:-1:-12
i = 1:length(h)
beta=asind(cosd(Lat).*cosd(delta).*cosd(15.*h)+sind(Lat).*sind(delta))
phi_s=asind((cosd(delta).*sind(15.*h))./cosd(beta))
theta=acosd(cosd(beta).*cos(phi_s-phi_c).*sind(tilt)+sind(beta).*cosd(tilt))
I_bc=DNI*cosd(theta)
I_rc=GHI*reflectance*((1-cosd(tilt))/2)
I_dc=DHI*((1+cosd(tilt))/2)
I_c(i)=I_bc+I_dc+I_rc
figure(1)
plot(I_c)
However, I get this error :
Error using +
Matrix dimensions must agree.
Error in Lab_1 (line 73)
I_c(i)=I_bc+I_dc+I_rc
Something is wrong with I_bc, I get 24 columns with 24 values in each column. However, when I compute I_rc and I_dc, I get 1 column with 24 values. So when I try to add them in the end, I can't, because they have different dimensions. What am I doing wrong with I_bc?
Amit
Amit 2014 年 1 月 30 日
This is the same code you posted in question. I have posted the answer with some modifications which I think will take care of the issue.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 1 月 30 日
Take a look at this syntax:
h=11:-1:-12
for i = h
disp(i);
end
A little bit out of the ordinary compared to your typical for loop, but perfectly valid MATLAB code. i takes on every value that h does. Then you'd just use i everywhere you're currently using h. That said, I usually do it the way Amit did it rather than this way.
  2 件のコメント
Amit
Amit 2014 年 1 月 30 日
Actually, I just told him the mistake in his code without modifying it too much.
I, as well you (I am pretty sure), would do this without loop. This can be easily vectorized.
Image Analyst
Image Analyst 2014 年 1 月 30 日
Correct. I was just alerting him and others to a not-so-well-known way of doing for loops.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by