Why am I getting an error message on my code?

1 次查看(过去 30 天)
function [estPiAve, estPiStD] = estPiStats(numThrows,numTrials)
estPiValues = zeros([numTrials 1]);
% populate the estPiValues vector
for i = 1:numTrials
estPiValues(i) = estPi(numThrows);
end
estPiAve = mean(estPiValues);
estPiStD = std(estPiValues);
plot(estPiValues,'o')
hold on
plot([1, numTrials], pi^[-1, 1],'k');
plot([1, numTrials], (estPiAve+1.96^estPiStD)^[-1, 1],'--k');
plot([1, numTrials], (estPiAve-1.96^estPiStD)^[-1, 1]),'--k';
hold off
end
Not enough input arguments.
Error in estPiStats (line 3)
estPiValues = zeros([numTrials 1]);

回答(2 个)

Cameron
Cameron 2023-3-22
Try this instead
estPiValues = zeros(numTrials,1);

VBBV
VBBV 2023-3-23
numThrows = 10;
numTrials = 30;
[estPiAve, estPiStD] = estPiStats(numThrows,numTrials) % provide all input arguments when calling function
estPiAve = 0.5101
estPiStD = 0.2798
function [estPiAve, estPiStD] = estPiStats(numThrows,numTrials)
estPiValues = zeros([numTrials 1]);
% populate the estPiValues vector
for i = 1:numTrials
estPiValues(i) = rand(1); %estPi(numThrows);
end
estPiAve = mean(estPiValues);
estPiStD = std(estPiValues);
plot(estPiValues,'o')
hold on
plot([1, numTrials], pi.^[-1, 1],'k');
plot([1, numTrials], (estPiAve+1.96^estPiStD).^[-1, 1],'--k');
plot([1, numTrials], (estPiAve-1.96^estPiStD).^[-1, 1]),'--k';
hold off
end
  1 个评论
VBBV
VBBV 2023-3-23
if you dont provide all input arguments to the function, then it results in such error
numThrows = 10;
numTrials = 30;
[estPiAve, estPiStD] = estPiStats(numThrows) % if you dont provide enough input arguments it will throw such error
Not enough input arguments.

Error in solution>estPiStats (line 5)
estPiValues = zeros([numTrials 1]);
function [estPiAve, estPiStD] = estPiStats(numThrows,numTrials)
estPiValues = zeros([numTrials 1]);
% populate the estPiValues vector
for i = 1:numTrials
estPiValues(i) = rand(1); %estPi(numThrows);
end
estPiAve = mean(estPiValues);
estPiStD = std(estPiValues);
plot(estPiValues,'o')
hold on
plot([1, numTrials], pi.^[-1, 1],'k');
plot([1, numTrials], (estPiAve+1.96^estPiStD).^[-1, 1],'--k');
plot([1, numTrials], (estPiAve-1.96^estPiStD).^[-1, 1]),'--k';
hold off
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Filter Analysis 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by