How to for loop through a function?

11 views (last 30 days)
Nason Zikayo
Nason Zikayo on 13 Apr 2020
Commented: Sindar on 15 Apr 2020
So I have the code below which generates coordinates for a 2D airfoil. It takes a inputs such as CST_no_tail([-1 -1 -1], [1 1 1],100) and outputs a [101,2] array of x and y coordinates. This part works fine.
function [coord] = CST_no_tail(wl,wu,N);
%CST_no_tail([-1 -1 -1], [1 1 1],100) - Running this in command works fine
% Description : Create a set of airfoil coordinates using CST parametrization method
% Input : wl = CST weight of lower surface
% wu = CST weight of upper surface
% dz = trailing edge thickness
% Output : coord = set of x-y coordinates of airfoil generated by CST
% Create x coordinate
x=ones(N+1,1);y=zeros(N+1,1);zeta=zeros(N+1,1);
for i=1:N+1
zeta(i)=2*pi/N*(i-1);
x(i)=0.5*(cos(zeta(i))+1);
end
% N1 and N2 parameters (N1 = 0.5 and N2 = 1 for airfoil shape)
N1 = 0.5;
N2 = 0.5;
zerind = find(x(:,1) == 0); % Used to separate upper and lower surfaces
xl= x(1:zerind-1); % Lower surface x-coordinates
xu = x(zerind:end); % Upper surface x-coordinates
[yl] = ClassShape(wl,xl,N1,N2); % Call ClassShape function to determine lower surface y-coordinates
[yu] = ClassShape(wu,xu,N1,N2); % Call ClassShape function to determine upper surface y-coordinates
y = [yl;yu]; % Combine upper and lower y coordinates
coord = [x, y]; % Combine x and y into single output
%% Function to calculate class and shape function
function [y] = ClassShape(w,x,N1,N2);
% Class function; taking input of N1 and N2
for i = 1:size(x,1)
C(i,1) = x(i)^N1*((1-x(i))^N2);
end
% Shape function; using Bernstein Polynomials
n = size(w,2)-1; % Order of Bernstein polynomials
for i = 1:n+1
K(i) = factorial(n)/(factorial(i-1)*(factorial((n)-(i-1))));
end
for i = 1:size(x,1)
S(i,1) = 0;
for j = 1:n+1
S(i,1) = S(i,1) + w(j)*K(j)*x(i)^(j-1)*((1-x(i))^(n-(j-1)));
end
end
% Calculate y output
for i = 1:size(x,1)
y(i,1) = C(i,1)*S(i,1) + x(i);
end
I'm trying to compare multiple inputs so I want to loop through a matrix of multiple inputs, running each iteration through the function.
Can someone explain why my method below doesn't work?
ar = {[-1 -1 -1] [1 1 1] 100;
[-0.5 -1 -1] [0.5 1 1] 100;
[-1 -1 -0.5] [1 1 0.5] 100;
[-0.5 -1 -0.5] [0.5 1 0.5] 100
}
% create coord array
shapes_list = zeros(101,2, length(ar));
for j = 1:length(ar);
shapes_list(:,:,j) = CST_no_tail(ar{j,:}) %should take output coord and insert it insto
end
It looks like output of the for loop isnt the same as the quivalent cell but I'm not sure why?
For example inserting the first row of ar into the function works fine - CST_no_tail([-1 -1 -1], [1 1 1],100)
However when inserting the row from the matrix doesnt work - CST_no_tail(ar(1))

Answers (1)

Sindar
Sindar on 13 Apr 2020
To pass/access the elements in a cell array, you need {}. () accesses the cell. Check the difference between what returns from
ar(1)
and
ar{1}
  6 Comments
Nason Zikayo
Nason Zikayo on 14 Apr 2020
Thanks for the link, this looks like a really useful debugging feature.
Error in CST_no_tail (line 28)
[yl] = ClassShape(wl,xl,N1,N2); % Call ClassShape function to determine lower surface y-coordinates
Checking this I can see that after completing the classshape with the input wl = [-1 -1 -1] or ar{1,1}, the function never moves on to line 29 (which takes wu or ar {1,2}). Instead line 28 is repeatedly exectuted after completion despite a valid output being recieved. Not sure why this is as from my understanding, once line 28 executes and yl is recived it should move on to execute line 29.
CST_no_tail(ar{j,:}) should be called, I've fixed the code in the post if you wanted to recreate the error.
Sindar
Sindar on 15 Apr 2020
Try running line-by-line, seeing what runs (also check the variable sizes in case it isn't a recursion that's the problem)

Sign in to comment.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!