Can you put a function inside a for loop?

97 Ansichten (letzte 30 Tage)
Jacob
Jacob am 28 Mär. 2014
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?

Antworten (2)

Mischa Kim
Mischa Kim am 28 Mär. 2014
Bearbeitet: Mischa Kim am 28 Mär. 2014
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 am 28 Mär. 2014
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!

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by