Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This is coded in MATLAB. Please provide the code for part C and D. Exercise 3 A magic square is a n x n matrix
This is coded in MATLAB. Please provide the code for part C and D.
Exercise 3 A magic square is a n x n matrix composed of distinct positive integers from 1 to n? (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 9 4 We want to estimate numerically the probability p that a n x n matrix M is magic if we randomly fill its elements with numbers between 1 and n2. Here, for simplicity, we allow that the elements of M can be repeated: each element of M is randomly chosen between 1 and n? independently of the other elements, hence the same number can appear several times in the matrix. To estimate the probability, we will use a Monte-Carlo method. The theoretical aspects of this method are well beyond this course, but the idea is simple. This method will draw a large number N of random matrices and count the total number of times, k, we have a magic square. The probability p will then be approximated by . c) Write a function named probaMagic (n, mc iter) that has two input arguments. The first one, n. is the dimension of the matrix (that is n as used above). The second input argument mc.iter is the number of Monte-Carlo iterations (notation N in the introduction of this exercise). This function will draw mc.iter random matrices as returned by your function randomMatrix and count the number of times, say k, that the matrice is magic (using your function isMagic). Finally, the function probaMagic will return one single output which is the estimated probability p - k / mc.iter. d) Estimate the probabilities that anxn matrix whose integer elements are randomly chosen between 1 and n? is magic, for n = 2 and n = 3. You will use 107 Monte-Carlo iterations for this estimation. (Warning: This calculation may take several minutes on slow computers)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