Solving linear equations

| categories: linear algebra | View Comments

kreyszig_7_3

Contents

Solving linear equations

Given these equations, find [x1; x2; x3].

$x_1 - x_2 + x_3 = 0$

$10 x_2 + 25 x_3 = 90$

$20 x_1 + 10 x_2 = 80$

reference: Kreysig, Advanced Engineering Mathematics, 9th ed. Sec. 7.3

When solving linear equations, it is convenient to express the equations in matrix form:

A = [[1 -1 1];
    [0 10 25];
    [20 10 0]]

b = [0; 90; 80]  % b is a column vector
A =

     1    -1     1
     0    10    25
    20    10     0


b =

     0
    90
    80

making the augmented matrix

Awiggle = [A b]
Awiggle =

     1    -1     1     0
     0    10    25    90
    20    10     0    80

get the reduced row echelon form

rref(Awiggle)
ans =

     1     0     0     2
     0     1     0     4
     0     0     1     2

by inspection the solution is:

$x_1=2$

$x_2=4$

$x_3=2$

Let's test that out.

testing the solution

x = [2;4;2]
A*x
b
% you can see that A*x = b
x =

     2
     4
     2


ans =

     0
    90
    80


b =

     0
    90
    80

The shortcut way to solve this problem in Matlab with notation.

the backslash operator solves the equation Ax=b. Note that when there are more equations than unknowns

x = A\b
x =

    2.0000
    4.0000
    2.0000

linsolve function

x = linsolve(A,b)
x =

    2.0000
    4.0000
    2.0000

Not recommended solution methods

You may recall that a solution to

$\mathbf{A}\mathit{x} = \mathit{b}$

is:

$\mathit{x} = \mathbf{A}^{-1}\mathit{b}$

x = inv(A)*b

% while mathematically correct, computing the inverse of a matrix is
% computationally inefficient, and not recommended most of the time.
x =

    2.0000
    4.0000
    2.0000

What if no solution exists?

The following equations have no solution:

$3x_1 + 2x_2 + x_3 = 3$

$2x_1 + x_2 + x_3 = 0$

$6x_1 + 2x_2 + 4x_3 = 6$

A = [[3 2 1];
    [2 1 1];
    [6 2 4]]

b = [3; 0; 6]
A =

     3     2     1
     2     1     1
     6     2     4


b =

     3
     0
     6

how do you know no solution exists?

rref([A b])
% the last line of this matrix states that 0 = 1. That is not true, which
% means there is no solution.
ans =

     1     0     1     0
     0     1    -1     0
     0     0     0     1

Matlab gives you a solution anyway! and a warning.

x = A\b


% categories: Linear algebra
Warning: Matrix is close to singular or badly scaled.
         Results may be inaccurate. RCOND = 3.364312e-018. 

x =

  1.0e+016 *

    1.8014
   -1.8014
   -1.8014

blog comments powered by Disqus