Create objects in a loop

조회 수: 10 (최근 30일)
anis oukil
anis oukil 2018년 8월 4일
댓글: anis oukil 2018년 8월 4일
Hello, i have a class with properties and methods, i want to calculate somtehing, which is a method in my class, by varying a property in my class. Is it possible to create a loop that creates as objects as the length of my property, and then use the method for every object created ?
I could do it manually but for i=10:1:5000, it takes a long long time...
Here are the parts of my code for this question the property i want to vary is "N", the function that calculates what i want is "integ_sigma"
classdef pot_flow_class
properties
U;
H;
N;
Ng;
r;
th;
sigma;
% thp;
X;
Y;
nb_iso;
interp;
end
methods
function obj = pot_flow_class(valH,valN,valNg,valR,valU,val_nb_iso)
obj.U = valU;
obj.H = valH;
obj.N = valN;
obj.Ng = valNg;
obj.r = valR;
obj.nb_iso = val_nb_iso;
obj.th=0:pi/(valN-1):pi;
% sigma pour le pb infini
obj.sigma=sin(obj.th)/pi;
obj.X=linspace(-10,10,valNg);
obj.Y=linspace(-200,200,valNg);
end
function obj = calcul_source(obj)
RC = obj.r*cos(obj.th);
RS = obj.r*sin(obj.th);
RC = RC(2:obj.N-1);
RS = RS(2:obj.N-1);
Mat_A = pi*eye(obj.N-2);
for i=1:obj.N-2
for j=1:obj.N-2
Mat_A(i,j)= Mat_A(i,j)+...
pi/obj.N*...
(RC(i)*(RC(i)+RC(j)-2*obj.H)+RS(i)*(RS(i)-RS(j)))/((RC(i)+RC(j)-2*obj.H)^2+(RS(i)-RS(j))^2)...
-pi/obj.N*...
(RC(i)*(RC(i)+RC(j)-2*obj.H)+RS(i)*(RS(i)+RS(j)))/((RC(i)+RC(j)-2*obj.H)^2+(RS(i)+RS(j))^2);
end
end
obj.sigma(2:obj.N-1)=linsolve(Mat_A,-RS');
end
function integ_sigma(obj)
S = trapz(obj.th,obj.sigma.*obj.sigma);
disp(S)
end
end
end
  댓글 수: 2
Matt J
Matt J 2018년 8월 4일
to create a loop that creates as objects as the length of my property, and then use the method for every object created ?
There is no object creation shown in integ_sigma, so it is not clear what you mean. If you want to create multiple objects, you should do that in the constructor.
anis oukil
anis oukil 2018년 8월 4일
I want to create multiple objects of this class for N from 10 to 5000 , and call calcul_source then integ_sigma for each N.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

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