MATLAB Answers

How can I fix the if statement issue in my code?

5 views (last 30 days)
Feyza Boyun
Feyza Boyun on 22 Oct 2020
Commented: Star Strider on 22 Oct 2020
Here is my code. I want P_ABP and P_ACP values to be zero if they are smaller than 0. But I still get negative values when I plot the graph. If statement is bolded.
day = 40;
angle_mu = 8;
lat = 30.43;
Ps = 1000;
elev = 59.9;
time = linspace(0,24,24);
alpha = 360/24*(time-12);
dec = 23.44sind(360(day-80)/365.25);
zen = acosd(sind(dec).*sind(lat)+cosd(dec).*cosd(lat).*cosd(alpha));
tan_azi = atand(sind(alpha)./sind(lat).*cos(alpha)-cosd(lat).*tand(dec));
azi = atand(tan_azi);
for i = 1:length(alpha)
if alpha(i) >=0 && tan_azi(i) >=0
azi(i) = 180 + azi(i);
elseif alpha(i) >=0 && tan_azi(i) <=0
azi(i) = 360 + azi(i);
elseif alpha(i) <=0 && tan_azi(i) >=0
azi(i) = azi(i);
elseif alpha(i) <=0 && tan_azi(i) <=0
azi(i) = 180 + azi(i);
end
end
A = 0.433*2;
surf_aziABP = 0;
surf_aziACP = 0;
P_ABP = 0;
P_ACP = 0;
rotation = linspace(1,8,24);
for a = 1:length(time)
surf_aziABP = 60-angle_mu + (rotation(a).*time);
surf_aziACP = 60+angle_mu + (rotation(a).*time);
for b = 1:length(time)
P_ABP = (Ps.*(cosd(elev).*cosd(zen)+sind(elev).*sind(zen).*cosd(azi- surf_aziABP(b))));
P_ACP = (Ps.*(cosd(elev).*cosd(zen)+sind(elev).*sind(zen).*cosd(azi- surf_aziACP(b))));
end
end
if P_ABP < 0
P_ABP = 0;
elseif P_ACP < 0
P_ACP = 0;
end
Power_Total = P_ABP + P_ACP;
POW = A.*Power_Total;
plot(rotation,POW)
grid on
xlabel('Angular Velocity/Hour')
ylabel('Power (W/m^2')
sumPOW = sum(POW)
trapz(time,POW)

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 22 Oct 2020
Replace the if block with:
P_ABP = max(P_ABP,0);
P_ACP = max(P_ACP,0);
With that, I got no negative values whe I ran your code.

  6 Comments

Show 3 older comments
Star Strider
Star Strider on 22 Oct 2020
As always, my pleasure!
The if block would have to cycle through each element of the vector in a loop, so it is possible to do so, however the max call (or logical indexing) would be more efficient.

Sign in to comment.

More Answers (1)

VBBV
VBBV on 22 Oct 2020
Edited: VBBV on 22 Oct 2020
So many syntax errors in your code
Use code section for better readability
Try this, it works without errors
day = 40;
angle_mu = 8;
lat = 30.43;
Ps = 1000;
elev = 59.9;
time = linspace(0,24,24);
alpha = 360/24*(time-12);
dec = 23.44*sind(360*(day-80)/365.25);
zen = acosd(sind(dec).*sind(lat)+cosd(dec).*cosd(lat).*cosd(alpha));
tan_azi = atand(sind(alpha)./sind(lat).*cos(alpha)-cosd(lat).*tand(dec));
azi = atand(tan_azi);
for i = 1:length(alpha)
if alpha(i) >=0 && tan_azi(i) >=0
azi(i) = 180 + azi(i);
elseif alpha(i) >=0 && tan_azi(i) <=0
azi(i) = 360 + azi(i);
elseif alpha(i) <=0 && tan_azi(i) >=0
azi(i) = azi(i);
elseif alpha(i) <=0 && tan_azi(i) <=0
azi(i) = 180 + azi(i);
end
end
A = 0.433*2;
surf_aziABP = 0;
surf_aziACP = 0;
P_ABP = 0;
P_ACP = 0;
rotation = linspace(1,8,24);
for a = 1:length(time)
surf_aziABP = 60-angle_mu + (rotation(a)*time(a));
surf_aziACP = 60+angle_mu + (rotation(a)*time(a));
for b = 1:length(time)
P_ABP(a,b) = (Ps*(cosd(elev)*cosd(zen(b))+sind(elev)*sind(zen(b))*cosd(azi(b)- surf_aziABP)));
P_ACP(a,b) = (Ps*(cosd(elev)*cosd(zen(b))+sind(elev)*sind(zen(b))*cosd(azi(b)- surf_aziACP)));
end
end
if P_ABP < 0
P_ABP = 0;
elseif P_ACP < 0
P_ACP = 0;
end
Power_Total = P_ABP + P_ACP;
POW = A.*Power_Total;
plot(rotation,POW)
grid on
xlabel('Angular Velocity/Hour')
ylabel('Power (W/m^2')

  1 Comment

Feyza Boyun
Feyza Boyun on 22 Oct 2020
I did not know, I will do that next time. The code works however I still get negative values, which means the if statement is not working properly.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!