0% found this document useful (0 votes)
59 views11 pages

Ans Problem 1: Matlab Code

The document contains MATLAB code to generate random numbers and plot various probability distributions. It includes code to: 1) Plot the relative frequency of heads over multiple coin toss trials to estimate the probability of heads. 2) Generate Gaussian random variables and plot the estimated vs true probability density function. 3) Plot the estimated and true Rayleigh probability density function. 4) Plot the estimated and true probability density function of the magnitude squared of a complex Gaussian random variable. 5) Generate a scatter plot of two correlated random variables and conclude that if one variable equals 1, then the other is approximately equal to 1.

Uploaded by

Umer Qureshi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views11 pages

Ans Problem 1: Matlab Code

The document contains MATLAB code to generate random numbers and plot various probability distributions. It includes code to: 1) Plot the relative frequency of heads over multiple coin toss trials to estimate the probability of heads. 2) Generate Gaussian random variables and plot the estimated vs true probability density function. 3) Plot the estimated and true Rayleigh probability density function. 4) Plot the estimated and true probability density function of the magnitude squared of a complex Gaussian random variable. 5) Generate a scatter plot of two correlated random variables and conclude that if one variable equals 1, then the other is approximately equal to 1.

Uploaded by

Umer Qureshi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

ANS PROBLEM 1: MATLAB CODE

close all clear all Trials=2000; for i=1:Trials %Close any previously opened window like graph. %Clear all varibles in workspace. %Supposing Number of trials. %Loop

if rand(1,1)<0.5 %Condition X(i,1)=1; else X(i,1)=0; end end vector=ones(size(X));%generate vector of 1's of the size of no of heads R_f=cumsum(X)./cumsum(vector); plot(R_f) grid; title('No. of Trials Required for an Accurate Estimate of Probability') xlabel('Minimum no of Trials Reqd for accuracy') ylabel('Relative Frequency') %Formula for relative frequency . %Tail=0. %Head=1.

GRAPH

OBSERVATION: From the graph it can be concluded that at least 400 trials are must
an accurate estimate of probability of heads.

to get

ANS PROBLEM 2(a): MATLAB CODE[THE COMPLETE CONCEPT IS TAKEN FROM THE CODE IN TEXT BOOK]
close all; clear all; randn('state',0) x=randn(1000, 1); bincenters=[-4:0.5:4]'; bins=length(bincenters); h=zeros(bins,1); %close previously opened window %clear ll variables in workspace %initialize guassian random no's gnrtr %generate 1000 random #'s %specify bins center positions %total no of bins %Generate 0's vector

for i=1:length(x) for k=1:bins

%dumping random no's into specified bins

if x(i)>bincenters(k)-0.5/2 & x(i)<=bincenters(k)+0.5/2%condition h(k,1)=h(k,1)+1; end end end %incrementing the zero vector by 1

pxest=h/(1000*0.5); xaxis=[-5:0.01:5]'; px=(1/sqrt(2*pi))*exp(-0.5*xaxis.^2);

%estimated probability

%true Probability

bar(bincenters,pxest,0.5) hold on plot(xaxis,px,'r') legend('estimated','true') grid;

%plot bars % command for plot on plot

title('True and Estimated PDF for Guassian Rnd Vrble'); ylabel('Estimated and True Probability'); xlabel('Ran.#');

GRAPH:

ANS PROBLEM 2(b): MATLAB CODE[THE COMPLETE CONCEPT IS TAKEN FROM THE CODE IN TEXT BOOK]
close all; clear all; randn('state',0) z=randn(2000, 1)+j*randn(2000, 1); Mg_z=sqrt(real(z).^2+imag(z).^2); width_of_PDF=1; bincenters=[0.5:0.5:4]'; bins=length(bincenters); h=zeros(bins,1); for i=1:length(Mg_z) for k=1 :bins if Mg_z(i)>bincenters(k)-0.5/2 & Mg_z(i)<=bincenters(k)+0.5/2 %close previously opened wndow %clear data in worksapce %initialize guassian random number generator %generate complex numbers %magnitude of complex numbers %probability density function's width %specify bins center positions %total no of bins %generate 0's vector %assigning corresponding values to bins

h(k,1)=h(k,1)+1; end end end pxest=h/(1000*0.5); xaxis=[0:0.01:5]'; px=(xaxis/width_of_PDF).*exp((-0.5*xaxis.^2)/width_of_PDF); %true probability bar(bincenters,pxest,0.5) hold on plot(xaxis,px,'r') legend('estimated','true') grid; title(' Rayleigh PDF . '); xlabel('complex #s.'); %estimated probability

ylabel('Estimated and True Probability');

GRAPH:

ANS PROBLEM 2(c): MATLAB CODE[THE COMPLETE CONCEPT IS TAKEN FROM THE CODE IN TEXT BOOK]
close all; clear all; randn('state',0) z=randn(1000, 1)+j*randn(1000, 1); Mg_z=sqrt(real(z).^2+imag(z).^2); width_of_PDF=1; bincenters=[-3:0.5:3]'; bins=length(bincenters); h=zeros(bins,1); %close previously opened wndow %clear data in worksapce %initialize guassian random number generator %generate complex numbers %magnitude of complex numbers %probability density function's width %specify bins center positions %total no of bins %generate 0's vector

for i=1:length(Mg_z) for k=1 :bins

%assigning corresponding values to bins

if Mg_z(i)>bincenters(k)-0.5/2 & Mg_z(i)<=bincenters(k)+0.5/2 h(k,1)=h(k,1)+1; end end end pxest=h/(1000*0.5); xaxis=[-3.25:0.01:3.25]; px=1/(3.25-(-3.25)); bar(bincenters,pxest,0.5) hold on plot(xaxis,px,'r') legend('estimated','true') grid; xlabel('complex #s.'); ylabel('Estimated and True Probability'); %true probability %estimated probability

GRAPH:

ANS PROBLEM 2(d): MATLAB CODE[THE COMPLETE CONCEPT IS TAKEN FROM THE CODE IN TEXT BOOK]
close all; clear all; randn('state',0) z=randn(2000, 1)+j*randn(2000, 1); Mg_z=sqrt(real(z).^2+imag(z).^2); width_of_PDF=1; Mag_square=Mg_z.^2; bincenters=[0.5:0.5:4]'; bins=length(bincenters); h=zeros(bins,1); for i=1:length(Mag_square) for k=1 :bins %close previously opened wndow %clear data in worksapce %initialize guassian random number generator %generate complex numbers %magnitude of complex numbers %probability density function's width %square of magnitude %specify bins center positions %total no of bins %generate 0's vector %assigning corresponding values to bins

if Mag_square(i)>bincenters(k)-0.5/2 & Mag_square(i)<=bincenters(k)+0.5/2 h(k,1)=h(k,1)+1; end end end pxest=h/(1000*0.5); xaxis=[0:0.01:5]'; px=0.5.*exp(0.5.*-xaxis); bar(bincenters,pxest,0.5) hold on plot(xaxis,px,'r') legend('estimated','true') grid; title(' PDF for the Magnitude Squared of z i.e. |z|^2' ); xlabel('complex #s.'); ylabel('Estimated and True Probability'); %true probability %estimated probability

GRAPH:

ANS PROBLEM 3: MATLAB CODE:


close all clear all randn('state',0) X1=randn(3e3,1); X2=randn(3e3,1); Y1=X1+(0.1*X2); Y2=X1+(0.2*X2); Result=[Y1 Y2]; scatterplot(Result) title(' Scatter Plot For Y1 and Y2'); xlabel('Y1') %3e3 means 3000 random no's %close previously opened winodw %clear all data from workspace

ylabel('Y2')

GRAPH:

Conclusion:
If we let Y1=1 then we can say that Y2 is also approximately equal to 1, because if we see in the zoomed image of scatterplot in above figure we come to know that for Y1=1, the values of Y2 on vertical axis lie in the range [0.7,1.2] and the average of this interval is approximately equal to 1. So our conclusion is right.

You might also like