Matlab giving incorrect matrix multiplication :/

45 ビュー (過去 30 日間)
Anas
Anas 2023 年 12 月 21 日
コメント済み: Steven Lord 2024 年 1 月 3 日
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 件のコメント
Torsten
Torsten 2023 年 12 月 21 日
Which 2 cells?
ans(3,2) and ans(3,3)
Anas
Anas 2024 年 1 月 3 日
InvA is not the inverse of A
I have 2 matricies invA and A I multiplied them the answer is wrong wh?
This is my question.
Multiply this row (3rd row of invA) with the second colum of A
0.0050 -0.0030 0.0002
1
10
100
the answer is
0.005 - 0.03 + 0.02 = -0.005
Which is calculated wrong in the matlab command window
Why is it wrong! this is my question
Try multiplying the matrices I provided.

サインインしてコメントする。

回答 (2 件)

John D'Errico
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)
Ainv = 3×3
1.0000 -0.1500 0.0050 0 0.2000 -0.0100 0 -0.0500 0.0050
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
ans = 3×3
1 0 4.44089209850063e-16 0 1 -1.77635683940025e-15 0 1.11022302462516e-16 1
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 件のコメント
Anas
Anas 2024 年 1 月 3 日
編集済み: Anas 2024 年 1 月 3 日
InvA is not the inverse of A I know that but thanks for pointing it out so aggressively!
I have 2 matricies invA and A I multiplied them the answer is wrong wh?
This is my question.
Multiply this row (3rd row of invA) with the second colum of A
0.0050 -0.0030 0.0002
1
10
100
the answer is
0.005 - 0.03 + 0.02 = -0.005
Which is calculated wrong in the matlab command window
Why is it wrong! this is my question
Try multiplying the matrices I provided.
Steven Lord
Steven Lord 2024 年 1 月 3 日
A = [0.0050 -0.0030 0.0002 ];
B = [1; 10; 100];
result = A*B
result = -0.0050
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
ans = logical
0
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
x = 1×11
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000
It appears that x contains the value 0.3, but it does not contain exactly 0.3.
checkWithExactEquality = x == 0.3
checkWithExactEquality = 1×11 logical array
0 0 0 0 0 0 0 0 0 0 0
It does contain a value that is extremely close to 0.3, however.
tolerance = 1e-15;
checkWithTolerance = abs(x-0.3) < tolerance
checkWithTolerance = 1×11 logical array
0 0 0 1 0 0 0 0 0 0 0
whichValueTolerance = x(checkWithTolerance)
whichValueTolerance = 0.3000
How far away from 0.3 is the value we found using a tolerance?
howDifferent = whichValueTolerance - 0.3
howDifferent = 5.5511e-17
To do the same with ismembertol:
checkWithIsmembertol = ismembertol(x, 0.3, tolerance)
checkWithIsmembertol = 1×11 logical array
0 0 0 1 0 0 0 0 0 0 0
whichValueIsmembertol = x(checkWithIsmembertol)
whichValueIsmembertol = 0.3000
The ismembertol function found the same value that the check with a tolerance did.

サインインしてコメントする。


Chunru
Chunru 2023 年 12 月 21 日
編集済み: Chunru 2023 年 12 月 21 日
There is no problem for the matrix A and its inverse. Where do you get invA?
A =[ 1 1 1
0 10 20
0 100 400]
A = 3×3
1 1 1 0 10 20 0 100 400
invA = inv(A)
invA = 3×3
1.0000 -0.1500 0.0050 0 0.2000 -0.0100 0 -0.0500 0.0050
invA * A
ans = 3×3
1.0000 0 0.0000 0 1.0000 -0.0000 0 0.0000 1.0000

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by