Question: Write a program in MATLAB to create a magic square. A magic square is a square of numbers in which the sum of each column,

Write a program in MATLAB to create a magic square. A magic square is a square of numbers in which the sum of each column, the sum of each row, and the sum of each diagonal is the same. One method to generate a magic square is the Siamese method. Write a function* to generate magic square of size 'n' using Siamese method. Here are a couple of requirements:

- 'n' must be greater or equal to 3. If n is less than 3, the function must raise an error or print "Input is less than 3".

- 'n' must be odd. If the input 'n' to the function is even, the functino must raise an error or print "Input is even integer".

SIAMESE:

-Start by making a matrix using the 'zeros()' command.

-Create a loop with the iterator 'i' to iterate through every position in the magic square. In each iteration, the current number in the sequence will be put to position.

-Initialize integer variables 'x' and 'y', where 'x' represents the row and 'y' represents the column, of the current number.

-Start with 'x' as 1 and 'y' as '(n-1)/2 + 1. Place a '1' there.

-Change 'x' and 'y' such that they point to the position upward by 1 and to the right by 1.

-If the position is empty (i.e 0), put the next number there. If the position is taken (i.e not 0), move back to the previous position, move down by 1. If the position is daken move down by 1. Keep moving down until and empty space is reached.

THINGS TO CONSIDER:

-Out-of-bound: When the position is at an edge (top row, bottom row, left-most column, right-most column), wrap around to the opposite edge.

-To check if your function generates the correct answer, you can use the built-in function magic(n).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!