Why Does MATLAB Give the Wrong Result for This Function?

  • Context: MATLAB 
  • Thread starter Thread starter knowLittle
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 3K views
knowLittle
Messages
307
Reaction score
3
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.
 
Physics news on Phys.org
Your result is equivalent to zero within the precision of double precision floating point arithmetic.
 
Ok. Could you explain me, how to read 2.7918e-017?
 
[tex] 2.7918 \times 10^{-17} = 1.000000011_{2} \times 2^{-55}[/tex]
 
Last edited:
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.
 
If you're interested in floating point arithmetic, most computers conform to the IEEE754 standard.
 
Thank you, I'll take a look.