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

USING MATLAB USING MATLAB numbers. The probability, P, of guessing m numbers out

ID: 2259526 • Letter: U

Question

USING MATLAB

USING MATLAB

numbers. The probability, P, of guessing m numbers out of the r numbers can be calculated by the expression: mr-m n! Where-In this problem, we want to write a user-defined MATLAB function that calculates P. r)rn-r)! (a) The factorial n! of a positive integer is defined by n!=n(n-1)(n-2)--21, where 01-1. Write a user-defined function y = myfactfn(n) that calculates the factorial n!. The function should display an error message if a negative number is entered. Do not use MATLAB built-in function factorial. (b) Write a user-defined function y-myBinom (n,r) that calculates n! Use myfactfn as a subfunction inside myBinom(n,r) for r) r(n-r)! calculating factorial n!. (c) Write a user-defined function P LotteryProb (m,r,n).The input arguments are m the number of correct guesses; r, the number of numbers that need to be guessed; n, the number of numbers available. Use myBinom as a subfunction inside LotteryProb for calculating binomial coefficient (d) Use Lotteryprob to calculate the probability of correctly selecting 2 of 5 the numbers that are drawn out of 30 numbers in a lottery game.

Explanation / Answer

a)

%%% matlab function

function y = myfactfn(n)
if (n<0)
disp('Error : factorial of negative number is not possible ');
else
if (n==0)
y=1;
else
y=1;
for i=1:n
y=y*i;
end
  
end
end
end

b)

%%% matlab function

function y = myBinom(n,r)
y=myfactfn(n)/(myfactfn(r)*myfactfn(n-r));
end

c)

function p=LotteryProb(m,r,n)
A=myBinom(r,m);
S=myBinom(n,r);
p=A/S;
end

d)

clc;

p=LotteryProb(2,5,30)

OUTPUT:

p =

7.0172e-05