Matlab: how to display answers of underdetermined linear system as {a = r1, b = r1/2, c = r1} [closed]

StackOverflow https://stackoverflow.com/questions/17665400

  •  03-06-2022
  •  | 
  •  

Question

I have a 3x3 matrix, for example A=[3,2,4;2,0,2;4,2,3], and I'm trying to solve the following linear system in the form A*[a;b;c] = [8*a;8*b;8*c]:

3 2 4   a   8a
2 0 2 * b = 8b
4 2 3   c   8c

Ok, so I have:

3a + 2b + 4c = 8a
2a +      2c = 8b
4a + 2b + 3c = 8c

It's underdetermined, and an answer would be [2;1;2] In fact, if I use an online linear solver like this one‎, it will give me an answer like this:

{ a = r1, b = r1/2, c = r1 }

Still, I can't find a way to do that in Matlab. If I define B as B=[8*a;8*b;8*c] and try A\B, i get:

 2*b - 4*a + 4*c
 2*a - 7*b + 2*c
 4*a + 2*b - 4*c

And if I define B as [8;8;8], I get:

2
-3
2

While I expected:

2
1
2

Or something like the answer from the online solver form above. What am I doing wrong? Thanks in advance!

Was it helpful?

Solution

Oh well, even though this is NOT a programming problem, I'll answer it.

The question is to solve the linear system

Ax = 8x

where the 3x3 matrix A is given, and x is a 3x1 unknown vector.

A = [3 2 4;2 0 2;4 2 3];
[v,d] = eig(A)
v =
   -0.4941   -0.5580    0.6667
   -0.4720    0.8161    0.3333
    0.7301    0.1500    0.6667

d =
   -1.0000         0         0
         0   -1.0000         0
         0         0    8.0000

Here we see that the third eigenvalue is 8, so there is indeed a non-degenerate solution to the problem. It is of the form

k*v(:,3)

since v(:,3) is the corresponding eigenvector.

format rat
v(:,3)
ans =

       2/3     
       1/3     
       2/3     

Clearly this results in the solution given by the asker.

I'll note that this all works ONLY because the problem was posed in the form A*x=lambda*x, so a classical eigenvalue problem. Again, if you appreciate the mathematics behind the solution, then we can solve the problem using null:

null(A - 8*eye(3))
ans =
       2/3     
       1/3     
       2/3     

Of course, we could have used the symbolic toolbox.

sol = solve('3*a + 2*b + 4*c = 8*a','2*a + 2*c = 8*b','4*a + 2*b + 3*c = 8*c');

sol.a
ans =
z

sol.b
ans =
z/2

sol.c
ans =
z

Suppose instead that the problem was a completely general one? Thus, still a homogeneous linear system, but not an obvious eigenvalue problem? As an example, I'll try to solve the arbitrary linear problem

A*[a;b;c] = [a;2*b;3*c]

See that this is NOT written in the form of an eigenvalue problem. There are actually several ways we might decide to solve it. The unknowns are on BOTH sides of the equality. So just move them all to the left hand side. Semi-mathematically, we might do this as

B = A - diag([1 2 3])
B =
       2              2              4       
       2             -2              2       
       4              2              0       

We now try to solve the linear system

Bx = B*[a;b;c] = [0;0;0]

Do solutions to this exist? This time, they do not exist, beyond the trivial, degenerate solution, because B has full rank.

rank(B)
ans =
       3       

A full rank, homogeneous linear system has only the degenerate (zero) solution. Null tells us this too.

null(B)
ans =
   Empty matrix: 3-by-0

The symbolic toolbox solution reflects that fact.

sol = solve('3*a + 2*b + 4*c = a','2*a + 2*c = 2*b','4*a + 2*b + 3*c = 3*c')

sol.a
ans =
0

sol.b
ans =
0

sol.c
ans =
0

Until you appreciate the mathematics of the linear systems involved, this really is NOT a programming problem, and then it is essentially one command to solve the problem, so still not really much of a programming problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top