Can you put a function inside a for loop?

조회 수: 97 (최근 30일)
Jacob
Jacob 2014년 3월 28일
답변: Salaheddin Hosseinzadeh 2014년 3월 28일
I have this function:
%Outputs
%u - real number - horizontal velocity component due to doublet in global FoR
%v - real number - vertical velocity component due to doublet in global FoR % %Inputs
%p - 1 x 2 array - coordinates [x,z] of point at which to evaluate u,v
%p1 - 1 x 2 array - coordinates [x1,z1] of panel start point
%p2 - 1 x 2 array - coordinates [x2,z2] of panel end point %mu - real number - doublet strength
function [u,v]=cdoublet(p,p1,p2)
Can i put this function inside a for loop to get an array of u and another array for v? Of course, p, p1, p2 will also change for every iteration of the loop. Is it possible to put the function inside the loop?

답변 (2개)

Mischa Kim
Mischa Kim 2014년 3월 28일
편집: Mischa Kim 2014년 3월 28일
Sure, e.g.,
a = [5 4 3 2];
for ii = 1:numel(a)
b(ii) = times(a(ii),ii);
end
You can have any other function instead of times inside the loop.

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2014년 3월 28일
Hi Jacob
You have to make anoher function or script, lets call it 'main' in case you made a function, and in 'main' you can call 'cdoublet' and get its outputs, store them in an array and also change the values of p p1... in the main function.
But I suggest ou not to make a function, just write another script and put cdoblet ina for loop
x=[];
y=[];
for i=1:10
p1=i;
p2=i+1; % or what ever
[x(i),y(i)]=cdoublet(p,p1,p2);
end
you can do this in a new sript file
Good Luck!

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by