Write a MATLAB program to simulate the following experiments
Solution
Before writing the program, like to explain the functions required for the program.
According to the program we need to generate a sequence of numbers so Loops are used in the program.
Loops: Loops are MATLAB constructs that permit us to execute a sequence of statements more than once.
For loop:
for k = expr // k is a loop variable and expr is the loop control expression,
body
end
Example:
for k = 1:5
x = k
end
So we use a loop format for writing a sequence of numbers, X:Y. This is just shorthand for make a list of numbers from 1 to 6 increasing by 1 each step
n = 1e3;
Number_Dice = 1;
Roll1 = NULL // This initiates the variable
Roll2 = NULL
For (i in 1:100)
{
roll1[i] = Roll1dice(1)
roll2[i] = Roll1dice(1)
}
Sum(roll1 == roll2)
print(i)
RF11 = cumsum(S==11)/(1:100);
plot(1:100,RF11) // Plot the values
hold on
RF12 = cumsum(S==12)./(1:100);
plot(1:n,RF12,\'r\')
xlabel(\'Number of Rolls\')
ylabel(\'Relative Frequency\')
legend(\'Sum = 11\', \'Sum = 12\') grid on
figure S_Support = (1*Number_Dice):(6*Number_Dice);
hist(S,S_Support) // to draw the histogram
N_S_Sim = hist((roll1+roll2),density = 100 , breaks = 1:12,prob =T); // Breaks says that bins to start at 1, end at 12.

