![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276002/image.png)
randn command variance problem
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello, i am trying to create a randomly distributed temperature where the avarage is 310 and variance is 4.
I wrote the following code, but as could be seen from the plot bellow it gives me values of 318 ,301 which are far away from variance 4.
Where did i go wrong?
Thanks
n=100000;
Temp_rand=2*randn(n,1);
T = 37+273+Temp_rand;
plot(T);
xlabel('sample number');
ylabel('Temperature');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276001/image.jpeg)
0 commentaires
Réponse acceptée
Ameer Hamza
le 9 Mar 2020
Modifié(e) : Ameer Hamza
le 9 Mar 2020
In a normal distribution, the variance is not the maximum limit between two random numbers. A single random number can go way beyond the variance (but the probability of that happening is extremely small). If you want to have a strict limit, then use uniform random numbers. For example,
n=100000;
min_value = 27+273-4;
max_value = 27+273+4;
T = min_value + (max_value - min_value) * rand(n,1);
plot(T);
xlabel('sample number');
ylabel('Temperature');
ylim([min_value-2, max_value+2]);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276002/image.png)
1 commentaire
Steven Lord
le 9 Mar 2020
To supplement what Ameer Hamza said, for your original data set let's look at the bounds and the statistical properties of the data set.
n=100000;
Temp_rand=2*randn(n,1);
T = 37+273+Temp_rand;
M = mean(T);
V = var(T);
[N, X] = bounds(T);
[N, M, X, V]
I've run this several times. The smallest lower bound I've seen is 300.5861 and the highest upper bound is 318.8690. The mean is very close to 310 and the variance is around 4 in all my trials.
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!