I have a code but it keep saying "invalid expression. when calling a function or indexing a variable, use parentheses. otherwise check for mismatched delimiters
1 次查看(过去 30 天)
显示 更早的评论
The was working fine and showing the charts and all
prob = RollingDice()
function prob = RollingDice(~) % calculate the probability...
% of obtaining a # combination...
% between 1 and 12
n = 12; % total number of side between both dice
TNrolls = 0;
prob = zeros(1,12); % define a probability between 1 and 12
while TNrolls < n % total is greater than 1 in order to...
% get a number bigger than 0
dice(1) = randi(6); % the number of sides
dice(2) = randi(6);
Sum = dice(1) + dice(2); % sume represents the...
% the #'s from each dice
TNrolls = TNrolls + 2; % for every roll, we add +2...
% to the total for the # of dice
if Sum == 2
prob(2) = TNrolls/n; % depending on the # of roll it takes...
% obtain a sum of 2, is divides by total
% number of possible sides = 12
elseif Sum == 3
prob(3) = TNrolls/n;
elseif Sum == 4
prob(4) = TNrolls/n;
elseif Sum == 5
prob(5) = TNrolls/n;
elseif Sum == 6
prob(6) = TNrolls/n;
elseif Sum == 7
prob(7) = TNrolls/n;
elseif Sum == 8
prob(8) = TNrolls/n;
elseif Sum == 9
prob(9) = TNrolls/n;
elseif Sum == 10
prob(10) = TNrolls/n;
elseif Sum == 11
prob(11) = TNrolls/n;
elseif Sum == 12
prob(12) = TNrolls/n;
end
end
bar(prob)
xlabel('Sum for Roll')
ylabel('Frequency')
title(['Number of Rolls: ',num2str(Sum)])
end
1 个评论
回答(1 个)
Voss
2023-2-16
编辑:Voss
2023-2-16
It runs without error and shows a plot.
Probably there's a syntax error in a nearby part of your code.
prob = RollingDice()
function prob = RollingDice(~) % calculate the probability...
% of obtaining a # combination...
% between 1 and 12
n = 12; % total number of side between both dice
TNrolls = 0;
prob = zeros(1,12); % define a probability between 1 and 12
while TNrolls < n % total is greater than 1 in order to...
% get a number bigger than 0
dice(1) = randi(6); % the number of sides
dice(2) = randi(6);
Sum = dice(1) + dice(2); % sume represents the...
% the #'s from each dice
TNrolls = TNrolls + 2; % for every roll, we add +2...
% to the total for the # of dice
if Sum == 2
prob(2) = TNrolls/n; % depending on the # of roll it takes...
% obtain a sum of 2, is divides by total
% number of possible sides = 12
elseif Sum == 3
prob(3) = TNrolls/n;
elseif Sum == 4
prob(4) = TNrolls/n;
elseif Sum == 5
prob(5) = TNrolls/n;
elseif Sum == 6
prob(6) = TNrolls/n;
elseif Sum == 7
prob(7) = TNrolls/n;
elseif Sum == 8
prob(8) = TNrolls/n;
elseif Sum == 9
prob(9) = TNrolls/n;
elseif Sum == 10
prob(10) = TNrolls/n;
elseif Sum == 11
prob(11) = TNrolls/n;
elseif Sum == 12
prob(12) = TNrolls/n;
end
end
bar(prob)
xlabel('Sum for Roll')
ylabel('Frequency')
title(['Number of Rolls: ',num2str(Sum)])
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Naming Conventions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!