MATLAB Answers

Naime
0

fix inv warning in matlab

Asked by Naime
on 28 Jul 2019
Latest activity Commented on by Walter Roberson
on 2 Aug 2019
I use this code:
b=inv(A'*A)*A'*y;
Matlab gives warning. never use inv to solve linear system
How can I fix it?

  0 Comments

Sign in to comment.

1 Answer

Walter Roberson 님의 답변 28 Jul 2019
 채택된 답변

b=inv(A'*A)*A'*y;
Multiply both sides on the left by A'*A :
(A'*A) * b = (A'*A) * inv(A'*A) * A' * y
and for any invertable matrix, X * inv(X) is the identity matrix so (A'*A) * inv(A'*A) cancels out to identity, so
(A'*A) * b = A' * y
Multiply both sides on the left by inv(A'):
inv(A') * A' * A * b = inv(A') * A' * y
and inv(A') * A' cancels to the identity on both sides, so
A * b = y
Multiply by inv(A) on the left on both sides:
inv(A) * A * b = inv(A) * y
inverse cancels to identity, so
b = inv(A) * y
Now use the \ operator:
b = A \ y;
The above mathematics might not strictly apply if A is not a square matrix.

  8 Comments

Yes, but you should be questioning why you are doing that.
Provided that HH and WWInv are square matrices, then
inv(HH' * WWInv * HH)
equals
inv(HH)*inv(WWInv) * inv(HH')
You then right-multiply by HH' so
inv(HH)*inv(WWInv) * inv(HH') * HH'
and you group the last two together and the inv(HH') will cancel the HH, leaving
inv(HH)*inv(WWInv)
The name suggests that inv(WWInv) would probably be an existing variable named WW, so you could probably instead be using
RR = WW - HH \ WW
Thank you. But
RR = WW - HH * inv(HH' * WWInv * HH) * HH';
RR = WW - HH *inv(HH)*inv(WWInv)
RR = 0
Yes, I missed the HH pre-multiplier, but Yes, that logic appears correct. If you try it with actual random matrices, you will find that RR is 0 to within round-off error (e.g., for rand(15,15) all of the entries come out with absolute value less than 1E-13

Sign in to comment.