Reference no: EM132376585
Lab Assignment - Function
To keep in mind:
>>Unless otherwise indicated, your function should not print anything to the Command Window. Your function may be counted incorrect if it does.
>> Note that you are not required to use the suggested names of input variables and output variables, but you must use the specified function names.
1. One of the tedious tasks we do in Mathematics is solve equations manually. Why not write a MATLAB code to do it for us?
Write a function my_eqn_solver that takes two input arguments A (3x3 matrix) and B (3x1 vector) (as described below), in that order, and returns the solution matrix.
HINT: To solve the equation of the type:
A1X1 + A2X2 + A3X3 = B1
A4X1 + A5X2 + A6X3 = B2
A7X1 + A8X2 + A9X3 = B1
We first need to create two matrices
A1 A2 A3 B1 A = A4 A5 A6 and B = B2
A7 A8 A9 B3
Then,
C = A-1B or A\B gives you the solution vector C (3x1 vector). Remember, it is a matrix multiplication.
Example eq: X1 +2X2 + 3X3 = 1
-X1 + 2X3 = 0
X1 + 3X2 + X3 = 0
2. Write a function called even_index that takes a matrix, M, as input argument and returns a matrix that contains only those elements of M that are in even rows and columns. In other words, it would return the elements of M at indices (2,2), (2,4), (2,6), ..., (4,2), (4,4), (4,6), ..., etc. Note that both the row and the column of an element must be even to be included in the output. The following would not be returned: (1,1), (2,1), (1,2) because either the row or the column or both are odd. As an example, if M were a 5-by-8 matrix, then the output must be 2-by-4 because the function omits rows 1, 3 and 5 of M and it also omits columns 1, 3, 5, and 7 of M.
3. Write a function called mat_extract that takes two inputs: a matrix N and a scalar nonnegative integer n, in that order, where each dimension of N is greater than or equal to n. The function returns the n-by-n square array at the top right corner of N. This might look a
bit tricky, but it is worth scratching your head!
4. Write a function called sum_perimeter that computes the sum of the elements of an input matrix A that are on the "perimeter" of A. In other words, it adds together the elements that are in the first and last rows and columns. Note that the smallest dimension of A is at least 2.