Answered step by step
Verified Expert Solution
Question
1 Approved Answer
MATLAB: hey can i have help coding this problem in matlab. i post the same question before and i got wrong answer. Thanks Problem: Magic
MATLAB: hey can i have help coding this problem in matlab. i post the same question before and i got wrong answer. Thanks
Problem: Magic Square
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 Albrecht Drers engraving Melencolia I (year: 1514) is considered the first time a magic square appears in European art. Each row, column, and diagonal of Drers 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.
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Write a program to prove a series of numbers is indeed a 4 x 4 magic square. This problem is an excellent example of moving througha matrix of values similar to grid smoothing and comprehensive data validation and checks. You should be able to apply these same concepts to other problems and on the final exam.
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 x 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.
(d) Check for an arrangement of 4 x 4. If the matrix is not a 4 x 4, issue a statement to the command window informing the user of the mistake and ask the user to enter another matrix. This check should be repeated until the user enters a 4 x 4 matrix. You may assume the re-entered matrix contains only positive values; you do not need to re- check the new matrix for positive values, only for matrix dimensions.
(c) Check that all values are between 0 and 30. For each value found that is outside of this range, ask the user to replace the number with a value between 0 and 30. Keep asking the user until they have replace that particular number with a value between 0 and 30. The user input statement should include the location (row and column) of the number being replaced.
(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 criteria is not met, issue a statement to the command window informing the user they have not entered a magic square and ask the user if they wish to try another magic square. This question can be posed using either a text answer entered by the user (yes, no) or by using a menu. If the user chooses to run the program again, the entire program starting with step (a) should begin again.
(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 semimagic; the summation value is
called the magic constant.
2. If, in addition to criterion 1, each diagonal sums to the same value as the rows and columns, the magic square is classified as normal; ** for-loop or nested for-loop required in the solution. The use 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.
The magic constant for your magic square is 24. The classification for your magic square:
Semi-Magic. Normal. Perfect
Yes. Yes. Yes.
After this table appears, ask the user if they wish to try another magic square. This question can be posed using either a text answer entered by the user (yes, no) or by using a menu. If the user chooses to run the program again, the entire program starting with step (a) should begin again.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started