Why Does Calculating Pi with Small Steps Yield Absurd Results in MATLAB?

  • Thread starter Thread starter quasi426
  • Start date Start date
  • Tags Tags
    Pi Strange
quasi426
Messages
207
Reaction score
0
I decided to numerically calculated pi using MATLAb and running a for loop. When I make the steps too small I get absurd quantities for pi. Here see for yourself. When I make the interval higher then about 1e10 i get errors, but 1e6 works ok. Why would this be? Thanks

<code>


clc
clear all

interval = 1e11;
pi_calculated = 0;

for i = -1: 2/interval :1
pi_calculated = sqrt(1-i^2)*2/interval + pi_calculated;
end

format long
pi_calculated = 2*pi_calculated


<code>
 
Physics news on Phys.org
You may be running out of machine precision. MATLAB doesn't use arbitrary-precision arithmetic.

- Warren
 
I'm not certain I know what machine precision is?
 
If MATLAB is using, e.g. IEEE standard double-precision floating point numbers, which are 64 bits wide, they cannot contain arbitrarily small nor arbitrarily large numbers. If you try to use a very, very, very small number, it will be rounded off, because the machine (computer) does not have the ability to represent it.

Other systems (like Mathematica) are capable of arbitrary-precision arithmetic by default; MATLAB may or may not support arbitrary-precision arithmetic, but I'm pretty certain it is not enabled by default.

- Warren
 
Nothing surprising about it. The more terms you use in order to increase accuracy, the more calculations you need to do and the errors tend to accumulate- especially from round off error.
 
Back
Top