MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Thread Subject: Erros with "For" loop nested in "While" loop

 Subject: Erros with "For" loop nested in "While" loop From: Pathiraja Date: 2 Jan, 2012 00:58:08 Message: 1 of 4 I GET THIS MESSAGE, ??? Subscript indices must either be real positive integers or logicals. Error in ==> BEMMT at 82             Thrust = sum(Dt)*rStep; WHEN I TRIES TO RUN BELOW CODE, for i = 1:3     waitbar(i/MaxV,WB)          for j = 1:2         omega(j) = 2*pi*j;                  a = 0.2;         finish = 0;         sum = 1;         while finish == 0             Thrust = 0;             for k = 1:size(r1,2)                 Loc(k) = r1(k);                 VT(k) = omega(j)*Loc(k);                 W2(i,j,k) = sqrt((i*(1+1))^2 + omega(j)^2*Loc(k)^2);                 Phi(i,j,k) = atan((i*(1+a))/(omega(j)*Loc(k)));                 Alpha(i,j,k) = RTheta + T1*Loc(k) - rad2deg(Phi(i,j,k));                                  CL(i,j,k) = aa*deg2rad(Alpha(i,j,k));                 CD(i,j,k) = Cdo + K*CL(i,j,k)^2;                                  Lift(i,j,k) = 0.5*ro*W2(i,j,k)^2*Cr*aa*deg2rad(Alpha(i,j,k));                 Drag(i,j,k) = 0.5*ro*W2(i,j,k)^2*Cr*CD(i,j,k);                 Dt(i,j,k) = (Lift(i,j,k)*cos(Phi(i,j,k)))-(Drag(i,j,k)*sin(Phi(i,j,k)));                 Dq(i,j,k) = (Lift(i,j,k)*sin(Phi(i,j,k)))+(Drag(i,j,k)*cos(Phi(i,j,k)));                 Dpr(i,j,k) = Dq(i,j,k)*omega(j)*Loc(k);                                  eff(i,j,k) = Dt(i,j,k)*i/Dpr(i,j,k);             end             Thrust = sum(Dt)*rStep;             da = Thrust/(2*ro*Ar*i^2);             anew = (a + da)/2;                          if (a - anew)<1e-10                 finish = 1;             end                          a = anew;             sum = sum + 1;                          if sum>500                 finish = 1;             end         end         Torque = sum(Dq,3)*rStep;         Pr = sum(Dpr,3)*rStep;         Eff = Thrust.*repmat((MinV:size(Thrust,1))',1,size(Thrust,2))./Pr;         AdvRatio(i,j) = i/(j*D);     end end COULD SOMEONE HELP ME TO CORRECT THIS CODE
 Subject: Erros with "For" loop nested in "While" loop From: Roger Stafford Date: 2 Jan, 2012 01:22:08 Message: 2 of 4 "Pathiraja " wrote in message ... > ??? Subscript indices must either be real positive integers or logicals. > Error in ==> BEMMT at 82 > Thrust = sum(Dt)*rStep; > ......... > sum = 1; > ......... > Thrust = sum(Dt)*rStep; - - - - - - - -   Your difficulty is caused by using the name 'sum' for two purposes, a variable name and the matlab function with that name. When you wrote  Thrust = sum(Dt)*rStep; matlab assumed you referred to the variable 'sum' rather than the 'sum' function and was then puzzled by the fact that Dt is not a valid index. You should never use matlab's reserved function names to name your variables. Roger Stafford
 Subject: Erros with "For" loop nested in "While" loop From: Pathiraja Date: 2 Jan, 2012 10:37:09 Message: 3 of 4 "Roger Stafford" wrote in message ... > "Pathiraja " wrote in message ... > > ??? Subscript indices must either be real positive integers or logicals. > > Error in ==> BEMMT at 82 > > Thrust = sum(Dt)*rStep; > > ......... > > sum = 1; > > ......... > > Thrust = sum(Dt)*rStep; > - - - - - - - - > Your difficulty is caused by using the name 'sum' for two purposes, a variable name and the matlab function with that name. When you wrote > > Thrust = sum(Dt)*rStep; > > matlab assumed you referred to the variable 'sum' rather than the 'sum' function and was then puzzled by the fact that Dt is not a valid index. You should never use matlab's reserved function names to name your variables. > > Roger Stafford THANK YOU ROGER STAFFORD Can you suggest me a way to correct this error. Furthermore, When I remove While Loop and leave the calculations in the While Loop under for loop (i.e. k = 1:size(R1,2)), the above command works. I need to sum all the forces. If you have an idea how this can be done it will be great. Looking forward to hearing from you
 Subject: Erros with "For" loop nested in "While" loop From: Roger Stafford Date: 2 Jan, 2012 17:31:08 Message: 4 of 4 "Pathiraja " wrote in message ... > THANK YOU ROGER STAFFORD > > Can you suggest me a way to correct this error. Furthermore, When I remove While Loop and leave the calculations in the While Loop under for loop (i.e. k = 1:size(R1,2)), the above command works. > I need to sum all the forces. > If you have an idea how this can be done it will be great. > Looking forward to hearing from you - - - - - - - - - -   I have already told you how to correct your error, Pathiraja! In the lines  sum = 1; and  sum = sum + 1; you have named a variable 'sum'. Don't ever do that!! Name this variable something else because that is a name reserved for the 'sum' function in matlab. That is why matlab got confused on the line  Thrust = sum(Dt)*rStep; You intended the name 'sum' here to refer to matlab's summing function but it thought you were still referring to the 'sum' variable.   The same applies to all other matlab functions. Don't ever name any of your variables the same as any of these function names if you want to also use that corresponding function. Roger Stafford