Matlab's numeric solution to det of Matrix is incorrect

In summary: I would recommend checking your code against the Mathematica source to see if there are any errors there.
  • #1
Thanos_ST
2
4
TL;DR Summary
Equation solved numerically with vpasolve does not provide accurate solution. The solution is extracted from a PhD Thesis and was verified with Mathematica's numeric solver.
Hello everyone,

I have an equation derived as the det of a matrix, which I have solved in Mathematica 11 with Findroot and verified with the respective PhD Thesis' data. However, I now try to get more accustomed to Matlab (R2017b) and hence I tried to reproduce the problem and find its solution at this platform with vpasolve.
To my surprise, the Matlab solution is different! I also saw the plot, and it differs as well from the one Mathematica produces (which matches the one on the PhD Thesis). I even specify the solution of Mathematica to define the area (else the initial solution of Matlab is vastly different, see below).

So my code is the following:
Code:
syms L

B = [(-cos(L)+cosh(L))/(2*L^2) (-sin(L)+sinh(L))/(2*L^3); (-L*sin(L)+L*sinh(L))/(2*L^2) (-L*cos(L)+L*cosh(L))/(2*L^3)]

vpasolve (det(B) == 0, L, 4.7000) % L=4.73004 is the initial solution derived from Mathematica %

fplot (det(B), [-10, 10])

And the solution I get is 3.9407331356929149250770292025221 (instead of 4.73004). If I do not specify the area of the initial solution, the answer is -226.94495142003040084517833499812.

What am I doing wrong?

Thank you for reading my post.
 
Last edited:
  • Wow
Likes Wrichik Basu
Physics news on Phys.org
  • #2
Why not post this on the Matlab website so they can look at it?

You're using Matlab 2017 not their latest product so even if there is an error and it doesn't happen on the latest Matlab they will not likely change it. They may test it themselves on the latest product and then again maybe not.
 
  • #3
Something interesting is going on here. I could reproduce it on R2020b. Using solve returns the same as vpasolve without an initial condition. I don't think there is an error in MATLAB; probably we are not doing something correctly.

@Thanos_ST, Please let us know if you post it to MATLAB Central. I am interested in this.
 
  • #5
Wrichik Basu I posted it on MATLAB central, per jedishrfu's suggestion.
EDIT: not on the one you found, I posted it on a different topic. Also, solve does not produce any solution for me.
 
  • #6
How many solutions of your equation are there? Are you sure that your choice of initial guess will converge to the solution you are looking for?

EDIT: It should be straightforward to plot a graph of [itex]\det B(L)[/itex] against [itex]L[/itex] and see what's going on. This is always a necessary first step if you need to select an initial value for an iterative method; otherwise you may end up at a different solution than the one you expect.

EDIT: I see you did plot it.

Digging further at https://uk.mathworks.com/help/matlab/ref/det.html I found this:
Limitation: det uses the LU decomposition to calculate the determinant, which is susceptible to floating-point round-off errors.
Result: The determinant calculation is sometimes numerically unstable. For example, det can produce a large-magnitude determinant for a singular matrix, even though it should have a magnitude of 0.

However, Wolfram Alpha's symbolic calculation of the determinant (here), appears to agree with your Matlab result; in particular there is no root near 4.7 (detail plot for L=3 to 5).

It does seem like it is the original Mathematica result which is wrong, or else you have made a copying error when converting it to Matlab.
 
Last edited:
  • Like
Likes jedishrfu

FAQ: Matlab's numeric solution to det of Matrix is incorrect

Why does MATLAB give an incorrect determinant for my matrix?

MATLAB might give an incorrect determinant due to numerical precision issues, especially for large or ill-conditioned matrices. Floating-point arithmetic can introduce small errors that accumulate, leading to inaccurate results.

How can I improve the accuracy of the determinant calculation in MATLAB?

To improve accuracy, consider using symbolic computation with the Symbolic Math Toolbox in MATLAB. This can provide exact arithmetic and avoid the pitfalls of floating-point errors.

Is there a specific matrix size or type that causes MATLAB to compute the determinant incorrectly?

Large matrices or matrices with a high condition number (ill-conditioned matrices) are more prone to numerical inaccuracies. These matrices amplify floating-point errors, leading to incorrect determinant values.

Can the data type of the matrix elements affect the determinant calculation in MATLAB?

Yes, the data type can affect the calculation. Using higher precision data types like 'double' instead of 'single' can reduce numerical errors. However, for exact arithmetic, symbolic data types are preferred.

Are there alternative methods to compute the determinant more accurately in MATLAB?

Yes, you can use the LU decomposition method to compute the determinant more accurately. By decomposing the matrix into lower and upper triangular matrices, you can calculate the determinant as the product of the diagonal elements of these matrices.

Similar threads

Replies
13
Views
2K
Replies
1
Views
2K
Replies
4
Views
2K
Replies
3
Views
2K
Replies
3
Views
6K
Replies
2
Views
1K
Replies
5
Views
2K
Replies
1
Views
1K
Replies
2
Views
1K
Back
Top