You are on page 1of 8

Cambridge Books Online

http://ebooks.cambridge.org/

Mathematical Explorations with MATLAB


Ke Chen, Peter Giblin, Alan Irving
K. Chen, Peter J. Giblin, A. Irving
Book DOI: http://dx.doi.org/10.1017/CBO9780511624117
Online ISBN: 9780511624117
Hardback ISBN: 9780521630788
Paperback ISBN: 9780521639200

Chapter
8 - Magic Squares pp. 101-107
Chapter DOI: http://dx.doi.org/10.1017/CBO9780511624117.009
Cambridge University Press

8
Magic Squares

Aims of the project


Magic squares have been known for centuries. This project explores
their properties from the perspective of matrix algebra, that is, using
addition and multiplication of matrices. The project is not concerned
with the number-theoretic problem of finding magic squares containing
consecutive integers. The project is self-contained, but it may be of
interest to know that several of the mathematical results come from the
article [16]. This article also contains other results on the same subject.

Mathematical ideas used


Matrix multiplication, row reduced echelon form and solution of linear
equations are used. Also, for example, 3 x 3 matrices are regarded as
lying in nine-dimensional space R 9 , and subspaces of R 9 are considered.
(There is no requirement to know the definition of an abstract vector
space: all spaces are contained in some R n .) The ideas of linear independence and basis are used. It is necessary to know that, in a subspace
X of dimension r in R n , a set of r vectors in X which is linearly independent automatically spans X and so forms a basis. It is necessary to
know the definitions of eigenvalues and eigenvectors, and to use these in
a simple argument involving powers of matrices.

MATLAB techniques used


The project is about matrices, so you will need the techniques described
in Chapter 2. At one point there is an M-file with several 'for' loops,
and 'if statements, so you will need to understand these ideas. See
Chapter 3. Note that the project is somewhat 'open-ended': students
who work quickly might like to go on to the final section, on 5 x 5 magic
squares, which could be regarded as optional.
101
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:40:20 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.009
Cambridge Books Online Cambridge University Press, 2016

102

Magic Squares

8.1 Introduction
This project is about magic squares. An n x n magic square is an n x n
matrix of real numbers with the following property:
All rows, all columns and the two 'main' diagonals of the matrix add up to the
same number, r say, called the magic constant.

For example,

has this property for r = 6. The two main diagonals in this case are
3 + 2 + 1 (top left to bottom right) and 2 + 2 + 2 (top right to bottom
left).
There are many algorithms for producing magic squares with the additional property that the entries are the integers 1,2,... , n 2 in some
order. For example, with n = 3,

(8.1)

The MATLAB function magic does this (try typing magic(3)). In this
project we shall not go into these algorithms but instead investigate the
algebra underlying magic squares, using your knowledge of matrices and
solution of linear equations.

8.2 M a g i c squares size 3 x 3


(i) C o n s i d e r a g e n e r a l 3 x 3 m a t r i x
n
A = (dij) = I a 2 l

012 ai3
a 2 2 CL23

Thus there are nine entries a n , . . . , 033. Explain why, writing down the
conditions for all rows, all columns and the two main diagonals to add
to the same number r, we get the condition M v = 0, where M is the

Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:40:20 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.009
Cambridge Books Online Cambridge University Press, 2016

8 . 2 Magic

squares

size

3 x 3

103

8 x 10 matrix
/ - 1 1 1
- 1 0 0
- 1 0 0
- 1 1 0
- -11 0 1
-1 0 0
-1 1 0

1
0
0
0
0
1
0

0
1
0
1
0
0
0

0
1
0
0
1
0
1

0 0 0 0 \
1 0 0 0
0
1 1 1
0 1 0 0
0 0 1 0
1 0 0 1
0 0 0 1

- 1 0 0 1 0 1 0 1 0 0 /

and v is the column vector


(r

an

ai 2

13

a 22

(This really is just a matter of blindly applying the definition!)


(ii) Use the MATLAB function rref to find the reduced row echelon
form of the matrix M. Why does it follow that, if A is to be magic, then
three of the entries in the matrix A, say those in the bottom row, can be
chosen arbitrarily and the rest are then determined? (Note that, after
row reduction of M, the top row of the reduced matrix can be ignored
since it just gives the equation
r = sum of elements in bottom row of .A,
and this is now the only equation involving r.)
Another way of saying this is that, writing the rows of a 3 x 3 matrix
in succession to give a vector
(an ai 2 ai 3 a2i a22 a23 a3i a32 a33) G R ,
the subspace of magic squares has dimension 3.
(iii) Verify that
0

are all magic squares. Why does it follow from (ii) that every 3x3 magic
square A can be written uniquely in the form of a linear combination
A=

H~

Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:40:21 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.009
Cambridge Books Online Cambridge University Press, 2016

104

Magic Squares

for Ai, A2, A3 G R? (Hint: You are being asked to show that E\, E2, E3
form a basis for the three-dimensional subspace of magic squares. Since
there are three matrices E{ it is enough to show that they are linearly
independent, considered as vectors in R 9 .) What are the A^ when A is
given by (8.1) above?
(iv) A 3 x 3 matrix has, in addition to its two 'main' diagonals, four
other 'broken' diagonals:
Gil + 23 + 32, Gi2 + a 21 + 33, &13 + O>21 + ^32, &12 + &23 + &31-

If, for a magic square A, we require in addition all the broken diagonals to add up to the same magic constant r, then the square is called
pandiagonal.
In general, a n n x n matrix has two main diagonals and 2n 2 broken
diagonals, and if all these and all the rows and all the columns add up
to the same r, then the matrix is a pandiagonal magic square.
In the 3 x 3 case, expand your matrix M by the addition of four rows
corresponding to the four broken diagonals and use the new matrix (Ml
say) to show that the only pandiagonal magic squares are XE3 for A G R.
(Thus you should find from the row reduced echelon form of M l that
only one entry of A is now arbitrary, and the others are all equal to this
one.)
(v) We now look at products of 3 x 3 magic squares. By (iii) the product
of two such squares has the form
(X1E1 + X2E2 + X3E3)(fi1E1

+ /i 2 2 + /X3S3).

(8.2)

Let

Verify that all products EiEj (including i = j) can be written as linear


combinations of / , P, E3 (I being the 3 x 3 identity matrix), and hence
that (8.2) is such a linear combination.
Deduce that the product of an even number of 3 x 3 magic squares is
a linear combination of / , P, E%. Finally deduce that the product of an
odd number of 3 x 3 magic squares is also magic. (For the 'even' case,
using the result just proved, you need to show that multiplying together
linear combinations of / , P, E3 gives again a linear combination of these
three matrices. This can be expressed by saying 'the subspace of R 9

Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:40:21 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.009
Cambridge Books Online Cambridge University Press, 2016

8.3 Magic squares size 4 x 4

105

spanned by / , P, 3 is closed under matrix multiplication'. Note this is


not true of the subspace of magic squares itself. For the product of an
odd number of magic squares you now know that this can be written as
the product of a linear combination of / , P, E$ and a linear combination
of
EUE2,ES.)
(vi) Explain why the condition that all the rows of a 3 x 3 matrix A add
up to r is equivalent to the condition that (1,1,1) is an eigenvector of
A with corresponding eigenvalue r. Explain also why this last condition
implies that, for any integer k > 0, (1,1,1) is an eigenvector of Ak with
eigenvalue rk. (This is a standard property of eigenvectors and follows
from the definition.) Now deduce from (v) that if A is a 3 x 3 magic
square with magic constant r, and k is odd, then Ak is a magic square
with magic constant rk.

8.3 M a g i c squares size

4 x 4

(i) Set up a matrix corresponding to M above in the 4 x 4 case. It will be


a 10 x 17 matrix. Use MATLAB to show that the rank is 9 and deduce
that in R 1 6 the 4 x 4 magic squares form a subspace of dimension 8.
(ii) Let p = [p(l) p(2) p(3) p(4)] be a permutation of 1, 2, 3, 4, i.e. the
numbers 1, 2, 3, 4 in some order. The permutation matrix A corresponding to p is the 4 x 4 matrix which is all zeros except for A(i,p(i)) = 1
for i = 1, 2, 3,4. So, for example, if p = [3 1 2 4] then
/ 0 0 1 0
1 0 0 0
A =
0 1 0 0
V0 0 0 1
Note that this is in fact magic.
There are seven other permutations of 1, 2, 3, 4 which give magic
squares in this way. Find them. As a hint, here is one fairly brute force
way to enumerate the permutations of 1, 2, 3, 4 in MATLAB.

Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:40:21 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.009
Cambridge Books Online Cambridge University Press, 2016

106

Magic Squares

for a=l:4
for b=l:4
if b~=a
for c=l:4
if c~=a & c~=b
for d=l:4
if d~=a k d~=b & d~=c
p= [a b c d] ;
end;
end;
end;
end;
end;
end;
end;
After the permutation p is found, you will want to calculate the permutation matrix, then test whether it is magic. The quickest way to do this
is to use the criterion M v = 0 as in Question (i) in 8.2. Now, however,
M will be the 10 x 17 matrix you have just found, and v will be a 17 x 1
column vector of the form (1 a n . . . a^)T.
(iii) Add more rows to your matrix M to test for pandiagonal 4 x 4 magic
squares (see Question (iv) in 8.2 for the definition). Call the resulting
matrix M l . What is the dimension of the space now? Are any of these
given by permutation matrices?
(iv) By finding the reduced row echelon form of your matrix M l , show
that every 4 x 4 pandiagonal magic square has the form
a b c + d-\-e

b+c-d+e
-a + 2c + 2d
2b

a + b + c-\-d + e a+bcd+e

a + 6 + 3c + <i e

b+c+d-e
a-2c + 2e

b-c+d+e
a

2c

-6+c+d+e
-a + 2b + 2c
2d

2e

where a, 6, c, a7, e are arbitrary real numbers. Write an M-file to generate


such matrices and use MATLAB to find the eigenvalues of several examples. Do you have any conjectures about the general result here? Can
you explain one of the eigenvalues in the same way as Question (vi) in
8.2? (One relation between eigenvalues follows from the fact that the
sum of the eigenvalues of any square matrix is equal to the trace of the

Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:40:21 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.009
Cambridge Books Online Cambridge University Press, 2016

8.4 Magic squares size 5x5

(optional)

107

matrix, that is, the sum of the entries in the leading diagonaltop left
to bottom right.)

8.4 M a g i c squares size 5 x 5

(optional)

Find out what you can about the dimension of the space of 5 x 5 magic
squares, and of the pandiagonal ones. Are the pandiagonal ones spanned
in this case by permutation matrices? If so, find a basis consisting of
permutation matrices. (You can find details in the article [16].)

Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:40:21 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.009
Cambridge Books Online Cambridge University Press, 2016

You might also like