How to repeat run

4 views (last 30 days)
I am generating random numbers and after that I estimate them. In my code, there is a function for 2 and 3 parameterkombination (but thats not important for the question). But my problem is, to estimate for example 30 random numbers (n=30), I "run" the code then I get one number. But if I want to have for example 50 estimated numbers, I need to click 50 times "run"-button. Is there a code where it estimate automatically 50 times but everytime it estimate with other random numbers, like I would "run" the code manually. (sry for my bad english).
clear all;
n = 30;
t0 = 0.9;
b = 2;
T = 2;
for v_T = 1:length(T)
for v_b = 1:length(b)
T_A = T(v_T)
b_A = b(v_b)
pdf2p = @(x) (b_A/T_A).*(x/T_A).^(b_A-1).*(exp(-(x/T_A).^(b_A)));
integral_pdf2p = integral(pdf2p,0,Inf);
pdf2p_norm = @(x) (b_A/T_A).*(x/T_A).^(b_A-1).*(exp(-(x/T_A).^(b_A)))/integral_pdf2p;
pdf2p_norm_VZW = @(x) -(b_A/T_A).*(x/T_A).^(b_A-1).*(exp(-(x/T_A).^(b_A)))/integral_pdf2p;
min2p=fminbnd(pdf2p_norm_VZW,0,2*T_A); %returns a value x that is a local minimizer
max2p=pdf2p_norm(min2p) % X-wert min2p in die normierte funktion einsetzen = hochpunkt
%Grenze der Funktion 2P
Grenze_pdf2p_norm=2*T_A; %rechter grenzwert. 2*T um schneller die 99% der Fläche zu bekommen.
while (integral(pdf2p_norm,0,Grenze_pdf2p_norm)<0.9999) %solange das integral(die fläche) der normierten funktion(zwischen 0 bis 2*T_A) kleiner als 99% ist
Grenze_pdf2p_norm=Grenze_pdf2p_norm+0.1; %grenze wird um 0,1 nach rechts versetzt
end
Grenze_pdf2p_norm;
i=1;
while(i<=n) %ist diese bedingung wahr, dann wird die untere zeilen ausgeführt
ZufallszahlenX2p = random('unif',0,Grenze_pdf2p_norm); % xachse zufallszahlen generieren
ZufallszahlenY2p = rand*max2p; % yachse zufallszahlen generieren
if(ZufallszahlenY2p<pdf2p_norm(ZufallszahlenX2p)) %wenn zufallszahl in yachse < normierte funktion (mit zufallszahl der xachse)
data2p(i,v_b,v_T)=ZufallszahlenX2p; %nimm den wert an
% i um 1 erhöhen und wdh
i=i+1; % i um 1 erhöhen und wdh
end
end
end
end
for v_T = 1:length(T)
for v_b = 1:length(b)
T_A = T(v_T)
b_A = b(v_b)
% 2P schätzen
params2p(v_b,1:2,v_T) = wblfit(data2p(:,v_b,v_T));
params2p(v_b,4,v_T) = T_A;
params2p(v_b,5,v_T) = b_A;
params2p(v_b,7,v_T) = n;
end
Ergebnis2p((v_T-1) *length(b)+1:v_T*length(b), 1:size(params2p, 2)) = params2p(:,:,v_T);
Ergebnis3p((v_T-1) *length(b)+1:v_T*length(b), 1:size(params3p, 2)) = params3p(:,:,v_T);
end

Accepted Answer

per isakson
per isakson on 6 Dec 2020
Put your script in a function and call it 50 times
for jj = 1 : 50
[ Ergebnis2p, Ergebnis3p ] = good_name( );
% store or display the result somewhere
end
function [ Ergebnis2p, Ergebnis3p ] = good_name( )
% your code, except "clear all"
end
  5 Comments
per isakson
per isakson on 7 Dec 2020
I am investigating more combinations of T and b In that case you should let T and b be input arguments
function [ Ergebnis2p, Ergebnis3p ] = good_name( T, b )
n = 30;
t0 = 0.9;
% b = 1:5;
% T = 2:6;
...
Test runs
>> [ Ergebnis2p, Ergebnis3p ] = good_name( 2, (1:5) );
>> whos
Name Size Bytes Class Attributes
Ergebnis2p 5x7 280 double
Ergebnis3p 5x7 280 double
>> [ Ergebnis2p, Ergebnis3p ] = good_name( (2:6), (1:5) );
Warning: Maximum likelihood estimation did not converge. Function evaluation
limit exceeded.
> In mlecustom (line 239)
In mle (line 245)
In good_name (line 102)
>> whos
Name Size Bytes Class Attributes
Ergebnis2p 25x7 1400 double
Ergebnis3p 25x7 1400 double
And maybe it's better to put together the desired row inside the function, good_name, and return it as output.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!