As early as 650 BC, mathematicians had been composing magic squares, a sequence
ID: 3688017 • Letter: A
Question
As early as 650 BC, mathematicians had been composing magic squares, a sequence of n numbers arranged in a square such that all rows, columns, and diagonals sum to the same constant. Used in China, India, and Arab countries for centuries, artist Albrech Durer’s engraving Melencolia I (year: 1514) is considered the first time a magic square appears in European art. Each row, column, and diagonal of Durer’s magic square sums to 34. In addition, each quadrant, the center four squares, and the corner squares all sum to 34. An example of a “magic square” is displayed below:
Write a MATLAB program to prove a series of numbers is indeed a 4 × 4 magic square. Your program should complete the following steps, in this order:
a) Ask the user to enter their proposed magic square in a single input statement (e.g. [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16] – note this example is a 4 × 4 matrix, but NOT a magic square). You may assume the user will enter whole numbers; they will not enter either decimal values or text.
b) Check that all values are positive; **for-loop or nested for-loop required in the solution. If one or more of the values in the matrix are negative or zero, issue an error statement to the command window informing the user of the mistake and exit the program (without executing the remainder of the program). This check should work even if the user does not enter a 4 × 4 matrix; it should work regardless of the size of the matrix entered.
c) Check for an arrangement of 4 × 4. If the matrix is not a 4 × 4, issue an error statement to the command window informing the user of the mistake and exit the program.
d) Determine if the matrix is a form of a magic square. The minimum requirement to be classified as a magic square is each row and column sums to the same value. **for-loop or nested for-loop required in the solution. If this criterion is not met, issue a statement to the command window informing the user they have not entered a magic square and exit the program (reminder: this is not an error).
e) Determine the classification of the magic square using the following requirements:
1. If each row and column sums to the same value, the magic square is classified as “semi-magic”; the summation value is called the magic constant. **for-loop or nested for-loop required in the solution.
2. If, in addition to criterion #1, each diagonal sums to the same value, the magic square is classified as “normal”; **for-loop or nested for-loop ENGR1315 Introduction To Engineering – HW #16 & #17 Spring 2016 required in the solution. The user of built-in functions such as diag, fliplr, rot90, trace or similar built-in functions is forbidden.
3. If, in addition to #1 and #2, the largest value in the magic square is equal to 16, the magic square is classified as “perfect”. Format your magic square classification similar to the format shown below. You may choose to format your table differently, but each classification should contain a “yes” or “no” next to each magic square category. Sample output on command window: The magic square constant for your magic square is 24. The classification for your magic square:
Semi-magic Normal Perfect
Yes Yes No
A few test cases for you to consider:
• Albrecht Durer magic square: [16, 3, 2, 13; 5, 10, 11, 8; 9, 6, 7, 12; 4, 15, 14, 1];
• Chautisa Yantra magic square: [7, 12, 1, 14; 2, 13, 8, 11; 16, 3, 10, 5; 9, 6, 15, 4];
• Sangrada Familia church, Barcelona magic square: [1, 14, 14, 4; 11, 7, 6, 9; 8, 10, 10, 5; 13, 2, 3, 15];
• Random magic square: [80, 15, 10, 65; 25, 50, 55, 40; 45, 30, 35, 60; 20, 75, 70, 5];
• Steve Wozniak’s magic square: [8, 11, 22, 1; 21, 2, 7, 12; 3, 24, 9, 6; 10, 5, 4, 23].
Explanation / Answer
Steps for Durers matrix
· Step-2:Separate the elements of a row with blanks or commas.
· Step-3:Use a semicolon, ; , to indicate the end of each row.
· Step-4:Surround the entire list of elements with square brackets, [ ].
A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
The magic Function
MATLAB actually has a built-in function that creates magic squares of almost any size. Not surprisingly, this function is named magic:
B = magic(4)
B =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
This matrix is almost the same as the one in the Dürer engraving and has all the same "magic" properties; the only difference is that the two middle columns are exchanged.
You can swap the two middle columns of B to look like Dürer's A. For each row of B, rearrange the columns in the order specified by 1, 3, 2, 4:
A = B(:,[1 3 2 4])
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Note:Just substitute values in a command window to get the remaining types of magic squares.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.