Question
% Script name: EE380_Lab0.m % Sam Jalali, Sept. 7th, 2018 % Simulate the probability of Red and Blue balls taken from a box of 3
% Script name: "EE380_Lab0.m"
% Sam Jalali, Sept. 7th, 2018
% Simulate the probability of Red and Blue balls taken from a box of 3 reds % and 5 blues. Demonstrate how the frequency approach results become closer
% to the expected theoretical value as the number of trials increases.
clearvars;
rng('shuffle');
N = 100; % initial number of trials
M = 6; % number of experiments (frequency)
for m = 1:M
N = 10 * N; % new number of trials
RedCNT = 0;
BlueCNT = 0;
% the trial loop for
n = 1:N
if (rand * 8) < 3
RedCNT = RedCNT + 1;
else BlueCNT = BlueCNT + 1;
end
end
NumTrials(m) = N;
ProbRed(m) = RedCNT / N;
ProbBlue(m) = BlueCNT / N;
end
% fprintf(' The probability of Red is %8.5f ', ProbRed);
% fprintf(' The probability of Blue is %8.5f ', ProbBlue);
subplot(2,1,1);
% Plot the probability of Red versus the number of trials
semilogx(NumTrials, ProbRed, '-bs','LineWidth', 2.0);
grid on; line([NumTrials(1),NumTrials(end)], [3/8,3/8]);
xlabel('number of trials'); title('probability');
subplot(2,1,2);
% Plot the probability of Red versus the number of trials
semilogx(NumTrials, ProbBlue, '-rs','LineWidth', 2.0);
grid on; line([NumTrials(1),NumTrials(end)], [5/8,5/8]);
xlabel('number of trials'); title('probability');
Write this script as a function with input: initial number of trials, and number of experiments, Outputs: arrays of number of trials, probability of red and blue. Call the function from a main script in which the plots are made.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started