How to for loop through a function?
조회 수: 12 (최근 30일)
이전 댓글 표시
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))
댓글 수: 0
답변 (1개)
Sindar
2020년 4월 13일
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
Sindar
2020년 4월 15일
Try running line-by-line, seeing what runs (also check the variable sizes in case it isn't a recursion that's the problem)
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!