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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!