Sunteți pe pagina 1din 8

# 603 Matlab HW01

Himani Gupta
% Q1 Generate 1024 Uniform Random Variables with mean 2 and variance 6
% mu=mean, var = variance
%Function used rand
clc;
mu=2;
var=6;
N=1024;
r=(mu-1)+rand(1,N)*sqrt(var);
plot(r);

Fig1-Plot Uniform Random Variables

% Q2 Generate1024 Gaussian Random Variables with mean 2 and variance 6
%mu=mean, var= variance
% function used randn
clc;
mu=2;
var=6;
N=1024;
r=mu+randn(1,N)*sqrt(var);
plot(r);

Fig2- Gaussian Random Variables

%Q3a Plot Gaussian RVs and superimpose mean and (+,-) one standard deviation
% mu=mean, var= variance, sigma= standard deviation

mu=2;
var=6;
sigma=1;
r=mu+randn(1024,1)*sqrt(var);
s=1:length(r);
plot(r,'black');
hold on;
plot(s,mu,'green');
hold on;
plot(s,sigma,'red');
hold on;
plot(s,-sigma,'blue');
hold off;

Fig3a-Gaussian Random variables, Mean and Standard Deviation

%Q3b Plot Uniform RVs and superimpose mean and (+,-) one standard deviation
% mu=mean, var= variance, sigma= standard deviation

mu=2;
var=6;
sigma=1;
r=(mu-1)+randn(1024,1)*sqrt(var);
s=1:length(r);
plot(r,'black');
hold on;
plot(s,mu,'green');
hold on;
plot(s,sigma,'red');
hold on;
plot(s,-sigma,'blue');
hold off;

Fig3b-Uniform Random variables, Mean and Standard Deviation

% Q4a Normalize histogram with Gaussian RV and superimpose PDF.
%sigma=standard deviation
% function used hist

sigma=sqrt(6);
[r,x]=hist(randn(1024,1)*sqrt(6)+2,30);
PDF=(1/(sqrt(2*pi)*sigma))*exp(-((x-2).^2)/(2*sigma^2));
bar(x,r/trapz(x,r));
hold on;
plot(x,PDF,'r');
hold off;

Fig4a-Histogram Superimposed by PDF with Gaussian RVs

% Q4b Normalize histogram with uniform rvs and superimpose PDF.
%sigma=standard deviation
% function used hist

sigma=sqrt(6);
[r,x]=hist(rand(1024,1)*sqrt(6)+2,30);
PDF=(1/(sqrt(2*pi)*sigma))*exp(-((x-2).^2)/(2*sigma^2));
bar(x,r/trapz(x,r));
hold on;
plot(x,PDF,'r');
hold off;

Fig4b-Histogram Superimposed by PDF with Uniform RVs

% Q5.a Square each RV and plot new histogram (without normalized hist)
% Derive new PDF and superimpose
% r=old RVs, p=new RVs(square of each old RVs)
%mu= new mean, sigma= new standard deviation

r=2+randn(1024,1)*sqrt(6);
[p,x]=hist(r.^2,30);
mu=mean(p);
sigma=std(p);
PDF=(1/(sqrt(2*pi)*sigma))*exp(-((x-mu).^2)/(2*sigma^2));
bar(x,p,'black');
hold on;
plot(x,PDF,'r');
hold off;

Fig5.a-New Histogram Superimposed by New Theoretical PDF

% Q5.b Square each RV and plot new histogram (with normalized hist)
% Derive new PDF and superimpose
% r=old RVs, p=new RVs (square of each old RVs)
% mu= new mean, sigma= new standard deviation

r=2+randn(1024,1)*sqrt(6);
[p,x]=hist(r.^2,500);
mu=mean(p);
sigma=std(p);
g=(1/(sqrt(2*pi)*sigma))*exp(-((x-mu).^2)/(2*sigma^2));
bar(x,p/trapz(x,p),'black');
hold on;
plot(x,g,'r');
hold off;

Fig5.b-New Histogram Superimposed by New Theoretical PDF