As early as 650 BC, mathematicians had been composing magic squares, a sequence
ID: 3684510 • 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.
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 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
Explanation / Answer
matlab program:
mat=zeros(4,4);
rowSum=zeros(1,4);
colSum=zeros(1,4);
diagonalSum=0;
sizeMat=size(mat);
for i=1:sizeMat(1)
for j=1:sizeMat(2)
mat(i,j)=input('Enter row column element');
end
end
mat
sem='yes';
normal='yes';
perfect='yes';
for i=1:sizeMat(1)
for j=1:sizeMat(2)
rowSum(1,i)=rowSum(1,i)+mat(i,j);
end
end
for i=1:sizeMat(1)
for j=1:sizeMat(2)
colSum(1,i)=colSum(1,i)+mat(j,i);
end
end
rowSum
colSum
sum=rowSum(1,1);
for i=1:sizeMat(1)
if sum~=rowSum(1,i) || sum~=colSum(1,i)
sem='no';
end
end
if(strcmp(sem,'yes'))
for i=1:sizeMat(1)
diagonalSum=diagonalSum+mat(i,i);
end
if sum~=diagonalSum
normal='no';
end
else
normal='no';
end
max=0;
if(strcmp(normal,'yes'))
for i=1:sizeMat(1)
for j=1:sizeMat(2)
if max<mat(i,j)
max=mat(i,j);
end
end
end
if(max~=16)
perfect='no';
end
else
perfect='no';
end
disp('semi-magic normal perfect');
fprintf(' %s %s %s',sem,normal,perfect);
output:
Enter row column element1
Enter row column element2
Enter row column element3
Enter row column element4
Enter row column element3
Enter row column element4
Enter row column element1
Enter row column element2
Enter row column element4
Enter row column element3
Enter row column element2
Enter row column element1
Enter row column element2
Enter row column element1
Enter row column element4
Enter row column element3
mat =
1 2 3 4
3 4 1 2
4 3 2 1
2 1 4 3
rowSum =
10 10 10 10
colSum =
10 10 10 10
semi-magic normal perfect
yes yes no>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.