This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Chapter 7 Magic Squares With origins in centuries old recreational mathematics, magic squares demonstrate Matlab array operations. Figure 7.1. Lo Shu. (Thanks to Byerly Wiser Cline.) Copyright c 2009 Cleve Moler Matlab R is a registered trademark of The MathWorks, Inc. TM August 8, 2009 1 2 Chapter 7. Magic Squares Magic squares predate recorded history. An ancient Chinese legend tells of a turtle emerging from the Lo river during a flood. The turtles shell showed a very unusual pattern a threebythree grid containing various numbers of spots. Of course, we do not have any eyewitness accounts, so we can only imagine that the turtle looked like figure 7.1. Each of the three rows, the three columns, and the two diagonals contain a total of 15 spots. References to Lo Shu and the Lo Shu numerical pattern occur throughout Chinese history. Today, it is the mathematical basis for Feng Shui , the philosophy of balance and harmony in our surroundings and lives. An nby n magic square is an array containing the integers from 1 to n 2 , arranged so that each of the rows, each of the columns, and the two principal diagonals have the same sum. For each n > 2, there are many different magic squares of order n , but MATLABs function magic(n) generates a particular one. Matlab can generate Lo Shu with A = magic(3) which produces A = 8 1 6 3 5 7 4 9 2 The command sum(A) sums the elements in each column to produce 15 15 15 The command sum(A) transposes the matrix, sums the columns of the transpose, and then transposes the results to produce the row sums 15 15 15 The command sum(diag(A)) sums the main diagonal of A, which runs from upper left to lower right, to produce 15 3 The opposite diagonal, which runs from upper right to lower left, is less important in linear algebra, so finding its sum is a little trickier. One way to do it makes use of the function that flips a matrix upsidedown. sum(diag(flipud(A))) produces 15 This verifies that A has equal row, column, and diagonal sums. Why is the magic sum equal to 15? The command sum(1:9) tells us that the sum of the integers from 1 to 9 is 45 . If these integers are allocated to 3 columns with equal sums, that sum must be sum(1:9)/3 which is 15 . There are eight possible ways to place a transparency on an overhead projec tor. Similarly, there are eight magic squares of order three that are rotations and reflections of A . The statements for k = 0:3 rot90(A,k) rot90(A,k) end display all eight of them. 8 1 6 8 3 4 3 5 7 1 5 9 4 9 2 6 7 2 6 7 2 4 9 2 1 5 9 3 5 7 8 3 4 8 1 6 2 9 4 2 7 6 7 5 3 9 5 1 6 1 8 4 3 8 4 3 8 6 1 8 9 5 1 7 5 3 2 7 6 2 9 4 These are all the magic squares of order three. The 5 is always in the center, the other odd numbers are always in the centers of the edges, and the even numbers are always in the corners. 4 Chapter 7. Magic Squares Figure 7.2. Albrect Durers Melencolia, 1514....
View Full
Document
 Spring '11
 Adams
 matlab

Click to edit the document details