next up previous
Next: MuPAD Up: Octave Previous: Installation and Usage

Solving Systems of Linear Equations

Next we'll discuss how to solve systems of linear equations. In algebra courses we are taught several methods of doing this, including substitution, cancellation, or matrix manipulation. Octave uses the matrix method. For simple sets of equations, say those with two unknowns, the matrix method may seem overly complicated and pointless. However, in systems of greater than two variables the power of the matrix method is quickly apparent. 2

We start with a system of three linear equations:

\begin{displaymath}\begin{split}x + 2y + 3z & = 20 \\ 2x + y + z & = 11 \\ x + y - z & = 1 \\ \end{split}\end{displaymath} (2)

We'll use the coefficients of the variables to construct a square matrix. Next, we construct a matrix of solutions. This will yield the following matrices:

$\displaystyle A = \begin{bmatrix}1 & 2 & 3 \cr 2 & 1 & 1 \cr 1 & 1 & -1 \end{bmatrix} B = \begin{bmatrix}20 \cr 11 \cr 1 \end{bmatrix}$ (3)

To do this in octave we type the following commands:

  octave:27> A = [1, 2, 3; 2, 1, 1; 1, 1, -1;]

The returns the following formatted matrix:

  A =
     1   2   3
     2   1   1
     1   1  -1

To create the matrix of solutions we do the following:

  octave:27> B = [20; 11; 1]

which returns this:

  B =
    20
    11
     1

It is important to note that semicolons, not commas, are used to distinguish the elements of the matrix. If instead we had typed:

  octave:27> B = [20, 11, 1]

we would have created a 1 row by 3 column matrix.

Now we can use Octave to solve this system of equations. From our studies of matrix manipulation, we know that the inverse of a square matrix multiplied by the corresponding solution matrix will return the solution set of the system. We find the inverse matrix of A by doing:

  octave:30> inv(A)

This returns $ A^{-1}$ :

ans =
     -0.28571   0.71429  -0.14286
     0.42857  -0.57143   0.71429
     0.14286   0.14286  -0.42857

To complete the process, we multiply the inverse with the solution matrix to yield:

octave:9> inv(A)*B
ans =
     2.0000
     3.0000
     4.0000

You'll notice that the solutions are formatted as reals. This is a consequence of the floating point arithmetic required for calculations and is dependent upon the machine. Luckily, Octave has a shortcut function that does not require calculating the inverse matrix and then doing a separate multiplication operation. Use the backslash, or back division operator to calculate the solution:

octave:10> A \ B
ans =
     2
     3
     4


next up previous
Next: MuPAD Up: Octave Previous: Installation and Usage
Kwan Lowe 2003-01-31