LaTex2Web logo

LaTeX2Web, a web authoring and publishing system

If you see this, something is wrong

Collapse and expand sections

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.

Cross-references and related material

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.

Discussions

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.

Publications

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.

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

Around the Solution of a Puzzle

Fabienne Chaplais Mathedu

Keywords: Linear algebra, Linear systems

1 Introduction

Section 1.1

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’.

2 Let’s solve a Puzzle

2.1 The puzzle statement

Here is a little puzzle that we will aim to solve.

The hypotheses of the puzzle are the following ones:

  1. The double of the age of the captain plus the number of children of my aunt is 89.

  2. 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:

  1. How old is the captain?

  2. How many children does my aunt have?

2.2 Rewrite the hypotheses

Let’s denote:

  1. \( x\) the age of the captain,

  2. and \( y\) the number of children of my aunt.

Then the hypotheses are transformed the following way:

  1. “The double of the age of the captain plus the number of children of my aunt is 89" becomes:

    \[ \begin{equation} 2x+y=89 \end{equation} \]

    (1)

  2. “The age of the captain plus the triple of the number of children of my aunt is 52” becomes:

    \[ \begin{equation} x+3y=52 \end{equation} \]

    (2)

2.3 Rewrite the questions

The questions of the puzzle are then transformed the following way:

  1. “How old is the captain?” becomes "What is the value of \( x\) ?"

  2. “How many children does my aunt have?” becomes "What is the value of \( y\) ?"

2.4 System of 2 Linear Equations of 2 Real Variables

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:

\[ \begin{equation} \left\{ \begin{matrix} 2x&&+&&y&&=&&89\\ x&&+&&3y&&=&&52 \end{matrix} \right. \end{equation} \]

(3)

3 Solve the System 3

We solve now the system 3 by the method of substitution/elimination.

3.1 Solve the first Equation in \( x\)

The equation 1 is equivalent to the linear equation in \( x\) :

\[ \begin{equation}2x=89-y\end{equation} \]

(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:

\[ \begin{equation}x= \frac{89}{2} -\frac{1}{2}y\end{equation} \]

(5)

3.2 Replace \( x\) by its Value in the Second Equation

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:

\[ \begin{equation}\frac{89}{2} -\frac{1}{2}y+3y=52\end{equation} \]

(6)

If we multiply both members of the equality by \( 2\) , we obtain:

\[ \begin{equation}89-y+6y=104\end{equation} \]

(7)

The equation 7 is equivalent to the affine equation in \( y\) :

\[ \begin{equation}5y+89=104\end{equation} \]

(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.

3.3 Replace \( y\) by its Value \( 3\) in the First Equation

In the equation 5, we aliminate \( x\) by replacing it by its value \( 3\) .

The equation 5 is thus equivalent to:

\[ \begin{equation}x=\frac{89}{2}-3\times\frac{1}{2}\end{equation} \]

(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.

3.4 Conclusion

Theorem 1

The solution of the system of linear equations in \( (x,y)\in\mathbb{R}^{2}\) :

\[ \begin{equation} \left\{\begin{matrix}2x&&+&&y&&=&&89\\ x&&+&&3y&&=&&52\end{matrix}\right.\end{equation} \]

(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:

  1. \( 2x+y=2\times 43+3=86+3=89\) ,
  2. and \( x+3y=43+3\times 3=43+9=52\) .

4 The Matrix View

Now, let’s enter the linear algebra, with an interpetation of the linear system 3with matrices and vectors.

4.1 The linear system 3 as a matrix equation

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:

  1. its first element is the sum of the products of the elements of the first row of \( A\) by the elemens of \( X\)

  2. 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

\[ \begin{equation} AX=\begin{bmatrix}2&1\\ 1&3\end{bmatrix} \begin{bmatrix}x\\ y\end{bmatrix} =\begin{bmatrix}2x+y\\ x+3y\end{bmatrix} \end{equation} \]

(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:

\[ \begin{equation} \left\{ \begin{matrix} 2x&&+&&y&&=&&89\\ x&&+&&3y&&=&&52 \end{matrix} \right. \end{equation} \]

(12)

is equivalent to the matrix equation:

\[ \begin{equation} AX=B \end{equation} \]

(13)

4.2 Inverse of the Matrix \( A\)

Section 4.2.1

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 \).

Section 4.2.2

And we may define a matrix \( AA\) the following way:

  1. The diagonal elements of \( A\) are exchanged to obtain the diagonal elements of \( AA\) .

  2. The anti-diagonal elements of \( A\) are opposed to obtain the anti-diaognal elements of \( AA\) .

\( AA\) is thus equal to:

\[ \begin{equation} AA=\begin{bmatrix}3&-1\\ -1&2\end{bmatrix} \end{equation} \]

(14)

Then the inverse of the matrix \( A\) is the product of the inverse of \( \det(A)\) and \( AA\) :

\[ \begin{equation} A^{-1}=\frac{1}{5}\begin{bmatrix}3&-1\\ -1&2\end{bmatrix} \end{equation} \]

(15)

where we have to enter the factor \( \frac{1}{5}\) in the matrix \( AA\) element by element, giving:

\[ \begin{equation} A^{-1}= \begin{bmatrix} \frac{3}{5}&-\frac{1}{5}\\ \\ -\frac{1}{5}&\frac{2}{5} \end{bmatrix} \end{equation} \]

(16)

As the fraction have decimal numbers representations \( A^{-}\) may be rewritten;

\[ \begin{equation} A^{-1}= \begin{bmatrix} 0.6 & -0.2\\ -0.2 & 0.4 \end{bmatrix} \end{equation} \]

(17)

4.3 Solve the system 3 the matrix way

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:

\[ \begin{equation} \left\{ \begin{matrix} 2x&&+&&y&&=&&89\\ x&&+&&3y&&=&&52 \end{matrix} \right. \end{equation} \]

(18)

is the couple of elements of the column vector \( A^{-1}B\) .

5 The matrices and vectors in Python

\paragraph{} We will use the Python packages ‘numpy’ and ‘numpy.linalg’ to define and manipulate vectors and matrices in Python.

5.1 Vectors 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}\) .

5.2 Matrices in Python

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}\) .

5.3 Product of a Matrix and a Vector in Python

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@V

Then 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}\) .

5.4 Invert a Matrix in Python

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}\)

5.5 Solve our matrix Equation in Python

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' 
'''

6 Conclusion

In that note, we have seen:

  1. How to rewrite a simple puzzle as a system of two linear equations of two real variables.

  2. How to solve the system obtained with a method of elimination and substitution, also called the ‘pivot’ method.

  3. How to transform the linear system into a matrix equation.

  4. How to solve the matrix equation obtained directly with the inversion of a 2x2 matrix.

  5. How to set and solve the matrix equation in Python, with the packages ‘numpy’ and ‘numpy.linalg’.