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

Obvious Result Wrong in Matlab

  1. Apr 5, 2012 #1
    clf;
    clear all;
    syms x
    %t=linspace(0, pi);
    %x=t;

    f=@(x) exp(-x).*cos(2*x)

    h1=ezplot(f,[ 0, pi]); %plots the function f

    f(pi./4)

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    My output gives me:

    f =

    @(x)exp(-x).*cos(2*x)


    ans =

    2.7918e-017
    %%%%%%%%%%%%%%%%%
    But, the real answer is 0.

    I'm clueless. Please, help.
     
  2. jcsd
  3. Apr 5, 2012 #2

    jhae2.718

    User Avatar
    Gold Member

    Your result is equivalent to zero within the precision of double precision floating point arithmetic.
     
  4. Apr 5, 2012 #3
    Ok. Could you explain me, how to read 2.7918e-017?
     
  5. Apr 5, 2012 #4
    [tex]
    2.7918 \times 10^{-17} = 1.000000011_{2} \times 2^{-55}
    [/tex]
     
    Last edited: Apr 5, 2012
  6. Apr 5, 2012 #5

    jhae2.718

    User Avatar
    Gold Member

    It's [itex]2.7918\times10^{-17}[/itex], which is an order of magnitude less than the minimum number representable under double precision, which is of the order 10-16. This value is called the machine epsilon, and you can find it using the MATLAB eps command.
     
  7. Apr 5, 2012 #6
    Thank you.
     
  8. Apr 5, 2012 #7

    jhae2.718

    User Avatar
    Gold Member

    If you're interested in floating point arithmetic, most computers conform to the IEEE754 standard.
     
  9. Apr 5, 2012 #8
    Thank you, I'll take a look.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook