The fix(x) command does not work

3 vues (au cours des 30 derniers jours)
Oskar
Oskar le 29 Nov 2020
Why does the one not become a one when i fix it. I have tried everything but cant get it to work!
A=[4 2 3 1; 2 5 6 2; 0 0 1 3; -1 -2 9 8]
B=inv(A);
C=A*B;
C = fix(C)
D=B*A;
D = fix(D)
C==D
  1 commentaire
John D'Errico
John D'Errico le 29 Nov 2020
Please don't post a PICTURE of code. That makes it impossible for people to copy what you did, and then use it as an example to help you. Is there a good reason why you WANT to make it more difficult for someone to help you???????

Connectez-vous pour commenter.

Réponse acceptée

John D'Errico
John D'Errico le 29 Nov 2020
Modifié(e) : John D'Errico le 29 Nov 2020
Fix does work. You need to understand what you asked it to do.
A = [4 3 2 1;2 5 6 2;0 0 1 3;-1 -2 9 8];
format long g
B = inv(A)
B =
0.361823361823362 -0.190883190883191 -0.168091168091168 0.0655270655270655
-0.153846153846154 0.230769230769231 0.307692307692308 -0.153846153846154
0.00854700854700855 0.0427350427350427 -0.350427350427351 0.11965811965812
-0.00284900284900285 -0.0142450142450142 0.45014245014245 -0.0398860398860399
Do you recognize the matrix inverse is NOT composed of integer values?
C = A*B
C =
1 8.15320033709099e-17 5.55111512312578e-17 -3.46944695195361e-17
-1.12757025938492e-16 1 3.33066907387547e-16 9.71445146547012e-17
-3.46944695195361e-18 0 1 -1.38777878078145e-17
7.63278329429795e-17 9.71445146547012e-17 -4.44089209850063e-16 1
The resulting product is NOT identically an identity matrix. That is exactly as expected. The deviations from an identity matrix are tiny, errors in the least significant bits.
fix(C)
ans =
0 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
C(1,1)
ans =
1
>> C(1,1) == 1
ans =
logical
0
>> C(1,1) - 1
ans =
-1.11022302462516e-16
So C(1,1) is not exactly 1. In fact, it is just a tiny bit less than 1. Close to 1 is not exactly 1. For example:
fix(1 - eps)
ans =
0
Is the result an identity matrix? It should be effectively so, if A was of full rank.
rank(A)
ans =
4
norm(A*B - eye(4))
ans =
6.74932945500581e-16
Again, all completely expected.
Since A is composed of integers, and A is of full rank, you could do this:
Bs = inv(sym(A))
Bs =
[127/351, -67/351, -59/351, 23/351]
[ -2/13, 3/13, 4/13, -2/13]
[ 1/117, 5/117, -41/117, 14/117]
[ -1/351, -5/351, 158/351, -14/351]
A*Bs
ans =
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]
The result is now an EXACT identity matrix, because Bs is also exact, which was possible because A was an integer matrix.
Note that symbolic inverses on large matrices will typically be massively computationally intensive.

Plus de réponses (1)

Steven Lord
Steven Lord le 29 Nov 2020
Just because a number is displayed as 1 in the default display format does not mean that the stored value is exactly 1. It's close but not exactly equal to 1. It's ever so slightly less than 1.
A=[4 2 3 1; 2 5 6 2; 0 0 1 3; -1 -2 9 8];
B=inv(A);
C=A*B;
1 - C(2, 2)
ans = 1.1102e-16

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by