Why am I getting an error message on my code?

2 Ansichten (letzte 30 Tage)
Ilvar
Ilvar am 22 Mär. 2023
Kommentiert: VBBV am 23 Mär. 2023
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]);

Antworten (2)

Cameron
Cameron am 22 Mär. 2023
Try this instead
estPiValues = zeros(numTrials,1);

VBBV
VBBV am 23 Mär. 2023
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 Kommentar
VBBV
VBBV am 23 Mär. 2023
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by