Question

USING MATLAB Write a MATLAB function that simulates a single roll of a n-sided die. The...

USING MATLAB

Write a MATLAB function that simulates a single roll of a n-sided die.

The inputsand outputs of the function are:

Inputs:

•The probabilities for each side, given as a vector p = [p1,p2...pn]

Outputs:

•The number onthe face of the die after a single roll, i.e.onenumber fromthe setof integers

{1, 2 ,....n}

Note: The sum p1+p2+.....pn must be equal to 1.0, otherwise the probability values are incorrect.

Savethe function as: nsided_die(p)

Test the functionwitha 5-sided die,where the probabilities are given by the vector p=[0.10, 0.25, 0.30, 0.15, 0.20]

Test the functionwitha 5-sided die,where theprobabilities are given by the vector p=[0.10, 0.25, 0.30, 0.15, 0.20]

To test the function, roll the die for N=10,000times and plot the outcome as a stem plot.

Please explain each line

Thank you

Homework Answers

Answer #1

10000 times!!!!!!! nothing will be visible if we plot for this many larg times..so I limited the code to 100 time instead of 10000 times...still if you want result for 10000 times then simply change 100 to 10000 in the code.....

code:::

clc;
close all;
clear all;
n=input('how many sided is youu die is');
disp('enter the probabilities of each side')
for i=1:n
p(i)=input('');
num(i)=p(i)*100;
end
l=[];
a=cumsum(p);
if a(n)==1
for i=1:n;
m=i*ones(1,num(i));
l=[l m];
end
for i=1:100
x=randsample(1:length(l),1);
stem(i,l(x),'*');
hold on
end
else
disp('probability values are incorrect');
end

corresponding plot::::


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT