MATLAB: For loop and if else statement? it is not working with the different angle. could you please suggest me some solution

for loopif statement

for i=1:size(angle)
if(angle(i) >= 1 || angle(i)<= 9)
v1(i) =v0(i)*(1-deficit1); %affected by the x away one% downstream wind for turbine 2nd






v2(i) =v1(i)*(1-deficit2); %affected by the x away one% downstream wind for turbine 2nd
v3(i) =v2(i)*(1-deficit3); %affected by the x away one% downstream wind for turbine 2nd
else
end
if(angle(i) >=10 || angle(i) <=170)
v1(i) =v0(i);
v2(i) =v0(i);
v3(i) =v0(i);
else
end
if(angle(i) >= 171 || angle(i)<= 189)
v1(i)=v0(i)*(1-deficit2); %affected by the x away one% downstream wind for turbine 2nd
v2(i)=v1(i)*(1-deficit1); %affected by the x away one% downstream wind for turbine 2nd
% v3(i)=v2(i)*(1-deficit3) %affected by the x away one% downstream wind for turbine 2nd

else
end
if(angle(i) >=190 || angle(i) <=350)
v1(i) =v0(i);
v2(i) =v0(i);
v3(i) =v0(i);
else
end
if(angle(i) >= 351 || angle(i)<= 360)
v1(i)=v0(i)*(1-deficit2); %affected by the x away one% downstream wind for turbine 2nd
v2(i)=v1(i)*(1-deficit1); %affected by the x away one% downstream wind for turbine 2nd
% v3(i)=v2(i)*(1-deficit3) %affected by the x away one% downstream wind for turbine 2nd
else
end
end

Best Answer

  • You should be using && rather than || for those tests.
    Also remember that if you are dealing with floating point numbers, that (say) 9.2 is not <= 9 and is not >= 10 and so would not match any of your conditions.
    if angle(i) < 1
    ummm, what? angle is negative or between 0 and 1
    elseif angle(i) < 10
    1 inclusive to 10 exclusive case
    elseif angle(i) < 170
    10 inclusive to 170 exclusive case
    elseif angle(i) <= 190
    170 inclusive to 190 inclusive case
    elseif angle(i) < 350
    190 exclusive to 350 exclusive case
    elseif angle(i) <= 360
    350 inclusive to 360 inclusive case
    else
    ummm, what? angle is greater than 360 or is nan
    end