Solve a system of equations in the form given below
3 views (last 30 days)
Show older comments
I have a system of equations like this:
V=A.K1+B.K2
W=D.K1+C.K2
V,W,K1,K2,K1- vectors (1xn)
A,B,C,D-matrices (nxn)
K1 and K2 are vectors of unknowns
I dont want to use inverses to solve for the unknowns because some matrices are poorly conditioned. Is there any function in matlab that could solve it using more stable solution method?
0 Comments
Answers (2)
John D'Errico
on 20 Jun 2020
More stable than inv? You essentially NEVER want to use inv anyway.
Formulate it as a linear system of equations. Then call lsqminnorm. In the case of a singular or near singular problem, lsqminnorm will yield the minimum norm solution, thus much like pinv offers, but this is formulated as a solver tool. pinv also provides a minimum norm solution, and can be used as a "replacement" for inv.
Note that the result will not be a unique solution twhen the system is singular or nearly so. Therefore aslking for "stability" is a bit of a difficult question. It will be as stable as possible. Just not unique, and that will be true no mattter what you do in that circumstance.
2 Comments
madhan ravi
on 20 Jun 2020
+1 John how’s that even possible every answer you give is so impressive U+1F92F
Walter Roberson
on 20 Jun 2020
syms K1 [1 n]
syms K2 [1 n]
eqns1 = V == A*K1 + B*K2; %system of n equations
eqns2 = W == D*K1 + C*K2; %system of n equations
sol = solve([eqns1, eqns2], [K1 K2]); %2n equations in 2n varibles
This assumes the symbolic toolbox, and it assumes that the system is only ill-conditioned and not singular -- if it is singular then you might get parameterized solutions that you need to use 'returnconditions', true to access properly.
Note that all numeric values will be converted to symbolic numbers (mostly rational) when this is done, and the symbolic numbers are designed to be "good guesses", typically what you would "want" the results to be, rather than exact conversions.
For example sym(sqrt(2)*(1-49*eps)) guesses that you want 2^(1/2) but sym(sqrt(2)*(1-50*eps)) says that is too far from sqrt(2) to justify using that so it chooses 3184525836262851 / 2251799813685248 instead.
If your floating point numbers must be considered to be exact as-is, then
Q = @(x) sym(x,'f')
syms K1 [1 n]
syms K2 [1 n]
eqns1 = Q(V) == Q(A)*K1 + Q(B)*K2; %system of n equations
eqns2 = Q(W) == Q(D)*K1 + Q(C)*K2; %system of n equations
sol = solve([eqns1, eqns2], [K1 K2]); %2n equations in 2n varibles
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!