-1

I'm running a while loop and I am running in to some problems.

I have the following piece of code:

  Angle_int = 0.5;                    % Initial interpolation angle of attack
  Clmax2d(1,1:length(Yle_wing)) = 3;  % Dummy value
  diff = 0;                         % Dummy value

  while sum(diff < 0) > fix(tol*length(Yle_wing))
      Angle_int = Angle_int + 0.5; % Interpolation angle increases with 0.5 with every iteration

          for j = 1:length(Yle_wing)
          CL3d = interp1(Angle,[cl_matrix(1,j)  cl_matrix(2,j) cl_matrix(3,j)],Angle_int,'linear');
          CL_3DD(:,j) = CL3d;
          end  

      diff = (Clmax2d - CL_3DD);     % Difference between Cl2d and Cl3d
      Angle_stall = Angle_int;
      CL_3D = CL_3DD;
  end

For some reason, CL_3D = CL_3DD; and Angle_stall = Angle_int; seem to disappear when the while loop finishes. Hence, I cannot use their converged values ahead of the while loop since these variables are not recognized. I get the following error:

Undefined function or variable "CL_3D".

Hence, does someone knows what I am doing wrong? or any tips would be welcome as well.

Thanks in advance, cheers

GameOfThrows
  • 4,510
  • 2
  • 27
  • 44
Balraj Boyal
  • 155
  • 1
  • 2
  • 13
  • 1
    Please fix your indentation, you have nested loops and this is hard to follow. Are you sure these loops aren't wrapped up in a function that goes out of scope? – Dan Oct 13 '15 at 15:08
  • Also, as an aside, don't use `diff` as a variable name because you will be masking the very useful `diff` function in Matlab. – Dan Oct 13 '15 at 15:09
  • Yeah, I don't understand how to display my code properly. I was aware of the faulty indentation. And my function is not going out of scope, I'm sure of it. – Balraj Boyal Oct 13 '15 at 15:10
  • Ohh good tip, thank you! @Dan – Balraj Boyal Oct 13 '15 at 15:10
  • @StewieGriffin Ill give it a try, just a sec – Balraj Boyal Oct 13 '15 at 15:12
  • 1
    @BalrajBoyal indent it by pressing space bar the appropriate number of times for each line :/ please edit your question doing this – Dan Oct 13 '15 at 15:12
  • Just noticed: Either you posted the wrong error message, or the error is some other place in your code. Is it possible the error message says: `"Undefined function or variable "CL_3DD".`? Notice the double D's. – Stewie Griffin Oct 13 '15 at 15:23

1 Answers1

1

The error message:

Undefined function or variable "CL_3D".

is always because you're trying to use a variable or function that you haven't initialized yet. Often this happens in loops where you want to increment a counter, or compare values etc.

A common error is doing something like this without writing ii = 0 in front of the loop:

while ii < some_num
    ii = ii + 1;
    some_function();
end

With your dummy variables, you never enter the loop (unless tol < 0, which seems like an odd choice). You probably want to initialize diff = Inf or something like that. However, using diff as a variable name is not a good idea, since it's a builtin function.

You probably try to use CL_3D, when it's not yet initialized (somewhere else in your code, not in the part you posted). MATLAB tells you which line the error appears in, you should try using it!

Maybe initializing it like zeros(size(Clmax2d)); could work (it will definitely remove your error, but it might not give the desired behavior).

PS!

Using i and j as variables are not recommended as they represent the imaginary unit in MATLAB.

Community
  • 1
  • 1
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70