How do you make multiple for loops into one for loop?

37 ビュー (過去 30 日間)
Fabian
Fabian 2024 年 3 月 31 日
コメント済み: Voss 2024 年 3 月 31 日
Hello,
I have a rough code that I want to fix/optimize. I have 5 for loops: all of them utilize the same given data but each loop builds off each other i.e. one loop creates a 10x1 matrix and the following loop uses those values to create a new set and so on. Each loop has a new equation which creates the values that uses the previous loop's value all to end in a final loop that uses all the found values to create a 10x10 matrix. I am struggling to get the final loop to work as well as optimizing the code to reduce the number of loops for the equations. Thank you!
clc; clear vars; clear all;
G=1367;%solar constant
phi=43.07;%lattitude of madison, wi
n=32;
delta = 23.45*sind(360*((284+n)/365));
w1=-75; w2=75; %hour angles from 7AM-5PM
w=15.* [-5 -4 -3 -2 -1 1 2 3 4 5]; %hour angles
t=[8 9 10 11 12 1 2 3 4 5]; %time 7AM-5PM
kT=[0.5 0.5 0.5 0.6 0.6 0.7 0.7 0.7 0.8 0.8]; %given values
A = ((12*3600*G)/pi); %simplified for eq
B = (1+(0.033*cosd((360*n)/365)));%simplified for eq
C = ((cosd(phi)*cosd(delta)*(sind(w2)-sind(w1))...%simplified for eq
+((pi*(w2-w1)/180)*sind(phi)*sind(delta))));%simplified for eq
Io = A*B*C; %hourly radiation on horizontal surface MJ/m^2
N=length(kT);
O=length(w);
IT=zeros(N, length(w));%populated matrix that fills, used for graphing
for j=1:O %1st loop uses changing w values
Rb(j,:)=(cosd(phi)*cosd(delta).*cosd(w(j)))+(sind(phi)*sind(delta));
%this equation is needed for final IT equation
end
for i=1:N %2nd loop needed to find I for final IT equation w/changing kT vals
I(i,:)=Io.*kT(i); %needed for following loops
end
for i=1:N %3rd loop needed to find Id for final IT equation w/changing kT vals
Id(i,:)=(.9511-(.1604*kT(i))+(4.388*kT(i)^2)-(16.638*kT(i)^3)+(12.336*kT(i)^4)).*I(i);
end
for i=1:N %4th loop needed to find Ib for final IT equation w/changing kT vals
Ib(i,:)=(1-(Id(i)/I(i)))*I(i);
end
for i=1:N %final loop uses all values created by previous loops to create a 10x10 matrix
%not exactly sure how to do the final loop, this is a rough guess
for k=1:length(w)
IT(i,k)=(Ib(i)*Rb(k))+(Id(i)*((1-cosd(beta))/2))+(I(i)*rho*((1-cosd(beta))/2));
%not sure if this is possible
end
end
  2 件のコメント
Manikanta Aditya
Manikanta Aditya 2024 年 3 月 31 日
Hey,
Looks like you are trying to calculate the values of 'Rb', 'I','Id','Ib' and 'IT' in seperate loops. You can actually calculate all these values in a single loop.
clc; clear vars; clear all;
G=1367; %solar constant
phi=43.07; %latitude of Madison, WI
n=32;
delta = 23.45*sind(360*((284+n)/365));
w1=-75; w2=75; %hour angles from 7AM-5PM
w=15.* [-5 -4 -3 -2 -1 1 2 3 4 5]; %hour angles
kT=[0.5 0.5 0.5 0.6 0.6 0.7 0.7 0.7 0.8 0.8]; %given values
A = ((12*3600*G)/pi); %simplified for eq
B = (1+(0.033*cosd((360*n)/365))); %simplified for eq
C = ((cosd(phi)*cosd(delta)*(sind(w2)-sind(w1))... %simplified for eq
+((pi*(w2-w1)/180)*sind(phi)*sind(delta))); %simplified for eq
Io = A*B*C; %hourly radiation on horizontal surface MJ/m^2
N=length(kT);
O=length(w);
IT=zeros(N, length(w)); %populated matrix that fills, used for graphing
for i=1:N
for j=1:O
Rb=(cosd(phi)*cosd(delta).*cosd(w(j)))+(sind(phi)*sind(delta));
I=Io.*kT(i);
Id=(.9511-(.1604*kT(i))+(4.388*kT(i)^2)-(16.638*kT(i)^3)+(12.336*kT(i)^4)).*I;
Ib=(1-(Id/I))*I;
IT(i,j)=(Ib*Rb)+(Id*((1-cosd(beta))/2))+(I*rho*((1-cosd(beta))/2));
end
end
Thanks.
Fabian
Fabian 2024 年 3 月 31 日
Thank you so much for the help, this solved my issue!

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

採用された回答

Voss
Voss 2024 年 3 月 31 日
編集済み: Voss 2024 年 3 月 31 日
No loops required:
Rb = cosd(phi)*cosd(delta).*cosd(w)+(sind(phi)*sind(delta)); % row vector the same size as w
I = Io.*kT; % row vector the same size as kT
Id = (0.9511-0.1604*kT+4.388*kT.^2-16.638*kT.^3+12.336*kT.^4).*I; % row vector the same size as kT and I
Ib = (1-(Id./I)).*I; % row vector the same size as kT, I, and Id
IT = Ib.'.*Rb+Id.'.*(1-cosd(beta))/2+I.'*rho*((1-cosd(beta))/2); % matrix of size numel(kT)-by-numel(w)
% because I, Id, and Ib are transposed
% in this line and Rb is not
(Assuming beta and rho are scalars - they are not defined in the question.)
  2 件のコメント
Fabian
Fabian 2024 年 3 月 31 日
Thank you for the help, this solved my issue!
Voss
Voss 2024 年 3 月 31 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOceanography and Hydrology についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by