%%Function binomialPMF.m function px_k=binomialPMF(n,p) px_k=zeros(1,n+1); a=1;
ID: 3590024 • Letter: #
Question
%%Function binomialPMF.m
function px_k=binomialPMF(n,p)
px_k=zeros(1,n+1);
a=1;
for k=0:n
px_k(a)=(nchoosek(n,k))*((p^k))*((1-p)^(n-k));
a=a+1;
end
%function geometricPMF.m
function px_k=geometricPMF(kmax,p)
px_k=zeros(1,kmax);
for k=1:kmax
px_k(k)=((1-p)^(k-1))*p;
end
%%script file for plotting graphs
%1
n=40;p=.5;
plot(0:n,binomialPMF(n,p),'r')
hold on
p=.8;
plot(0:n,binomialPMF(n,p),'g')
n=80;.5;
hold on
plot(0:n,binomialPMF(n,p),'b')
legend('n=40 and p=.5','n=40 and p=.8','n=80 and p=.5')
xlabel('k')
ylabel('p_X(k)')
title("Binomial Probability Mass Function")
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%2
kmax=50;
figure
plot(1:kmax,geometricPMF(kmax,.3),'r')
hold on
plot(1:kmax,geometricPMF(kmax,.5),'g')
hold on
plot(1:kmax,geometricPMF(kmax,.8),'b')
legend('p=.3','p=.5','p=.8')
xlabel('k')
ylabel('p_X(k)')
title("Geometric Probability Mass Function")
i got the matlab code but its showing me error the function line someone plz help me fix this code and run it thanks u asap...
Explanation / Answer
In binomial PMF you just need to to add indentation on the for loop for the two statements in the body of the loop. Also you need to add an end after the last statement of the for loop. End marks the end of a block, this would show where the for loop ends.
For geometric PMF, you just need to indent the for loop body.
The first script doesn't give any error but warnings because you are calculating to the power 40. This is beyond the accuracy of the MatLab, but this won't affect your program that much. In title use single quotes instead of double quotes.
Again in the second script you just need to use single quotes instead of double quotes in title.
These are all the errors if anything else happens, do tell me.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.