Why do I get assertion failed?

23 visualizzazioni (ultimi 30 giorni)
Amanda
Amanda il 26 Set 2017
Modificato: OCDER il 26 Set 2017
I am writing a function to calculate an estimate of pi for a specified number of significant digits. When I test it, it says assertion failed. I have been working on this for the past two days and am not sure what I am doing wrong.
function [estimate, N_terms] = PiEstimate(numsig)
% Input
% numsig: Required number of significant digits (positive integer, scalar)
% Output
% estimate result of sum series expansion
% N_terms number of sum terms used in the calculation.
n = 0;
F = 0;
while n < 100
sum = 4 * (((- 1) ^ n) / (2 * n + 1));
F = F + sum;
error_approximatePercent = (abs(sum) / F) * 100;
error_approximateRelative = abs(sum) / F;
%when using the precent error
numsigP = 2 - log10(2 * error_approximatePercent);
%when using the relative error
numsigR = - log10(2 * error_approximateRelative);
n = n + 1;
estimate = 4 * sum;
disp([n, numsigP, numsigR])
end

Risposte (1)

OCDER
OCDER il 26 Set 2017
Modificato: OCDER il 26 Set 2017
What is the assertion statement being used? Here're what I see is wrong / odd with your function:
  1. F is actually the pi value estimate, but it is not an output to your function
  2. numsig is not used in your function to check the significant digits
  3. N_terms is not calculated or used in your function
  4. numsigP and numsigR are not used after the while loop
  5. numsigP and numsigR are the same values
  6. estimate can be calculated in just the last loop. Ex:
n = 99;
sum = 4 * (((- 1) ^ n) / (2 * n + 1));
estimate = 4 * sum; % Equals -.0804 regardless of 100 iterations or just last iteration.
To determine significant figures, see this post here which talks about it:

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by