Adding some changes and code rewriting
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
[EDIT: 20110628 00:13 CDT - reformat - WDR]
i have code and i would like to make some changes and add 2 conditions for these 3 random variables (c, gama, fi). i want to limit each of them in a specific zone and check the random number which is generated in every loop to not be duplicated also not be out of range.
these are conditions:
- 2.7< fi <16.3
- 450< c <800
- 1.92< gama <1.98
and each of them must be check to not be repeating in every loop.
this is my code :
clear;
clc
B=1000;L=2000;Sc=1.1;Sq=1.1;Sgama=.8;
nsamples=10000;
for i=1:nsamples
C=normrnd(620,147.64);
gama=normrnd(1.96,0.02);
fi=normrnd(3.76,1.1034);
Nq=tan((pi/4)+(pi*fi/360))*tan((pi/4)+(pi*fi/360))*2.718^(pi*tan(fi*pi/180));
Nc=(Nq-1)*cot(fi*pi/180);
Ngama=2*(Nq+1)*tan(fi*pi/180);
qult(i)=(C*Nc*Sc)+(384*Nq*Sq)+(980*Ngama*Sgama);
end
0 Commenti
Risposta accettata
Walter Roberson
il 28 Giu 2011
clc
B=1000; L=2000; Sc=1.1; Sq=1.1; Sgama=.8;
nsamples=10000;
rmem = zeros(3,nsamples);
for i=1:nsamples
while true
C = normrnd(620,147.64);
if C<=450 || C >= 800 || ismember(C,rmem(1,1:K-1)); continue; end
gama = normrnd(1.96,0.02);
if gama <= 1.92 || gama >= 1.98 || ismember(gama,rmem(2,1:K-1)); continue; end
fi = normrnd(3.76,1.1034);
if fi <= 2.7 || fi >= 16.3 || ismember(fi,rmem(3,1:K-1)); continue; end
rmem(:,K) = [C; gama; fi];
break
end
Nq = tan((pi/4)+(pi*fi/360)) * tan((pi/4)+(pi*f/360)) * exp(pi*tan(fi*pi/180));
Nc = (Nq-1)*cot(fi*pi/180);
Ngama = 2*(Nq+1)*tan(fi*pi/180);
qult(K) = (C*Nc*Sc)+(384*Nq*Sq)+(980*Ngama*Sgama);
end
I am not clear as to why you want to check for duplicates: it isn't that duplicates are impossible, but they are relatively rare at 53 bit precision. In the 500000 normrnd(620,147.6) that I generated as a test, the two closest values were a good 2.9899638320785e-11 apart, which was 7 clear bits greater resolution than the largest value generated. I calculate that a 50% chance of a duplicate does not arise until slightly under 112 million samples.
If you eliminate the checks for duplicates or generate a few extra points and discard the duplicates and any extras afterwards, then the code can be made considerably more efficient.
4 Commenti
Walter Roberson
il 8 Lug 2011
Looking back at the code, it appears I renamed a variable but inconsistently. Please change the line
for i=1:nsamples
to
for K=1:nsamples
At the end of the loops, rmem(1,:) will be all of the C values.
Più risposte (1)
Krishna Kumar
il 28 Giu 2011
To get the random numbers within some limits, you can use randn( since you seem to want random distributed values. x= lowerlimit+(lower limit-upperlimit)*randn(size(array)); For the second question, you must weigh the utility of it against the computational burden it imposes. I suppose randn function does not return exactly equal values so frequently. If at all you want to check the same, you need to store all previous values in an array and check the new value with this each time, which I think does not add value to code. Or you can try some funny ideas like this. random_number= mean(randn(1)+rand(1)). Since both of the random number may not be repeated, chance of repetition are still lower. Or form the random numbers in a single matrix initially,(nsamples,1) sizes vector and check for repetition before entering the loop.
Hope you can fix these things easily.
2 Commenti
Walter Roberson
il 28 Giu 2011
The mean() of two random values with the same distribution will have a smaller standard deviation than a single random value of that distribution would have: the values will cluster more to the center.
For example, with six-sided dice, the probability of a "1" being thrown on a single dice is 1/6, but the probability of 1 being the result of the mean of two six-sided dice is only 1 in 36 (both would have to be thrown as 1's for the mean to be 1.)
Vedere anche
Categorie
Scopri di più su Elementary Math in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!