Question

In: Math

(1)Using the Matlab code developed in Software Assignment #1: a. Convert the code that generates the...

(1)Using the Matlab code developed in Software Assignment #1:

a. Convert the code that generates the random number (H,T) with equal probabilities into a function called myBernolli(p, S) that takes as an input the probability of success p and S is the outcome defined as success (either T or H) and returns the outcome of the trial (either T or H).

b. Test that your function is actually producing the successful outcome with probability p by running the function in a loop of 1000 trials and counting how many times success is produced (it should be close to p*1000).

c. Write a Matlab function called myBinomial(n,p) that takes as an input the total number of trials n, the probability of success p and the outcome defined as success S and uses myBernolli() to return as an output the number of successes x.

d. Write a Matlab function called myGeometric() that takes as an input the probability of success p and the outcome defined as success S and uses myBernolli() to return as an output the number of trials till first success x.

e. Verify that myBinomial() and myGeometric() generates values that follow Binomial and Geometric Distributions by running each of them 5000 times in a loop and plotting a histogram of the random variable x generated from each.

Hints: Random numbers with probability p are generated in Matlab using the command function rand(). Read the help on Matlab to know how to use the function by typing help rand in Matlab command line. Histogram plots [hist()] is a function in Matlab. Read its help to know how to produce

Solutions

Expert Solution

ans :

code to copy::

a. bernoulis:

%Bernoulli’s function

function trialOutcome=myBernolli(p,S)

x=rand(1,1); %generate uniform random value from 0 to1

%check if random value is LESS THAN probability

%if so assign triout with 1

triout=(x<=p);

if triout==S

t=1;

else

t=0;

end

%assign to output variable

trialOutcome=t;

end

b. running Bernoulis function:

cnt=0;

for nn=1:1000 %1000 trials

%call function to check success outcome

t=myBernolli(0.56,1);

%if success outcome then increment the counter

if(t==1)

cnt=cnt+1;

end

end

%display the number of success outcome for given probability

disp("no of times success produced:");

disp(cnt);

c.binomial distribution:

%Bernoulli’s function

function trialOutcome=myBernolli(p,S)

x=rand(1,1); %generate uniform random value from 0 to1

%check if random value is less than probability

%if so assign triout with 1

triout=(x<=p);

if triout==S

t=1;

else

t=0;

end

%assign to output variable

trialOutcome=t;

end

%function for binomial distribution

function x=myBinomial(n,p)

ns=0; %counter for number of successes

S=0; %set success outcome

for kk=1:n %till said trials

if(tt==1) %if SUCCESS OUTCOME

end %if end

end%for end

x=ns; %output variable for number of success

end %end function

d. geometric distribution:

function x=myGeometric(p,S)
ns=0; %counter for number of successes
while 1 %till first success
tt=myBernolli(p,S);
if(tt==1) %if SUCCESS OUTCOME
ns=ns+1;
break;
else
ns=ns+1;
end
end
x=ns; %output variable
end %end function

e. plot:

h1=zeros(1,5000);
%to store return value from Geometric function
h2=zeros(1,5000);
for nn=1:5000
h1(nn)= myBinomial(100,0.25);
h2(nn)= myGeometric(0.25,1);
end
h1
h2
hist(h1);
hist(h2);


Related Solutions

Project 10-1 Convert lengths In this assignment, you’ll add code to a form that converts the...
Project 10-1 Convert lengths In this assignment, you’ll add code to a form that converts the value the user enters based on the selected conversion type. The application should handle the following conversions: From To Conversion Miles - Kilometers: 1 mile = 1.6093 kilometers Kilometers - Miles: 1 kilometer = 0.6214 miles Feet - Meters: 1 foot = 0.3048 meters Meters - Feet: 1 meter = 3.2808 feet Inches - Centimeters: 1 inch = 2.54 centimeters Centimeters - Inches: 1...
The following code must be written using matlab How to loop through a vector in matlab...
The following code must be written using matlab How to loop through a vector in matlab and assigning a value to every 4th entry. The vector could be of any length. Thanks
Design Rectangular array antenna in HFSS software or 5Ghz frequency and MATLAB CODE FOR IT. need...
Design Rectangular array antenna in HFSS software or 5Ghz frequency and MATLAB CODE FOR IT. need all the output waveform regarding that Design microstrip array antenna in HFSS software and MATLAB CODE FOR IT. IF YOU KNOW THEN ONLY DO OR ELSE LEAVE FOR OTHER ANTENNA ,ELECTRICAL ENGINEERING
Using newer versions of MATLAB, please write the complete code for: 1. Assume Y is an...
Using newer versions of MATLAB, please write the complete code for: 1. Assume Y is an exponential random variable with rate parameter λ=2. (1) Generate 1000 samples from this exponential distribution using inverse transform method (2) Compare the histogram of your samples with the true density of Y.
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft...
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft and dft. Applied to discrete signals. If you can with an example.Thank you!!
I want the code for the 2D Ising model using Matlab
I want the code for the 2D Ising model using Matlab
Matlab code problems I have to write a code using functions to find out if a...
Matlab code problems I have to write a code using functions to find out if a year is a leap year. I'm on the write track i feel like but I keep getting an error message and matlab isnt helping to troubleshoot. The issue is on line 30. Here is my code: function project_7_mfp() %   PROJECT_7_ABC   project_7_abc() creates a function that prompts the user %   enter a year after 1582 It then determines if it is a leap year %...
Design microstrip circular array antenna in HFSS software and MATLAB CODE FOR IT. Design microstrip circular...
Design microstrip circular array antenna in HFSS software and MATLAB CODE FOR IT. Design microstrip circular array antenna in HFSS software and MATLAB CODE FOR IT. IF YOU KNOW THEN ONLY DO OR ELSE LEAVE FOR OTHER ANTENNA ,ELECTRICAL ENGINEERING
How to design FIR lowpass filter using matlab. Note : Do not write matlab code. Only...
How to design FIR lowpass filter using matlab. Note : Do not write matlab code. Only explain the steps of designing filter
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT