# how to solve system of linear equations with unorganized unknowns?

33 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

John D'Errico
on 23 Mar 2015

Edited: John D'Errico
on 23 Mar 2015

Just move all of the unknowns to the left hand side.

A*X - B*Y = 0

Think of it like

[A,-B]*[X;Y] = 0

Any knowns in that set among the a and y variables, you move to the right hand side and sum them up. They are all constants. I fail to see the problem. How would you solve it using pencil and paper? (Answer: Exactly the same way.)

My guess is you are getting confused because you think you have TWO sets of unknowns, mixed in with knowns, in both X and Y. In fact, you can do the entire procedure in only several lines of code, fully vectorized. TRY IT! You might surprise yourself. (Take a shot and try it. REALLY. See how it goes. If you still get confused, then just comment here and I'll show you how.)

##### 5 Comments

John D'Errico
on 23 Mar 2015

In the example I just gave, here is my solution...

AB = [A,-B];

XY(unknowns) = AB(:,unknowns)\(-AB(:,knowns)*XY(knowns))

XY =

1

14.625

-2.5

4.875

7

10

(As I said, two lines of code.)

As a test to confirm this is indeed a solution...

A*XY(1:3) - B*XY(4:6)

ans =

0

0

0

In the other case, where you have

A*X = Y

with some elements of both X and Y unknown, this is IDENTICAl to the above more general problem, except that here we have B=eye(numel(Y)). No penalty functions are needed. That is simply a bad idea when the solution is so trivial. Honestly, this really is not hard to do. I call it trivial, but I've been writing the linear algebra code for things like this for years now. No matter what, it is still easy.

### More Answers (1)

### See Also

### Community Treasure Hunt

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

Start Hunting!