- F is actually the pi value estimate, but it is not an output to your function
- numsig is not used in your function to check the significant digits
- N_terms is not calculated or used in your function
- numsigP and numsigR are not used after the while loop
- numsigP and numsigR are the same values
- estimate can be calculated in just the last loop. Ex:
Why do I get assertion failed?
23 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (1)
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:
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:
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!