Solar Coding Optimization and Unrecognized variables in messy code

1 次查看(过去 30 天)
Hello,
I'm trying to set up a code that calculates the solar irradiation absorbed from sunlight in an area over the timespan of a year. I had a previous code that was set up for only a month, so I thought I'd manipulate that same code for each month of the year. However, I've been having issues with my code not recognizing a variable. For example, I get this error even though I've set up a loop that should calculate "gamma2" before the line:
I have a gamma value set up for each month, meaning that the code only calculates the gamma value for the first month January. My workspace shows as much, with only gamma1 being present, but gamma2-12 absent. My initial thought was that perhaps the index I was using wasn't right as I'd been using the same letter to create an index associated with the respective value of the dataset associated. As such, I'd changed the index loops as shown below:
If anyone can help me figure out a way to both optimize my code and figure out why I don't get gamma2 values when running the code, I'd be very grateful. Please let me know if more information is required. I will also attach my .mfile and associated text file for ease. I am very aware my code isn't the most optimized or streamlined, so I'd be open to ways on how I can optimize the loops as well.
Thank you
% Calculate gamma
for j1 = 1:length(omega1)
if(omega1(1:1:744) < 1)
gamma1(j1) = -acosd((cosd(theta_z1(j1)).*sind(phi) - sind(deltajan(j1)))./(sind(theta_z1(j1)).*cosd(phi)));
elseif (omega1(1:1:744) > 1)
gamma1(j1) = acosd((cosd(theta_z1(j1)).*sind(phi) - sind(deltajan(j1)))./(sind(theta_z1(j1)).*cosd(phi)));
end
end
for j2 = 1:length(omega2)
if(omega2(1:1:672) < 1)
gamma2(j2) = -acosd((cosd(theta_z2(j2)).*sind(phi) - sind(deltafeb(j2)))./(sind(theta_z2(j2)).*cosd(phi)));
elseif (omega1(1:1:672) > 1)
gamma2(j2) = acosd((cosd(theta_z2(j2)).*sind(phi) - sind(deltafeb(j2)))./(sind(theta_z2(j2)).*cosd(phi)));
end
end

采纳的回答

Ameer Hamza
Ameer Hamza 2020-5-12
Basically, you are using the if condition incorrectly. The lines inside if-block gamma1=... gamma2=... never gens executed, and these arrays remain empty. Follow shows the correct way to write this using logical indexing.
This block of code
for j1 = 1:length(omega1)
if(omega1(1:1:744) < 1)
gamma1(j1) = -acosd((cosd(theta_z1(j1)).*sind(phi) - sind(deltajan(j1)))./(sind(theta_z1(j1)).*cosd(phi)));
elseif (omega1(1:1:744) > 1)
gamma1(j1) = acosd((cosd(theta_z1(j1)).*sind(phi) - sind(deltajan(j1)))./(sind(theta_z1(j1)).*cosd(phi)));
end
end
should be replaced with
mask = omega1(1:1:744) < 1;
gamma1(mask) = -acosd((cosd(theta_z1(mask)).*sind(phi) - sind(deltajan(mask)))./(sind(theta_z1(mask)).*cosd(phi)));
mask = omega1(1:1:744) > 1;
gamma1(mask) = acosd((cosd(theta_z1(mask)).*sind(phi) - sind(deltajan(mask)))./(sind(theta_z1(mask)).*cosd(phi)));
Also, note that deltajan has 31 elements, whereas theta_z1 has 744 elements. So this code will still give an error, but you need to figure out why they are not of equal length

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by