Question: Using Matlab! 1. A magic square is a n n matrix composed of distinct positive integers from 1 to n^2 (so each integer appears only
Using Matlab!
1. A magic square is a n n matrix composed of distinct positive integers from 1 to n^2 (so each integer appears only once) such that the sum of the matrix elements in each row, column and diagonal is equal to the same magic constant.


For example, the matrix below is a magic square of dimension 3 with 15 as its magic constant: 6 1 8 7 5 3 2 94 a) Write a function named randomMatrix(n) that returns a nx n matrix with its elements that are integers randomly chosen between 1 and n?. Hints: As a reminder, the built-in MATLAB function rand (n) generates a nxn matrix whose elements are real numbers (i.e., double) uniformly distributed between 0 and 1. Generating numbers uniformly distributed between 0 and a can simply be achieved by calling a * rand (n). == b) Write a function named isMagic that takes one single matrix input M and returns the numerical output 1 if the matrix is magic, o else. Hints: if A is a matrix, then sum(A, 2) is a column vector containing the sum of each row. See MATLAB's documentation. if v is a vector, the command all(v 5) returns 1 if all the element values of v are equal to 5, else it returns 0. (5 is taken as an illustrative example and could be any other value!) c) Program validation. Use MATLAB's builtin function magic(n) to generate a n x n (n > 3) magic matrix randomly (see MATLAB's documentation). Print the matrix and test the correctness of isMagic. Use randomMatrix(n) to generate a nxn (n > 3) random matrix. Print the matrix and test the correctness of isMagic
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
