Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Iterating a function within a for loop

Asked by Christopher on 23 Jul 2013

Hello all,

My code is posted below. I am trying to iterate the function (VTL) I created for different values of alpha and obtain the value of CL for each iteration. As it sits right now, it will run for alpha = 1 then I get the following error.

??? In an assignment A(I) = B, the number of elements in B and I must be the same.

Error in ==> Inputs at 32 result(i)=VTL(chord,alpha(i),alpha_ol,b,Sref,lambda,et,npan1,npan2,A,B,A1,B1)

I am not sure how to fix this error or what's causing it. I am hoping someone can help me. I am basically trying to iterate the function for varies alphas and make a plot of CL vs. alpha. Any help is appreciated.

chord=1.5385;         % Root Chord
alpha=[1 2 3 4 5 6];        % Angle of Attack alpha
alpha_ol=-2;    % Alpha zero lift camber
b=10;           % Total wing span b
Sref=10;        % Area
lambda=.3;        % Taper ratio equals 1 for non-tapered wing
et=0;           % Twist at the edge of the two wings
                % Uses a linear twist from zero at the wing root to 
                % twist et at wing edges
%%%%%%%%%%%%%%%%%%%%%%%%%% Patch 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
npan1=3;   % number of panels patch 1
A=[0 -5 0];
B=[0 0 0];
%%%%%%%%%%%%%%%%%%%%%%%%%%% Patch 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
npan2=3; % number of panels patch 1
A1=[0 0 0];
B1=[0 5 0];
% End of inputs
result = zeros(1, length(alpha));
for i = 1:length(alpha)
 result(i)=VTL(chord,alpha(i),alpha_ol,b,Sref,lambda,et,npan1,npan2,A,B,A1,B1)
[Cl,CL(i),CDi,Span_load]=VTL(chord,alpha(i),alpha_ol,b,Sref,lambda,et,npan1,npan2,A,B,A1,B1)
end
clear all

0 Comments

Christopher

Products

1 Answer

Answer by Cedric Wannaz on 23 Jul 2013
Edited by Cedric Wannaz on 23 Jul 2013

You can debug it e.g. by rewriting

 result(i)=VTL(chord,alpha(i),alpha_ol,b,Sref,lambda,et,npan1,npan2,A,B,A1,B1)

as

 tmp = VTL(chord,alpha(i),alpha_ol,b,Sref,lambda,et,npan1,npan2,A,B,A1,B1)
 result(i) = tmp ;

Then you put your cursor on the line that defines tmp and press F12. This will set a break point at this location. After this, you run the code (e.g. with F5) which will stop at the break point (remove clear all statements from your code before, as they clear breakpoints). You will see that a green arrow will indicate where the "program pointer" is in the code, and the command window's prompt will be K>> (which indicates that your are in a debugging session). In the command window, type size(tmp) to check that tmp is a scalar (you can also type class(tmp) to check that it has a numeric type, e.g. double). If not, you have a problem and you'll want to check why VTL doesn't output a valid object (numeric, scalar). Display the value of i as well and you'll see that it will be 1 as you'll be in the first iteration of the loop. Then press F10 iteratively to step through the code and go on checking the dimension/content of everything (in particular tmp) as you loop.

With this, you'll learn using the debugger. Note that if you want to debug the function VTL, you can step in using F11 instead of just executing it in one step with F10.

4 Comments

Cedric Wannaz on 23 Jul 2013

Currently the error is on

 result(i)=VTL(chord,alpha(i),alpha_ol,b,Sref,lambda,et,npan1,npan2,A,B,A1,B1)

and it suggests that the size of the output of VTL that you are trying to store in result(i) is different from the size of result(i) (which is [1 1]). This is why I am proposing that you store the output of VTL in an intermediary variable whose size is not predefined (which hence won't generate an error), so you can study its size.

Christopher on 23 Jul 2013

Cedric,

I did what you suggested with setting up tmp. For some reason tmp is coming out as my first output variable cl which is a column vector. I don't know why it is outputting tmp as cl and not CL, which is a scalar.

Cedric Wannaz on 23 Jul 2013

Ok, then when the cursor hits the line tmp = VTL(..), press F11 to step in the function instead of F10. You will see that it will jump into the file VTL.m.

There, display each input parameter to check whether inputs are correct (you can either type the name of variables in the command window, or pass your mouse over variables names when they are small enough).

Once done and you are sure that inputs are correct, go on stepping thorough the function with F10 (unless you want to enter sub-functions with F11 again). At each step/line, look whether the computation outputs what you think it should output. At one point, you might realize that you you forgot some indexing and that you are working on a whole vector/array, or that you are performing an operation which unexpectedly generates an array.

Note that you can use multiple break points, and go from one to the next by pressing F5 again (which continues until the end of the program or until the next break point). Also, pressing Shift+F5 interrupts the debugging session.

It is a good exercise, because with that you cover 3/4 of the features of the debugger I'd say, so it is really not lost time.

Cedric Wannaz

Contact us