You are on page 1of 9

2.

2 EXERCISES WITH MATLAB


2.2.1 Standard normal distribution
MATLAB CODE

%2.2.1 STANDARD NORMAL DISTRIBUTION


close all;
clear all;
load dat1_3;
mean_x=mean(x);
sd_x=std(x);
l=length(x);

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);

mean_theoretical=2; %theoretical mean=1/alpha=1/(1/2)


variance_theoretical=4; %theoretical variance=1/alpha^2=1/(1/4)

fprintf('PRACTICAL VALUE OF MEAN = %f \n',mean_pract)


fprintf('PRACTICAL VALUE OF variance = %f \n',variance_pract)
fprintf('THEORETICAL VALUE OF MEAN = %f \n',mean_theoretical)
fprintf('THEORETICAL VALUE OF MEAN = %f \n',variance_theoretical)
[pd_pract,k]=density(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

PRACTICAL VALUE OF MEAN = 2.027534


PRACTICAL VALUE OF VARIANCE = 4.451815
THEORETICAL VALUE OF MEAN = 2.000000
THEORETICAL VALUE OF VARIANCE = 4.000000
Figure window
2.2.3 Sum of random numbers
MATLAB CODE

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;

fprintf('OBSERVED MEAN = %f\n',mean_X);


fprintf('OBSERVED VARIANCE = %f\n',mean_X)

plot(x_cord_pract,pd_pract_X,'-',x_cord_theo,pd_theo_x,'-.','linewidth',2)
grid on
title('PROBABILITY DENSITY FUNCTION');
legend(‘practical Density','Theoretical 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

You might also like