Why Does MATLAB Give the Wrong Result for This Function?

  • Context: MATLAB 
  • Thread starter Thread starter knowLittle
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB function that appears to yield an unexpected result when evaluated at a specific point. Participants explore the implications of floating-point arithmetic and precision in numerical computations.

Discussion Character

  • Technical explanation, Conceptual clarification

Main Points Raised

  • One participant reports that the output of the function at pi/4 is approximately 2.7918e-017, questioning why it is not zero.
  • Another participant explains that the result is effectively zero within the limits of double precision floating point arithmetic.
  • A request for clarification on how to interpret the notation 2.7918e-017 is made, prompting further explanation of its scientific notation.
  • Further clarification is provided that 2.7918e-017 can be expressed as 2.7918 × 10^{-17}, which is significantly smaller than the minimum representable number in double precision.
  • Participants mention the concept of machine epsilon and its relevance in floating-point arithmetic.
  • One participant notes that most computers adhere to the IEEE754 standard for floating-point representation.

Areas of Agreement / Disagreement

Participants generally agree on the interpretation of the output in the context of floating-point precision, but there is no explicit consensus on the implications of this for the original question regarding the function's output.

Contextual Notes

The discussion does not resolve the initial confusion regarding the function's output, as it focuses on the interpretation of numerical results rather than addressing potential issues in the function itself.

Who May Find This Useful

Individuals interested in numerical methods, floating-point arithmetic, or MATLAB programming may find this discussion relevant.

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.
 
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 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K