Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
186 views

Matlab Code For Random Variable

This document contains code to generate and analyze random variables in MATLAB. It: 1) Generates 1024 uniform and Gaussian random variables with mean 2 and variance 6, plotting the results. 2) Plots Gaussian random variables with the mean and +/- one standard deviation superimposed. It does the same for uniform random variables. 3) Generates histograms of the uniform and Gaussian random variables and superimposes the theoretical probability density function. 4) Squares the random variables, generates a new histogram, and superimposes the theoretical probability density function of the squared data.

Uploaded by

Him24
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views

Matlab Code For Random Variable

This document contains code to generate and analyze random variables in MATLAB. It: 1) Generates 1024 uniform and Gaussian random variables with mean 2 and variance 6, plotting the results. 2) Plots Gaussian random variables with the mean and +/- one standard deviation superimposed. It does the same for uniform random variables. 3) Generates histograms of the uniform and Gaussian random variables and superimposes the theoretical probability density function. 4) Squares the random variables, generates a new histogram, and superimposes the theoretical probability density function of the squared data.

Uploaded by

Him24
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like