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

  • Context: Undergrad 
  • Thread starter Thread starter quasi426
  • Start date Start date
  • Tags Tags
    Pi Strange
Click For Summary

Discussion Overview

The discussion revolves around the numerical calculation of pi using MATLAB, specifically addressing issues encountered when using very small step intervals in a for loop. Participants explore the implications of machine precision and rounding errors in numerical computations.

Discussion Character

  • Technical explanation, Debate/contested

Main Points Raised

  • One participant reports obtaining absurd results for pi when using very small intervals in MATLAB, suggesting that intervals larger than about 1e10 lead to errors, while 1e6 works correctly.
  • Another participant proposes that the issue may be related to running out of machine precision, noting that MATLAB does not utilize arbitrary-precision arithmetic.
  • A participant expresses uncertainty about the concept of machine precision.
  • Further clarification is provided regarding IEEE standard double-precision floating point numbers, explaining their limitations in representing very small or very large numbers and the potential for rounding errors.
  • One participant states that increasing the number of terms for greater accuracy leads to accumulated errors, particularly from round-off error.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding machine precision and its effects on numerical calculations. There is no consensus on the best approach to resolve the issues raised, and multiple viewpoints on the implications of machine precision and rounding errors are present.

Contextual Notes

The discussion highlights limitations related to machine precision in MATLAB and the potential for rounding errors, but does not resolve the specific causes of the absurd results reported.

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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K