Matlab Code: 2.2 Exercises With Matlab 2.2.1 Standard Normal Distribution
Matlab Code: 2.2 Exercises With Matlab 2.2.1 Standard Normal Distribution
for i=1:l
x_bar(i)=(x(i)-mean_x)/sd_x;
end
[x_density_pract,x_cord_pract]=density(x_bar);
m=-4;
inc=.008;
for i=1:1:1000
x_theo(i)=m+inc;
m=x_theo(i);
end
for i=1:1000
m=m+inc;
x_density_theo(i) = (1/sqrt(2*pi))*exp(-((x_theo(i)^2)/2));
end
x_cord_theo=-3.992:.008:4;
plot(x_cord_theo,x_density_theo,'--',x_cord_pract,x_density_pract,'-','linewid
th',2)
grid on
title('PROBABILITY DENSITY FUNCTION')
legend('Theoretical Density','Practical Density','tr')
xlabel('X')
ylabel ('fx(x)')
OUTPUT
Figure window
2.2.2 Exponential Distribution
MATLAB CODE
%Exponential Distribution
clc;
clear all;
close all;
load dat1_1;
Xn=pas_value;
Yn=-2*log(Xn);
mean_pract = mean(Yn);
variance_pract = var(Yn);
Yn_theo=.001:.001:16;
pd_Yn_theo=2*exp(-0.5*Yn_theo);
plot(k,pd_pract,'-',Yn_theo,pd_Yn_theo,'--','linewidth',2)
grid on
title('PROBABILITY DENSITY FUNCTION')
legend('Practical Density','Theoretical Density','tr')
xlabel('X')
ylabel ('f(x)')
OUTPUT
clc
clear
rand('state',0);
dat2_1=rand(1000,2);
pass_value=dat2_1;
z=sum(dat2_1,2);
mean_z=mean(z);
variance_z=var(z);
[z_pract_density,x_cord]=density(z);
% x_cord =linspace(min(z), max(z),10);
%theory
n=1;
for i=.001:.001:2
z_theo(n)=i;
if n<=1000
z_theo(n)=z_theo(n);
else
z_theo(n)=2-z_theo(n);
end
x_cord_theo(n)=i;
n=n+1;
end
plot(x_cord,z_pract_density,'-',x_cord_theo,z_theo,'-.','linewidth',2);
grid on;
title('PROBABILITY DENSITY FUNCTION');
legend('Practical Density','Theoretical Density','tr');
xlabel('z');
ylabel ('fz(z)');
fprintf('OBSERVED MEAN = %f\n',mean_z);
fprintf('OBSERVED VARIANCE = %f\n',variance_z);
OUTPUT
OBSERVED MEAN = 1.001405
OBSERVED VARIANCE = 0.176638
THEORETICAL MEAN=1
THEORETICAL VARIANCE=.1666
Figure window
2.2.4 Product of random numbers
MATLAB CODE
clc;
clear all;
close all;
load dat2_1
x=pass_value;
x1=x(:,1);
x2=x(:,2);
X=x1.*x2;
mean_X=mean(X);
variance_X=var(X);
pd_pract_X=density(X);
x_cord_pract=linspace(min(X),max(X),10);
n=1;
for i=.001:.001:1
pd_theo_x(n)=-log(i);
n=n+1;
end
[x_cord_theo]=.001:.001:1;
plot(x_cord_pract,pd_pract_X,'-',x_cord_theo,pd_theo_x,'-.','linewidth',2)
grid on
title('PROBABILITY DENSITY FUNCTION');
legend('Theoretical Density','practical Density','tr');
xlabel('X');
ylabel ('fx(X)');
OUTPUT
OBSERVED MEAN = 0.254443
OBSERVED VARIANCE = 0.254443
THEORETICAL MEAN=0.25
THEORETICAL VARIANCE=.0486
Figure window