Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Strange Result Calculating pi.

  1. Sep 28, 2006 #1
    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>
     
  2. jcsd
  3. Sep 28, 2006 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You may be running out of machine precision. MATLAB doesn't use arbitrary-precision arithmetic.

    - Warren
     
  4. Sep 28, 2006 #3
    I'm not certain I know what machine precision is?
     
  5. Sep 28, 2006 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  6. Sep 28, 2006 #5

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Strange Result Calculating pi.
  1. Strange integral (Replies: 4)

  2. Strange Symbol (Replies: 1)

Loading...