So I have the following system of equations
x1 - x2 = 20
x2 - x3 = 30
x3 - x4 = 75
x4 - x5 = -49
-x1 + x5 = -20
how would I solve the system using Matlab? I I'm a little stuck.
There's a good chance there's no solution but if someone could let me know how to do it that would be great!
First, convert this equation into matrix notation:
A = [ 1 -1 0 0 0
0 1 -1 0 0
0 0 1 -1 0
0 0 0 1 -1
-1 0 0 0 1];
b = [ 20
30
75
-49
-20];
You are trying to find x
giving Ax = b
. You can not take the inverse of A
since it is singular. To see this check its rank; rank(A) == 4
. It would be 5 if A
were non-singular.
So, you should find best x
approximating b
when multiplied by A
from left. This is an optimization problem: you want to minimize the error between Ax
and b
. Usually, people use least squares method. That is, you minimize the sum of squares of the residuals. This can be done by pseudo inverse as follows:
x = pinv(A) * b
gives
x =
31.8000
23.0000
4.2000
-59.6000
0.6000
Best approximation is found by
b2 = A*x
b2 =
8.8000
18.8000
63.8000
-60.2000
-31.2000
The least squares error is found to be
e = norm(b-b2)
e =
25.0440
If you want to try other methods alternative to least squares to minimize Ax-b
, you can google l1-minimization, sparse encoding, etc.
Just look at it and mentally add up the equations. LHS is zero, right hand side is something positive, so there is no solution!
来源:https://stackoverflow.com/questions/13484109/solving-system-of-equations-using-matlab