If statement in loop not recognized
3 views (last 30 days)
Show older comments
Hi
The following code:
clear all;
close all;
clc;
W = 16; %Collected weight in
g = 9.81; %gravity
rho = 1.225; %density of air
T_per_rotor = W/2 * g; %Thurst
N_b = 2; %Number of blades
C_d0 = 0.05; %Drag coeficcient of air foil of blade
C_l0 = 0.05; %Lift coeficcient of air foil of blade
SolOne = 10000;
kMax = 200;
PlotR = zeros(1,kMax);
PlotOmega = zeros(1,kMax);
PlotPower = zeros(1,kMax);
for k = 1 : kMax
j = k + 121;
for n = 0.15 : 0.001 : 1
A = pi * n ^2;
c = n/10;
P_0 = 1/8 * rho * N_b * (j)^3 * c * C_d0*(n)^4;
P_hover = T_per_rotor * sqrt(T_per_rotor/(2*rho*A));
Sol = P_0 + P_hover;
Lift = (1/2)*(1/3)*rho*N_b*c*j^2*n^3*C_l0;
if T_per_rotor - 5 < Lift < 5 + T_per_rotor
if Sol < SolOne
SolR = n %missing ; to see n
SolOmega = j %missing ; to see what j is
k %missing ; to see k **
SolPower = Sol/0.8;
SolOne = Sol;
PlotR(k) = SolR;
PlotOmega(k) = SolOmega;
PlotPower(k) = SolPower;
end
end
end
end
The code runs and outputs
k = 200
n = 1 % the two for loops run till the upper limit.
SolR = 0.5120
SolOmega = 122
k = 1 % inside the if statement
With the above results, one would think that the only time the if statements are valid, would be when k = 1. However if you change the limit for k to 20:kMax, SolR and SolOmega changes as well as k in the if statement, meaning that other values of j and n also are valid in both if statements. Question is: why is SolR and SolOmega stopping at 0.5120 and 122 and not at higher numbers?
I'm using the online matlab compiler:
Cheers
/Erik
1 Comment
ananya gupta
on 21 Apr 2020
Hi, Thanks to sharing code because i also faced issue while using codenut now I think I can resolve this. I also learn this from CETPA INFOTECH.
Answers (1)
Stephen23
on 21 Apr 2020
Edited: Stephen23
on 21 Apr 2020
The problem is likely to be this invented syntax:
T_per_rotor - 5 < Lift < 5 + T_per_rotor
MATLAB does not have ternary logical comparisons like X<Y<Z. The MATLAB documentation shows that all of the logical comparisons are binary functions like X<Y. So you will need to do join them together, e.g.:
(T_per_rotor-5)<Lift && Lift<(5+T_per_rotor)
2 Comments
Stephen23
on 21 Apr 2020
"However now the 1st if-statement is not entered even though Sol is smaller than SolOne"
That is the 3rd if-statement, not the 1st. Before it gets to the 3rd if-statement, it has to pass the 1st and 2nd. At the start of the code Sol might be less than SolOne, but the 1st if condition is not met:
>> T_per_rotor-2
ans = 76.480
>> Lift
Lift = 0.020549
It is not until many iterations later
>> k
k = 73
>> n
n = 0.99900
that the condition for the 1st if-statement is first met:
>> 2+T_per_rotor
ans = 80.480
>> Lift
Lift = 76.533
and luckily also the condition for the 2nd if. But not the 3rd one:
>> Sol
Sol = 11375.30525
>> SolOne
SolOne = 10000
Whether you like it or not, there is never an iteration where the conditions for all three if-statements are true. Perhaps there is a bug in the code, perhaps the algorithm has not been implemented correctly, perhaps the code is correct and those particular data simply do not produce those outputs, I have no idea. But for some reason you keep ignoring the conditions for the 1st and 2nd if-statements, which have to be true before you can even start to look at that 3rd if statement.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!