MATLAB Problems
Engg. Software Laboratory
Unless otherwise indicated, you may assume that each function will be given the correct number of inputs and
that those inputs have the correct dimensions. For example, if the input is stated to be three row vectors of four
elements each, your function is not required to determine whether the input consists of three two-dimensional
arrays, each with one row and four columns.
Unless otherwise indicated, your function should not print anything to the Command Window.
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.
To use auto-grader: type “grader” in Command Window (without quotes):
‘+ Press (108) to check for any particular problem,
+ Press Oto EXIT
. Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-
statements, possibly nested, to return the three elements of the vector as three scalar output
arguments in non-decreasing order, ie,, the first output argument equals the smallest element of,
the input vector and the last output argument equals the largest element. NOTE: Your function may
not use any built-in functions, e.g, sort, min, max, median, etc.
If we list all the natural numbers up to 15 that are multiples of 3 or 5, we get 3, 5, 6, 9, 10, 12 and 15.
‘The sum of these multiples is 60. Write a function called sum3andSmuls that returns the sum of all
the unique multiples of 3 or 5 up to n where n is a positive integer and the only input argument of,
the function.
. Write a function called intquad that takes as its input arguments two scalar positive integers named
n and m in that order. The function returns Q, a 2n-by-2m matrix. Q consists of four n-by-m
submatrices. The elements of the submatrix in the top left corner are all Os, the elements of the
submatrix at the top right are 1s, the elements in the bottom left are 2s, and the elements in the
bottom right are 3s.
. Write a function called reverse diag that creates a square matrix whose elements are 0 except for
1s on the reverse diagonal from top right to bottom left. The reverse diagonal of an n-by-n matrix
consists of the elements at the following indexes: (1, n), (2, n-1), (3, n-2), ... (n, 1). The function
takes one positive integer input argument named n, which is the size of the matrix, and returns the
matrix itself as an output argument. Note that using the built-in functions eye and diag are not
allowed. (Hint: you can index into a matrix with a single index and MATLAB will handle itas if it was
a vector using column-major order.)
. Write a function called holiday that takes two input arguments called month and day; both are scalar
integers representing a month (1-12) and a day (1-31). You do not need to check that the input is
valid. The function returns a logical true if the specified date is a holiday; if not, it returns false. For
the purposes of this exercise, select year 2018, and Saturday and Sunday in each week as holiday.6. Write a function that computes the time required to calculate the inverse of random matrix of size
NéN, and generates a plot for “CPU Computation time Vs Matrix Size”, where Matrix Dimention(N)
is large enough to generate a plot for Computation Time upto 10 mins. (You Need to submit the plot
only, not the function file.)
7. Write a function called triangle_wave that computes the sum
(-1* CDF sin(2k + De) +1)t)
yore (2k + 1)?
for each of 1001 values of tuniformly spaced from 0 to 4m inclusive. The input argument is a scalar
non-negative integer n, and the output argument is a row vector of 1001 such sums—one sum for
each value of ¢ (You can test your function by calling it with n == 20 or greater and plotting the
result and you will see why the function is called “trianglewave"). You can also vary value of n,
from 1 to 10, and then plot your results, to see the effect of variation of value of n,
You need to submit the function file only, not plots.
8. Write a function called spiral diag sum that takes an odd positive integer n as an input and
computes the sum of all the elements in the two diagonals of the n-by-n spiral matrix. For example,
starting with the number 1 and moving to the right in a clockwise direction, a 5-by-5 spiral is formed
as follows:
21 22 23 24 25
20 #7 8 & 10
mM 6 & 2 it
iw S 4 12
i? 16 15 14 13
‘The sum of the red elements above is 101. Hint: the problem does not ask for the matrix itself.