In: Mechanical Engineering
Use a random number generator to produce 1000 normally distributed numbers with a mean of 20 and a variance of 4. Obtain the mean, variance, and histogram of these numbers, and discuss whether they appear normally distributed with the desired mean and variance.
Program plan:
• To find the mean and variance of the 1000 normally distributed random numbers.
• To plot the histogram of these random numbers.
Program:
%**********************************************************
%A matlab code is written to find the mean and variance of
%the normally distributed random number with mean 20 and
%variance 4. To obtain the histogram of the numbers using
%matlab.
%**********************************************************
%Declaration of constant
m=1000;
%Generates 1000 random number of mean 20 and variance 2
x=2.*randn(1,m)+20;
%Displays text
disp('The mean of the 1000 random number is ');
%Displays value
disp(mean(x));
%Displays text
disp('The variance of the random numbers is ');
%Displays value
disp((std(x))^2);
%Declaration of array
range=[min(x):max(x)];
%Creates histogram
hist(x, range),
%Assigns text to x-axis
xlabel('Random values'),
%Assigns text to y-axis
ylabel('Absolute Frequency')
Output:
>> Untitled
The mean of the 1000 random number is
20.1020
The variance of the random numbers is
4.2016
The mean of the 1000 random number is
20.1020
The variance of the random numbers is
4.2016