MATLAB Why Does MATLAB Give the Wrong Result for This Function?

  • Thread starter Thread starter knowLittle
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion revolves around a MATLAB function defined as f(x) = exp(-x) * cos(2*x), which is plotted over the interval [0, pi]. The user encounters an unexpected output when evaluating the function at x = pi/4, receiving a result of approximately 2.7918e-017 instead of the expected zero. The response clarifies that this output is effectively zero within the limits of double precision floating point arithmetic, explaining that 2.7918e-017 is a very small number, close to the machine epsilon. This value is significantly smaller than the minimum representable number in double precision, highlighting the nuances of floating point calculations and the IEEE754 standard for numerical representation in computing.
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?
 
<br /> 2.7918 \times 10^{-17} = 1.000000011_{2} \times 2^{-55}<br />
 
Last edited:
It's 2.7918\times10^{-17}, 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.
 
Thank you.
 
If you're interested in floating point arithmetic, most computers conform to the IEEE754 standard.
 
Thank you, I'll take a look.
 

Similar threads

Replies
1
Views
2K
Replies
8
Views
2K
Replies
10
Views
3K
Replies
3
Views
2K
Replies
8
Views
3K
Replies
1
Views
2K
Replies
10
Views
3K
Back
Top