Search code examples
matlablinear-algebrasymbolic-mathalgebra

How to solve simple linear algebra equations using symbolic objects?


This is probably very simple but I'm having trouble setting up matrices to solve two linear equations using symbolic objects.

The equations are on the form:

(1) a11*x1 + a12*x2 + b1 = 0
(2) a21*x1 + a22*x2 + b2 = 0

So I have a vector {E}:

      [ a11*x1 + a12*x2 + b1 ]
{E} = [ a21*x1 + a22*x2 + b2 ]

I want to get a matrix [A] and a vector {B} so I can solve the equations, i.e. [A]*{X} + {B} = 0 => {X} = -[A]{B}.

Where

      [ x1 ]
{X} = [ x2 ]

      [ a11 a12 ]
[A] = [ a21 a22 ]

      [ b1 ]
{B} = [ b2 ]

Matrix [A] is just the Jacobian matrix of {E} but what operation do I have to perform on {E} to get {B}, i.e. the terms that don't include an x?

This is what I have done:

x = sym('x', [2 1]);
a = sym('a', [2 2]);
b = sym('b', [2 1]);

E = a*x + b;
A = jacobian(E,x);

n = length(E);
B = -E;
for i = 1:n
    for j = 1:n
        B(i) = subs(B(i), x(j), 0);
    end
end

X = A\B

I'm thinking there must be some function that does this in one line.

So basically my question is: what can I do instead of those for loops?

(I realize this is something very simple and easily found by searching. The problem is I don't know what this is called so I don't know what to look for.)


Solution

  • It is just B = subs(B,x,[0 0])