If you see this, something is wrong
To get acquainted with the document, the best thing to do is to select the "Collapse all sections" item from the "View" menu. This will leave visible only the titles of the top-level sections.
Clicking on a section title toggles the visibility of the section content. If you have collapsed all of the sections, this will let you discover the document progressively, from the top-level sections to the lower-level ones.
Generally speaking, anything that is blue is clickable.
Clicking on a reference link (like an equation number, for instance) will display the reference as close as possible, without breaking the layout. Clicking on the displayed content or on the reference link hides the content. This is recursive: if the content includes a reference, clicking on it will have the same effect. These "links" are not necessarily numbers, as it is possible in LaTeX2Web to use full text for a reference.
Clicking on a bibliographical reference (i.e., a number within brackets) will display the reference.
Speech bubbles indicate a footnote. Click on the bubble to reveal the footnote (there is no page in a web document, so footnotes are placed inside the text flow). Acronyms work the same way as footnotes, except that you have the acronym instead of the speech bubble.
By default, discussions are open in a document. Click on the discussion button below to reveal the discussion thread. However, you must be registered to participate in the discussion.
If a thread has been initialized, you can reply to it. Any modification to any comment, or a reply to it, in the discussion is signified by email to the owner of the document and to the author of the comment.
The blue button below that says "table of contents" is your tool to navigate in a publication.
The left arrow brings you to the previous document in the publication, and the right one brings you to the next. Both cycle over the publication list.
The middle button that says "table of contents" reveals the publication table of contents. This table is hierarchical structured. It has sections, and sections can be collapsed or expanded. If you are a registered user, you can save the layout of the table of contents.
First published on Wednesday, Apr 10, 2024 and last modified on Saturday, Apr 19, 2025
I am normally hidden by the status bar
Mathedu
Linear algebra, Linear systems
In that document, we will take advanrage of solving a little puzzle to explore everything about the solution of a system of two linear equations in two real variables.
We will first see how to solve such a system with the method of substitution/elimination.
Then we will explore end solve the equivalent matrix equation, manually and in Python, with the packages ‘numpy’ and ‘numpy.linalg’.
Here is a little puzzle that we will aim to solve.
The hypotheses of the puzzle are the following ones:
The double of the age of the captain plus the number of children of my aunt is 89.
The age of the captain plus the triple of the number of children of my aunt is 52.
And the questions the puzzle asks are the following ones:
How old is the captain?
How many children does my aunt have?
Let’s denote:
\( x\) the age of the captain,
and \( y\) the number of children of my aunt.
Then the hypotheses are transformed the following way:
“The double of the age of the captain plus the number of children of my aunt is 89" becomes:
(1)
“The age of the captain plus the triple of the number of children of my aunt is 52” becomes:
(2)
The questions of the puzzle are then transformed the following way:
“How old is the captain?” becomes "What is the value of \( x\) ?"
“How many children does my aunt have?” becomes "What is the value of \( y\) ?"
The puzzle is thus transformed into the following problem:
Find which real numbers fulfil the equations 1 and 2 together.
That means solving the following system of 2 linear equations of 2 real variables:
(3)
We solve now the system 3 by the method of substitution/elimination.
The equation 1 is equivalent to the linear equation in \( x\) :
(4)
Its solution depends on \( y\) and is: \( x=\frac{89-y}{2}= \frac{89}{2} -\frac{y}{2} \) .
Consequently, the solution of the first equation in \( x\) is:
(5)
In the equation 2, we eliminate \( x\) by replacing it by its value given by the equation 5.
The equation 2 is thus equivalent to:
(6)
If we multiply both members of the equality by \( 2\) , we obtain:
(7)
The equation 7 is equivalent to the affine equation in \( y\) :
(8)
Its solution is: \( y=\frac{104-89}{5}=\frac{15}{5}=3 \) .
Consequently, the solution of the system 3 in \( y\) is \( y=3\) and the answer to the second quetion of the puzzle is:
My aunt has \( 3\) children.
In the equation 5, we aliminate \( x\) by replacing it by its value \( 3\) .
The equation 5 is thus equivalent to:
(9)
If we set \( \frac{1}{2}\) into factor, we obtain: \( x=\frac{1}{2}(89-3)=86\times \frac{1}{2}=43 \) .
Consequently, the solution of the system 3 in \( x\) is \( x=3\) and the answer to the first quetion is:
The captain is \( 43\) years old.
Theorem 1
The solution of the system of linear equations in \( (x,y)\in\mathbb{R}^{2}\) :
(10)
is the couple of real numbers \( (x,y)=(43,3)\) .
Proof
Because of the calculations made, if \( (x,y)\in\mathbb{R}^{2}\) is a solution, then \( (x,y)=(43,3)\) .
And \( (x,y)=(43,3)\) is a solution, because:
Now, let’s enter the linear algebra, with an interpetation of the linear system 3with matrices and vectors.
We may define the column vectors: \( X=\begin{bmatrix}x\\ y\end{bmatrix}\) and \( B=\begin{bmatrix}89\\ 52\end{bmatrix}\) , as well as the square matrix: \( A=\begin{bmatrix}2&1\\ 1&3\end{bmatrix}\) .
The product \( AX\) of the matrix \( A\) and the column vector \( X\) is a column vector with two elements composed of:
its first element is the sum of the products of the elements of the first row of \( A\) by the elemens of \( X\)
its second element is the sum of the products of the elements of the second row of \( A\) by the elemens of \( X\)
So that it is equal to
(11)
The elements of the column vector \( AX\) are thus the first members of the linear system 3.
Moreover, the elemnets of the column vector \( B\) are the second membre of that system.
Theorem 1
Assume \( X=\begin{bmatrix}x\\ y\end{bmatrix}\) , \( B=\begin{bmatrix}89\\ 52\end{bmatrix}\) and \( A=\begin{bmatrix}2&1\\ 1&3\end{bmatrix}\) Then the system of 2 linear equations of 2 variables:
(12)
is equivalent to the matrix equation:
(13)
We may calculate the determinant of the matrix \( A=\begin{bmatrix}2&1\\ 1&3\end{bmatrix}\) as the difference between the product of the diagonal elements of \( A\) and the product of the anti-diagonal elements of \( A\) : \( \det(A)=2\times 3-1\times 1=5 \).
And we may define a matrix \( AA\) the following way:
The diagonal elements of \( A\) are exchanged to obtain the diagonal elements of \( AA\) .
The anti-diagonal elements of \( A\) are opposed to obtain the anti-diaognal elements of \( AA\) .
\( AA\) is thus equal to:
(14)
Then the inverse of the matrix \( A\) is the product of the inverse of \( \det(A)\) and \( AA\) :
(15)
where we have to enter the factor \( \frac{1}{5}\) in the matrix \( AA\) element by element, giving:
(16)
As the fraction have decimal numbers representations \( A^{-}\) may be rewritten;
(17)
We may multiply the matrix \( A^{-1}\) by the column vector \( B=\begin{bmatrix} 89\\ 52 \end{bmatrix}\) :
\( A^{-1}B=\begin{bmatrix} \frac{3}{5}&-\frac{1}{5}\\ -\frac{1}{5}&\frac{2}{5} \end{bmatrix} \begin{bmatrix}89\\ 52\end{bmatrix}=\begin{bmatrix} \frac{3\times 89-52}{5}\\ \\ \frac{2\times 52-89}{5} \end{bmatrix} \)
If we calculate further, we obtain: \( A^{-1}B= \begin{bmatrix} \frac{3\times 89-52}{5}\\ \\ \frac{2\times 52-89}{5} \end{bmatrix}= \begin{bmatrix} \frac{215}{5}\\ \\ \frac{15}{5} \end{bmatrix}= \begin{bmatrix} 43\\ 3 \end{bmatrix} \)
The elements of the column vector \( A^{-1}B\) are the element of the solution of the system 3, that is equivalent to the matrix system \( AX=B\) .
Theorem 2
Assume \( X=\begin{bmatrix}x\\ y\end{bmatrix}\) , \( B=\begin{bmatrix}89\\ 52\end{bmatrix}\) and \( A=\begin{bmatrix}2&1\\ 1&3\end{bmatrix}\) . Then the solution of the matrix eqution \( AX=B\) is the column vector \( A^{-1}B\) .
Corollary 1
Assume \( X=\begin{bmatrix}x\\ y\end{bmatrix}\) , \( B=\begin{bmatrix}89\\ 52\end{bmatrix}\) and \( A=\begin{bmatrix}2&1\\ 1&3\end{bmatrix}\) .
Then the solution of the system of linear equations:
(18)
is the couple of elements of the column vector \( A^{-1}B\) .
\paragraph{} We will use the Python packages ‘numpy’ and ‘numpy.linalg’ to define and manipulate vectors and matrices in Python.
To define vectors in Python, we first use the command:
from numpy import *to import all the functions of ‘numpy’ package.
The simpler way to create a vector is listing its elements.
For instance:
V=array([1, 2])creates the line vector \( V=\begin{bmatrix}1&2\end{bmatrix}\) .
To define matrices in Python, we first use the command:
from numpy import *to import all the functions of ‘numpy’ package.
The simpler way to create a matrix is listing its elements line by line.
For instance:
M=array([[1, 2], [3, 4]])creates the line vector \( M=\begin{bmatrix}1&2\\ 3&4\end{bmatrix}\) .
An important fact about the multiplication of a matrix and a vector in Python is that the vector is dealed as a column vector for the multiplication.
Then the result, that is a column vector, is displayed as a line vector.
Namely, let’s define a 2x2 matrix \( M\) and two vectors \( V\) and \( W\) with 2 elements in Python:
from numpy import *
M=array([[1, 2], [3, 4]])
V=array([1, 2])
W=M@VThen the line vector \( W=\begin{bmatrix} u&v \end{bmatrix}\) is so that the column vector \( W'=\begin{bmatrix} u\\ v \end{bmatrix}\) is the product of the matrix \( M=\begin{bmatrix}1&2\\ 3&4\end{bmatrix}\) and the column vector \( V'=\begin{bmatrix}1\\ 2\end{bmatrix}\) :
\( W'=\begin{bmatrix}1&2\\ 3&4\end{bmatrix}\begin{bmatrix}1\\ 2\end{bmatrix}= \begin{bmatrix}1\times 1 + 2\times 2\\ 3\times 1+4\times 2\end{bmatrix} =\begin{bmatrix}5\\ 11\end{bmatrix}\) , so that \( W=\begin{bmatrix}5&11\end{bmatrix}\) .
We need both ‘numpy’ and ‘numpy.linalg’ packages:
from numpy import *
from numpy.linalg import *Let’s now defin two matrices \( M\) and \( InvM\) :
M=array([[1, 2], [3, 4]])
InvM=inv(M)Then \( InvM\) is the inverse of the matrix \( M=\begin{bmatrix}1&2\\ 3&4\end{bmatrix}\) :
\( InvM=M^{-1}=\begin{bmatrix}1&2\\ 3&4\end{bmatrix}^{-1} =\frac{1}{1\times 4-3\times 2} \begin{bmatrix}4&-2\\ -3&1\end{bmatrix} =\begin{bmatrix}\frac{4}{-2}&\frac{-2}{-2}\\ \\ \frac{-3}{-2}&\frac{1}{-2}\end{bmatrix} = \begin{bmatrix}-2&0.5\\ 1&-1.5\end{bmatrix}\)
The following script ‘LinearSystem.py’ defines and solves the matrix equation \( AX=B\) , with \( A=\begin{bmatrix}2&1\\ 1&3\end{bmatrix}\) and \( B=\begin{bmatrix}89\\ 52\end{bmatrix}\) .
from numpy import *
from numpy.linalg import *
X0=array([43,3])
print('X0=',X0)
'''
It is a line vector
'''
A=array([[2,1],[1,3]])
print('A=\n',A)
'''
It is a matrix with 2 rows and 2 columns
'''
B0=A@X0
'''
The column vector B0' is the product of the matrix A and the column vector
X0'
'''
print('B0=',B0)
```
We have reconstructed the system of two linear equations and two
variables equivalent to the matric equation AX=B, with the solution (43,3).
```
B=array([89,52])
InvA=inv(A)
print('The inverse of A is\n',InvA)
X=InvA@B
'''
The column vector X' is the product of the inverse of A and the column
vector B'
'''
print('X=',X)
'''
The column vector X' is the solution of the matrix equation AX'=B'
'''In that note, we have seen:
How to rewrite a simple puzzle as a system of two linear equations of two real variables.
How to solve the system obtained with a method of elimination and substitution, also called the ‘pivot’ method.
How to transform the linear system into a matrix equation.
How to solve the matrix equation obtained directly with the inversion of a 2x2 matrix.
How to set and solve the matrix equation in Python, with the packages ‘numpy’ and ‘numpy.linalg’.