Create objects in a loop

10 Ansichten (letzte 30 Tage)
anis oukil
anis oukil am 4 Aug. 2018
Kommentiert: anis oukil am 4 Aug. 2018
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 Kommentare
Matt J
Matt J am 4 Aug. 2018
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 am 4 Aug. 2018
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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

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