Matlab: value non-zero but it should

  • MATLAB
  • Thread starter Rizlablack
  • Start date
  • Tags
    Matlab Value
In summary, the conversation discusses the reason for getting a non-zero result when calculating the determinant of a matrix in Matlab, even though it should be zero. It is explained that this is due to the limitations of representing numbers in binary on a computer, and that Matlab uses a general algorithm for calculating determinants. The possibility of using matrix decomposition and scaling tricks is also mentioned as a factor in the inexactness of the result.
  • #1
Rizlablack
8
0
Hello everybody!
I'm quite new with Matlab and I'm starting trying some stuff..
well I was trying to input

det(a)

where a=[1,2,3;4,5,6;7,8,9] and this should be zero. And I mean 0.0000
but the result is

6.6613e-16

why?o_O
 
Physics news on Phys.org
  • #2
I think it is because not all 10-base numbers can be represented exactly in the computer. You see, the computer works in binary and it has a finite number of bits to represent number, in other words, there is a given resolution between the two closest numbers that the computer can represent...so, when you get a number that cannot be represented exactly, it is going to be given the closest value the machine can represent...so, little by little, you start having an error.
 
  • #3
Matlab doesn't have a special-purpose determinant algorithm for 2x2 matrices, another for 3x3 matrices, another for 4x4 matrices, etc. It instead uses a general purpose algorithm that works for any NxN matrix.

Which algorithm?

I can't find Matlab implementation of det, but it definitely is not expansion of minors. That's an O(n!) algorithm. It probably uses some matrix decomposition, most likely LU decomposition:
  • Decompose the matrix as a product of a lower diagonal and upper diagonal matrix (LU decomposition).
  • Compute the product of the diagonals of the decomposed matrices.
  • Negate this product if an odd number of pivots were made in the LU decomposition.

Any decent matrix decomposition algorithm will use various scaling tricks to make the algorithm stable. While this scaling does make the algorithm stable, it also makes your exact numbers inexact. Hence the 6.6e-16 value for the determinant (which is essentially zero).
 
  • #4
thank you very much D H.. I had some clues that could be something like that but now I'm quite sure I'm not doing anything wrong!
 
  • #5


Hi there! It's great that you are exploring Matlab and trying new things. The reason you are getting a non-zero value for the determinant of a=[1,2,3;4,5,6;7,8,9] is because of the way computers handle floating point numbers. In Matlab, 0.0000 is not the exact value of zero, but rather a very small number close to zero. This is due to the limitations of representing numbers in a finite number of bits. Therefore, the value you are getting is a very small, but non-zero, number that is close to zero. This is a common issue in numerical computations and can be managed by using functions like "round" or "eps" to get a more accurate result. I hope this helps! Keep exploring and learning in Matlab. Good luck!
 

Related to Matlab: value non-zero but it should

1. Why am I getting a non-zero value in Matlab when it should be zero?

There are a few possible reasons for this. One common reason is that you may have a small rounding error due to the limitations of floating point arithmetic. Another possibility is that there is a bug in your code causing the unexpected non-zero value. It is important to carefully check your code and make sure all variables are assigned correctly.

2. How can I fix the non-zero value in Matlab?

If the non-zero value is due to a rounding error, you can try using the "eps" function to set a tolerance level for comparisons. If the issue is a bug in your code, you will need to carefully debug and fix the code to ensure that all variables are correctly assigned and calculations are performed accurately.

3. Can a non-zero value affect my results in Matlab?

Yes, even a small non-zero value can have an impact on your results. This is why it is important to carefully check your code and ensure all calculations are accurate. Using the "eps" function or other methods to handle rounding errors can also help improve the accuracy of your results.

4. Why is my code giving me different results in Matlab compared to other programming languages?

Matlab uses floating point arithmetic, which can sometimes lead to small differences in results compared to other languages that use different methods. It is important to understand the limitations of floating point arithmetic and make sure your code is properly accounting for these differences.

5. Is there a way to avoid non-zero values in Matlab?

While it is not always possible to completely avoid non-zero values in Matlab, there are some steps you can take to minimize their impact. These include using the "eps" function, carefully checking your code for bugs, and using appropriate data types for your calculations. It is also important to have a good understanding of floating point arithmetic and its limitations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
917
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top