Coin Toss and Level crossing problem
OBJECTIVE
- To understand and explore random process simulation
LEARNING OUTCOMES
- After the completion of this experiment students will be able to Simulate random processes and their statistics
SOFTWARE USED:
MATLAB® R2017a
1. Simulate a coin toss that maps a head as 1 and
tail as 0.
2. Toss the coin N = 100, 500,1000, 5000 and
500000 times and compute the probability (p) of head in each case.
3. Compute the absolute error |0.5 − p| in
each case and plot against N and understand the law of large numbers.
4. Create a uniform random vector with
maximum magnitude 10, plot and observe.
5. Set a threshold (VT = 2) and count how
many times the random function has crossed VT .
6. Count how many times the function has
gone above and below the threshold.
PROGRAM
clc;
clear all;
close all;
n=[10 100 500 1000 5000
10000 500000];
for i=1:7
heads=0;
tail=0;
disp('value of n is')
disp(n(i))
for cointoss=1:n(i)
flip=int32(randi([0, 1]));
if flip==1
toss_result(cointoss)=flip;
heads=heads+1;
else
toss_result(cointoss)=flip;
tail=tail+1;
end
end
disp('no. of heads is')
disp(heads);
disp('no. of tail is')
disp(tail);
disp('probability of heads
is')
Prob_H=heads/n(i)
disp('probability of tail
is')
Prob_T=tail/n(i)
ab_err(i) = abs(0.5 -
Prob_H);
disp('absolute error')
disp(ab_err(i))
end
plot(n,ab_err,'linewidth',2);
title('Absolute error Vs N')
xlabel('n')
ylabel('ab_err')
OUTPUT
PROGRAM
clc;
clear all;
close all;
r=randi(10,[1,100]);
plot(r,'linewidth',2)
hold on;
T=2; %threshold
for i=1:100
Threshold(i)=T;
end
plot(Threshold,'linewidth',2);
hold on;
disp('Number of values
crossed threshold')
sum(r>=T)
disp('Number of values under
threshold')
sum(r<T)
OUTPUT
Comments
Post a Comment