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

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 6 Dec 2020
Edited: per isakson on 7 Dec 2020
Now [ Ergebnis2p, Ergebnis3p ] are overwritten for each iteration and when the loop has finished only the last result is still there.
My idea was that you should replace "% store or display the result somewhere" with some code that did just that.
One possibility is to replace the loop by
Ergebnis2p = cell(1,50);
Ergebnis3p = cell(1,50);
for jj = 1 : 50
[ Ergebnis2p{jj}, Ergebnis3p{jj} ] = good_name( );
end
which stores the result in two large cell arrays.
Mustafa Vural
Mustafa Vural on 6 Dec 2020
Edited: per isakson on 7 Dec 2020
It does work but it doesnt work how it should be work, but I appreciate it.
When you run my code, you get one line where the first there columns are my estimated parameter. So when I run my code, it estimate my three parameter "T, b, t0". ergebnis3p shows my three estimated parameter (T, b, t0) and ergebnis2p only shows two parameter (T, b because t0=0).
So I want 50 lines where every line independently estimated his own 30 random numbers. Its mybe a bit complicated. Its like Monte-Carlo- Simulation.
per isakson
per isakson on 7 Dec 2020
Edited: per isakson on 7 Dec 2020
I have not tried to understand your code in any detail.
Why these loops when T and b both are scalars?
for v_T = 1:length(T)
for v_b = 1:length(b)
T_A = T(v_T);
b_A = b(v_b);
Why the complicated indexing?
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);
Now I run your code for the first time. I let the function return [ Ergebnis2p, Ergebnis3p ] , because the comment presented them as result. They are double row vectors of length seven.
>> clearvars
>> [ Ergebnis2p, Ergebnis3p ] = good_name( );
>> whos
Name Size Bytes Class Attributes
Ergebnis2p 1x7 56 double
Ergebnis3p 1x7 56 double
"So I want 50 lines where every line independently estimated his own 30 random numbers." Does that mean that you want a 50 by 30 double array? If so, you could use something like
M = nan( 50, 30 );
for jj = 1 : 50
[ out2p, out3p ] = good_name( );
% store or display the result somewhere
M( jj, .. ) = out2p(..);
M( jj, .. ) = out3p(..);
end
However, I don't understand which values shall go into the thirty columns.
Thats because, normally my T and b are not just one number, they are like b=1-5 for example.
I am investigating more combinations of T and b. I am not that good in Matlab so maybe it could be easier.
Thank you for you help, I am trying your code. I appreciate your time.
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)

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!