Matlab giving incorrect matrix multiplication :/
조회 수: 37 (최근 30일)
이전 댓글 표시
I have this code and I was calculating matrix multiplication and out of nowhere the answer of 2 cells was incorrect!!
if you multiply the last row with the second colum the answer is -0,005 which is not what is given here
I tried another solver online it gave me the right answer
>> invA
invA =
1.0000 -0.1500 0.0050
-0.1500 0.0650 -0.0030
0.0050 -0.0030 0.0002
>> A
A =
1 1 1
0 10 20
0 100 400
>> invA*A
ans =
1.0000 -0.0000 0.0000
-0.1500 0.2000 -0.0500
0.0050 -0.0100 0.0050
댓글 수: 4
답변 (2개)
John D'Errico
2023년 12월 21일
편집: John D'Errico
2023년 12월 21일
This is a common error I see novices to programming make. They do something strange, get something stranger yet, and immediately assume there is a problem in MATLAB. Instead, what they did was end up with some arrays where they have no clue what is in them, or how it got there.
A = [1 1 1
0 10 20
0 100 400];
The inverse of A is
Ainv = inv(A)
Note that Ainv is NOT a symmetric matrix, yet the matrix you have given us called invA, IS symmetric. Since A itslef is not symmetric, I would be highly surprised if the inverse was symmetric. You gave us:
1.0000 -0.1500 0.0050
-0.1500 0.0650 -0.0030
0.0050 -0.0030 0.0002
so a symmetric matrix, and claimed it was the inverse of A. WRONG. As you can see, the true inverse, Ainv, is not symmetric. And if you multiply A with the inverse I created here, we get an identity, but for some floating point trash in the "zero" elements.
format long g
Ainv*A
The solution is to be more careful about what you did. Don't jump to conclusions. Make sure you know EXACTLY what is in the arrays you generate. The problem does not lie in the solver you used, but in what you did.
댓글 수: 2
Steven Lord
2024년 1월 3일
A = [0.0050 -0.0030 0.0002 ];
B = [1; 10; 100];
result = A*B
Looks correct to me.
I suspect you're assuming it's incorrect because == doesn't say that it's exactly equal to minus five one-thousandths.
result == -0.005
This behavior is a consequence of floating point arithmetic. See this Answers post and the "Avoiding Common Problems with Floating-Point Arithmetic" section of this documentation page for more information.
If you are using the == operator to attempt to locate a floating-point number in an array, instead subtract the number you're trying to find from the numbers in the array and locate those positions where the difference is smaller than some tolerance or use the ismembertol function.
x = 0:0.1:1
It appears that x contains the value 0.3, but it does not contain exactly 0.3.
checkWithExactEquality = x == 0.3
It does contain a value that is extremely close to 0.3, however.
tolerance = 1e-15;
checkWithTolerance = abs(x-0.3) < tolerance
whichValueTolerance = x(checkWithTolerance)
How far away from 0.3 is the value we found using a tolerance?
howDifferent = whichValueTolerance - 0.3
To do the same with ismembertol:
checkWithIsmembertol = ismembertol(x, 0.3, tolerance)
whichValueIsmembertol = x(checkWithIsmembertol)
The ismembertol function found the same value that the check with a tolerance did.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!