Obvious Result Wrong in Matlab

  • MATLAB
  • Thread starter knowLittle
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses the result of a function and its equivalent value in double precision floating point arithmetic. The value given is 2.7918e-017, which is an order of magnitude less than the minimum number representable under double precision. This value is called the machine epsilon. The conversation also mentions the IEEE754 standard for floating point arithmetic.
  • #1
knowLittle
312
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
  • #2
Your result is equivalent to zero within the precision of double precision floating point arithmetic.
 
  • #3
Ok. Could you explain me, how to read 2.7918e-017?
 
  • #4
[tex]
2.7918 \times 10^{-17} = 1.000000011_{2} \times 2^{-55}
[/tex]
 
Last edited:
  • #5
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.
 
  • #6
Thank you.
 
  • #7
If you're interested in floating point arithmetic, most computers conform to the IEEE754 standard.
 
  • #8
Thank you, I'll take a look.
 

1. Why is my result in Matlab different from what I expected?

There could be several reasons for this. It could be due to a mistake in your code, incorrect input values, or a misunderstanding of the mathematical algorithm. It is important to carefully check your code and inputs to identify the source of the discrepancy.

2. How can I troubleshoot when my Matlab result is wrong?

First, try to identify the specific line of code that is causing the issue. Use the debugging tools in Matlab to step through your code and check the values of variables. Also, try to break down your code into smaller parts and test each part individually to isolate the problem.

3. Is it possible for Matlab to give incorrect results?

Yes, it is possible. Matlab is a powerful tool, but it is still prone to human error and bugs. It is important to carefully check your code and inputs and to validate your results using other methods or tools.

4. How do I prevent errors in my Matlab code?

One way to prevent errors is to write clear and organized code. Use comments to explain your thought process and logic, and use variable names that are easy to understand. It is also important to test your code with different inputs and to validate your results.

5. What should I do if I am still getting incorrect results in Matlab?

If you have exhausted all troubleshooting methods and are still getting incorrect results, it may be helpful to seek advice from a more experienced Matlab user or consult the documentation for the specific function or algorithm you are using. It may also be helpful to take a step back and reassess your approach to the problem.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
212
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
985
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top