Systems of Linear Equations

Graphical Representation of Linear Equations

In[1]:=

eqns = {x1 + 2x25,          3x1 + 4x26} ; 

soln = Table[Solve[eqns[[k]], x2],  {k, 1, 2}]//Flatten

Out[2]=

{x2 (5 - x1)/2, x2 -3/4 (-2 + x1)}

In[3]:=

lines = Table[x2/.soln[[k]],  {k, 1, 2}]

Out[3]=

{(5 - x1)/2, -3/4 (-2 + x1)}

In[4]:=

Plot[Evaluate[lines], {x1, -8, 5}, AxesLabel {x1, x2}, PlotStyle {Orange, ForestGreen}] ;

[Graphics:HTMLFiles/1_linear_equations_7.gif]

The Number of Solutions of a System of Linear Equations

The graphical representation of linear equations makes it clear that there are precisely three possibilities for the solution sets of such systems:
(1) There is no solution
(2) There is precisely one solution
(1) There is an infinite number of solutions

In[5]:=

(* No solution -- parallel lines *)eqns = {3x1 - 2x212, &nbs ... on", AxesLabel {x1, x2}, PlotStyle {Orange, ForestGreen}] ;

Out[6]=

{x23/2 (-4 + x1), x23/2 (-2 + x1)}

[Graphics:HTMLFiles/1_linear_equations_10.gif]

In[9]:=

(* One solution -- intersecting lines *)eqns = {3x1 - 2x212,  ... on", AxesLabel {x1, x2}, PlotStyle {Orange, ForestGreen}] ;

Out[10]=

{x23/2 (-4 + x1), x2 -2 (-3 + x1)}

[Graphics:HTMLFiles/1_linear_equations_13.gif]

In[13]:=

(* Infinite number of solutions -- coincident lines *)eqns = {3x1 - 2x2ɱ ... ns", AxesLabel {x1, x2}, PlotStyle {Orange, ForestGreen}] ;

Out[14]=

{x23/2 (-4 + x1), x23/2 (-4 + x1)}

[Graphics:HTMLFiles/1_linear_equations_16.gif]

Solving Systems of Linear Equations

Using Mathematica to solve linear equations

In[17]:=

eqns = {x1 + 3x2  4,            2x1 - x2  5} ; 

Solve[eqns, {x1, x2}]//Flatten

Out[18]=

{x119/7, x23/7}

Lay Example 1.1.1, p.5

In[19]:=

eqns = {x1 - 2x2 + x30,         &nbs ... 2 - 8x38,       -4x1 + 5x2 + 9x3 -9} ; 

Solve[eqns, {x1, x2, x3}]//Flatten

Out[20]=

{x129, x216, x33}

Linear Equations to Matrices

In[21]:=

<<LinearAlgebra`MatrixManipulation`

In[22]:=

Clear[x, y, x1, x2, x3] ; 

eqns = {a11 x + a12 yc1,           a21 x + a22 yc2} ; 

LinearEquationsToMatrices[eqns, {x, y}] ;

Map[MatrixForm, %]

Out[25]=

{( {{a11, a12}, {a21, a22}} ), ( {{c1}, {c2}} )}

In[26]:=

eqns = {x1 + 2x25,          3x1 + 4x26} ; 

LinearEquationsToMatrices[eqns, {x1, x2}] ;

Map[MatrixForm, %]

Out[28]=

{( {{1, 2}, {3, 4}} ), ( {{5}, {6}} )}

In[29]:=

eqns = {x1 - 2x2 + x30,         &nbs ... 2 - 8x38,       -4x1 + 5x2 + 9x3 -9} ; 

LinearEquationsToMatrices[eqns, {x1, x2, x3}] ;

Map[MatrixForm, %]

Out[31]=

{( {{1, -2, 1}, {0, 2, -8}, {-4, 5, 9}} ), ( {{0}, {8}, {-9}} )}

Matrices to Linear Equations

In[32]:=

matricesToLinearEquations[{m_, b_}, vars_] := If[Dimensions[m][[2]] == Length[vars]  ... ad[m . varsb], "Error: matricesToLinearEquations: Sizes don't match."]

In[33]:=

m = ( {{1, 2}, {3, 4}} ) ;

b = {5, 6} ;

vars = {x, y} ; 

matricesToLinearEquations[{m, b}, vars]

Out[36]=

{x + 2 y5, 3 x + 4 y6}

In[37]:=

eqns = {x1 - 2x2 + x30,         &nbs ... 2 - 8x38,       -4x1 + 5x2 + 9x3 -9} ; 

vars = {x1, x2, x3} ; 

matricesToLinearEquations[LinearEquationsToMatrices[eqns, vars], vars]

Out[39]=

{x1 - 2 x2 + x30, 2 x2 - 8 x38, -4 x1 + 5 x2 + 9 x3 -9}

Augmented Matrices

Make an augmented matrix.

In[40]:=

<<LinearAlgebra`MatrixManipulation`

In[41]:=

makeAug :: usage = "makeAug[{mat,vec}] takes a pair consisting of a matrix and vector, and returns the associated augmented matrix." ; <br />

makeAug[{mat_, vec_}] := If[Length[mat] Length[vec], AppendRows[mat, Map[List, vec]], "Error: makeAug: Sizes don't match."]

In[43]:=

eqns = {x1 - 2x2 + x30,         &nbs ... bsp;    2x2 - 8x38,  -4x1 + 5x2 + 9x3 -9} ; 

mv = LinearEquationsToMatrices[eqns, {x1, x2, x3}] ; 

aug = makeAug[mv] ;

%//MatrixForm

Out[46]//MatrixForm=

( {{1, -2, 1, 0}, {0, 2, -8, 8}, {-4, 5, 9, -9}} )

Decompose an augmented matrix

In[47]:=

decomposeAug :: usage = "decomposeAug[aug] decomposes an augmented matrix into a pair containing its component matrix and vector." ; 

decomposeAug[aug_] :=  {Table[Drop[aug[[k]], -1], {k, Length[aug]}], Table[aug[[k, -1]], {k, Length[aug]}]}

In[49]:=

a = ({{1, -2, 1, 0}, {0, 2, -8, 8}, {-4, 5, 9, -9}}) ; 

decomposeAug[a] ;

Map[MatrixForm, %]

Out[51]=

{( {{1, -2, 1}, {0, 2, -8}, {-4, 5, 9}} ), ( {{0}, {8}, {-9}} )}

Using RowReduce to Solve Linear Equations

Lay Example 1.1.1, p.5

In[52]:=

<<LinearAlgebra`MatrixManipulation`

In[53]:=

rr = RowReduce[aug] ;

%//MatrixForm

soln = TakeColumns[rr, -1]//Flatten

ToRules[{x1, x2, x3} soln]

Out[54]//MatrixForm=

( {{1, 0, 0, 29}, {0, 1, 0, 16}, {0, 0, 1, 3}} )

Out[55]=

{29, 16, 3}

Out[56]=

{x129, x216, x33}

Detecting an Inconsistent System

Lay Example 1.1.1, p.5

In[57]:=

<<LinearAlgebra`MatrixManipulation`

In[58]:=

eqns = {x2 - 4x38, 2x1 - 3x2 + 2x31, 5x1 - 8x2 + 7x31} ; 

mv = LinearEquationsToMatrices[eqns, {x1, x2, x3}] ;

aug = makeAug[mv] ;

%//MatrixForm

RowReduce[aug]//MatrixForm

Out[61]//MatrixForm=

( {{0, 1, -4, 8}, {2, -3, 2, 1}, {5, -8, 7, 1}} )

Out[62]//MatrixForm=

( {{1, 0, -5, 0}, {0, 1, -4, 0}, {0, 0, 0, 1}} )

Lay Example 1.1.2, p.8

In[63]:=

<<LinearAlgebra`MatrixManipulation`

In[64]:=

eqns = {x1 - 2x2 + x30,         &nbs ... bsp;    2x2 - 8x38,  -4x1 + 5x2 + 9x3 -9} ; 

mv = LinearEquationsToMatrices[eqns, {x1, x2, x3}] ;

aug = makeAug[mv] ;

%//MatrixForm

RowReduce[aug]//MatrixForm

Out[67]//MatrixForm=

( {{1, -2, 1, 0}, {0, 2, -8, 8}, {-4, 5, 9, -9}} )

Out[68]//MatrixForm=

( {{1, 0, 0, 29}, {0, 1, 0, 16}, {0, 0, 1, 3}} )

Lay Example 1.1.3, p.9

In[69]:=

<<LinearAlgebra`MatrixManipulation`

In[70]:=

eqns = { x2 - 4x38, 2x1 - 3x2 + 2x31, 5x1 - 8x2 + 7x31} ; 

mv = LinearEquationsToMatrices[eqns, {x1, x2, x3}] ;

aug = makeAug[mv] ;

%//MatrixForm

RowReduce[aug]//MatrixForm

Out[73]//MatrixForm=

( {{0, 1, -4, 8}, {2, -3, 2, 1}, {5, -8, 7, 1}} )

Out[74]//MatrixForm=

( {{1, 0, -5, 0}, {0, 1, -4, 0}, {0, 0, 0, 1}} )

Solving a  System of Equations with LinearSolve

We wish to solve the linear system represented by the following augmented matrix (See Lay Example 1.1.1, p.4 ff.)

In[75]:=

a = ({{1, -2, 1, 0}, {0, 2, -8, 8}, {-4, 5, 9, -9}}) ;

LinearSolve provides an easy way to do that .
First, extract the relevant components, m and b.

In[76]:=

m = TakeColumns[a, 3] ;

%//MatrixForm

b = TakeColumns[a, -1] ;

%//MatrixForm

Out[77]//MatrixForm=

( {{1, -2, 1}, {0, 2, -8}, {-4, 5, 9}} )

Out[79]//MatrixForm=

( {{0}, {8}, {-9}} )

Then use LinearSolve on the components.

In[80]:=

s = LinearSolve[m, b]

Out[80]=

{{29}, {16}, {3}}

Let's check that result.

In[81]:=

m . sb

Out[81]=

True


Created by Mathematica  (January 20, 2005) Valid XHTML 1.1!