Need help finding error in code
Show older comments
I have attached the m-file.
3 Comments
KSSV
on 22 Nov 2016
You should attach the code, not screen shot.
Walter Roberson
on 22 Nov 2016
You should also include a copy of the error message.
James Cook
on 22 Nov 2016
Answers (1)
KSSV
on 22 Nov 2016
clc; clear all ;
% The purpose of this script is to compute the probability of Paula
% winning a game in the ECE103 tennis assignment.
% Givens: -Paula has won 154/230 points when she serves
% -the scoring rules of tennis
% Plan: simulate a large number of tennis games, where Paula has a
% 154/230 chance of winning each point
%%Inputs
f_Paula = 154/230; % The fraction indicating the chance of Paula winning a point
n_games = 5; % the number of games you want to simulate
points_to_win = 7; % the number of points required to win a game
%%declare initial conditions
games_P = 0; % number of games that Paula has won
games_A = 0; % number of games that Aaron has won
tot_points_P = 0; % total number of points Paula won
tot_points_A = 0; % total number of points Aaron won
%%run simulations, one game at a time
for gg = 1:n_games % simulate the number of games requested
% at start of each game, change Paula and Aaron's scores to 0
score_P = 0;
score_A = 0;
game_over = 0; % this will change to 1 once the game is over
% keep simulating game if it is in't over
while game_over == 0
prob = rand(); % randomly decide if Paula wins the point
if rand<f_Paula
score_P = score_P + 1; % if Paula wins, add 1 to her score
tot_points_P = tot_points_P + 1;
else
score_A = score_A + 1; % if Paula loses, add 1 to Aaron's score
tot_points_A = tot_points_A + 1;
end
% decide if the game is over (someone has scored at least 4 points
% and is winning by at least 2)
max_score = max(score_P, score_A); % see what the leading score is
score_diff = abs(score_P-score_A); % see what the score difference is
game_over = (max_score>=points_to_win && score_diff>=2); % is 0 if game should continue, 1 if it should end
end
% once game is over, tally up who won the game
if score_P > score_A
games_P = games_A + 1; % if Paula wins, add to her total games won
else
games_A = games_A + 1; % if Aaron wins, add to his total games won
end
end
%%Wite out the number of games Paula wins
perc_P = 100*games_P/n_games;
% write_out = ['Paula won ',num2str(perc_P),'% of ',num2str(n_games),' games played'];
% disp(write_out)
fprintf('Paula won %f of %d games played\n',perc_P,n_games);
%%Wite out the number of points Paula wins
tot_points = tot_points_P + tot_points_A;
perc_P = 100*tot_points_P/tot_points;
% write_out = ['Paula won ',num2str(perc_P),'% of ',num2str(tot_points),' points played'];
% disp(write_out)
fprintf('Paula won %f of %d games played\n',perc_P,tot_points);
Categories
Find more on Strategy & Logic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!