Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the Matlab code developed in Software Assignment #1: a. Convert the code t

ID: 3768756 • Letter: U

Question

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 success 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 follows

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

the requirements in part e.

Explanation / Answer

Here you go:

Part A:

function [o] = myBernoulli(p,s)
% Matlab implementation of velocity function
%
%
% input: p : probability
%
% s : success
%
% output: o : outcome of success
%
%

n = 1;
U = rand(1, n);
if (s == 1)
toss = (U < p);
else
toss = (U > p);
end
a = zeros(n + 1);
avg = zeros(n);
for i = 2 : n + 1
a(i) = a(i - 1) + toss(i - 1);
avg(i - 1) = a(i)/(i - 1);
o = avg(i-1);
end

Part B:

clc;
clear all;
close all;

count = 0;
s = 1;
for i = 1:1000
o = myBernoulli(0.2,s);
if (o == s)
count = count + 1;
end
end

Part C :

function [x] = myBinomial(n,p,s)
% Matlab implementation of velocity function
%
%
% input: p : probability
%
% n : number of trials
%
% S : success
%
%
% output: x : number of successes
%

x = 0;
for i = 1:n
o = myBernoulli(p,s);
if (o == s)
x = x + 1;
end
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote