Solve overdetermined system of matrices

4 views (last 30 days)
Kelly
Kelly on 28 Aug 2015
Commented: Kelly on 28 Aug 2015
I have an overdetermined system of the form Ax = B, where A, x, and B are all matrices. My system looks like this:
A1 * x = B1
A2 * x = B2
...
AN * x = BN
I am trying to find the x which best fits my system. I initially attempted this using lsqonneg function, but this did not work because x, A, and B are matrices. I then manually wrote a least squares function:
function [difference] = least_squares(x)
difference = zeros(m,n);
for i = 1 : number of equations
difference = difference + (A{i}*x - B{i}).^2;
end
end
eqn = @least_squares;
x = fmincon(eqn,x0);
This solves the problem of matrix inputs. However, the fmincon and fminsearch functions still expect difference to be a scalar. Any suggestions for solving this problem are greatly appreciated!

Answers (1)

Star Strider
Star Strider on 28 Aug 2015
I don’t understand that ‘A, x, and B are all matrices.’
I can’t run your code, but if you’re doing a least-squares cost function, see if adding the sum function does what you want. This assumes ‘A’ is a matrix and ‘x’ and ‘B’ are vectors:
difference = difference + sum((A{i}*x - B{i}).^2);
It should give you a scalar value, and if all goes well, is the cost function that does what you want.
If they’re actually all matrices and my assumption from your code is in error, so that the result of the calculation in your ‘difference’ assignment is a matrix rather than a vector, consider using the norm function.
  4 Comments
Matt J
Matt J on 28 Aug 2015
I second Torsten's suggestion. Since there are no constraints in play, A\B should work just fine here. If there are linear constraints (e.g., positivity) there are ways to solve the problem with LSQLIN.
Kelly
Kelly on 28 Aug 2015
I implemented what Torsten suggested. It sounds like I was making an easy problem much more difficult than it needed to be. Thank you Torsten and Matt the suggestion and explanations!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!