MATLAB PROJECT 1
Please read the Instructions located on the Assignments page prior to working on the Project.
BEGIN with creating Live Script Project1.
Note: All exercises in this project will be completed in the Live Script using the Live Editor.
Please refer to the MATLAB video that explains how to use the Live Script: https://www.mathworks.com/videos/using-the-live-editor-117940.html?s_tid=srchtitle
The final script has to be generated by exporting the Live Script to PDF.
Each exercise has to begin with the line
Exercise#
You should also mark down the parts such as (a), (b), (c), and etc. This makes grading easier.
Important: we use the default format short for the numbers in all exercises unless it is specified otherwise. We do not employ format rat since it may cause problems with running the codes and displaying matrices in Live Script. If format long has been used, please make sure to return to the default format in the next exercise.
.
Exercise1 (4 points) Difficulty: Easy
Jordan Block is a square matrix with a scalar r on the main diagonal and 1’s on the diagonal right above it. All other entries are zero.
**Create a function in a file that begins with
function J=jord(n,r)
which, when possible, produces an Jordan matrix with a scalar r on its main diagonal.
**First, the function will verify whether n is a positive integer number. You can use a built-in MATLAB function such as mod or fix. **If n is not a positive integer number, the code has to assign an empty matrix to J and display a message that 'n is not positive integer and Jordan Block cannot be built'- you can use disp, sprintf, or fprintf commands.
**If n is a positive integer, you will proceed with constructing a Jordan Block matrix J as described at the beginning of this exercise.
After the function jord is created and saved in a file, we return to the Live Script.
**First, we set the format mode by typing format
format compact
**Then, we print in the Live Script the created function jord by typing
type jord
**Next, we input
r=rand(1)
and run the function J=jord(n,r) for each of the following n to get either a non-empty output J or an empty matrix and a message that Jordan Block cannot be built:
(a) n=0;
(b) n=-2;
(c) n=3.5;
(d) n=-2.5;
(e) n=4;
Exercise2 (5 points) Difficulty: Moderate
DESCRIPTION: In this part of the project, you will create a function added in a file. You will code the sum of two matrices A and B, when they are of the same size, by using any of these two definitions: (1) the sum of A and B is the matrix C whose columns are the sums of the corresponding columns of A and B or (2) the sum of A and B is the matrix C whose entries are the sums of the corresponding entries of the matrices A and B. Then, you will verify if your output matrix C matches the output of a MATLAB built-in function for the sum, A+B.
**Create the function which begins with: function C = added(A,B)
**First, your function has to verify whether the input matrices A and B have the same size. If not, output a message 'the matrices are not of the same size and cannot be added', and assign an empty matrix to C. After that, the program terminates.
**If the matrices can be added, you will continue with calculating the sum C of A and B using one of the two definitions of the sum of two matrices (see above) – to code it, you can employ “for” loop or vectorized statement. Output and display the matrix C.
**Next, you will use a logical “if” statement to verify whether the calculated matrix C matches the output for a built-in MATLAB function A+B. If the outputs C and A+B do not match, program an output message 'check the code!' – use disp or fprintf commands – and, if you receive this message, you will need to correct the code and re-run Section. This will be the end of your function added. Save it in your Current folder in MATLAB.