Write a Matlab program to demonstrate common matrix operations. The user enters
ID: 3769619 • Letter: W
Question
Write a Matlab program to demonstrate common matrix operations. The user enters an option:
Option 1: Calculate the determinant and inverse of a matrix
Option 2: Calculate matrix multiplication of two matrices
Option 3: Calculate addition and element by element multiplication of two matrices.
The program first determines whether these options are valid:
Option 1: Determinant must be greater than 1.0 times 10 to the minus twelfth power)
Option 2: Columns of matrix 1 must equal rows of matrix 2
Option 3: The number of rows and columns of matrix 1 are the same as matrix 2
See the example output below Match that output. Note:
Stay in a while loop until the user enters option 0
If the user enters an option that is an invalid option, display:
Enter matrix option: 4
option: 4 is invalid
An option is invalid if it is less than 0 or greater than 3.
This example output was produced by using the load command to load assignment4.mat file of matrices available here. See the textbook Page 99 for an example of the load command that will put the saved matrices in the file into the Matlab workspace so that they can be used by the program. After saving the assignment4.mat file to the directory you are using for the assignment, you can execute:
load assignment4
or drag and drop the file name into the command window.
This is sample output to the command window when you execute a correct program using the matrices in the assignment4.mat file:
Matrix options:
1 determinant and inverse
2 matrix multiplication
3 add and element by element multiplication
0 end program
Enter matrix option: 1
Enter matrix 1: m11
Determinant of matrix: 5.000000
inverse =
-0.6000 0.8000 -0.4000
-0.4000 0.2000 0.4000
0.8000 -0.4000 0.2000
Matrix options:
1 determinant and inverse
2 matrix multiplication
3 add and element by element multiplication
0 end program
Enter matrix option: 1
Enter matrix 1: m12
option cannot be performed
Matrix options:
1 determinant and inverse
2 matrix multiplication
3 add and element by element multiplication
0 end program
Enter matrix option: 1
Enter matrix 1: m13
option cannot be performed
Matrix options:
1 determinant and inverse
2 matrix multiplication
3 add and element by element multiplication
0 end program
Enter matrix option: 2
Enter matrix 1: m21
Enter matrix 2: m22
Matrix multiplication answer:
answer =
10 13 8 15
6 6 0 5
Matrix options:
1 determinant and inverse
2 matrix multiplication
3 add and element by element multiplication
0 end program
Enter matrix option: 2
Enter matrix 1: m22
Enter matrix 2: m21
option cannot be performed
Matrix options:
1 determinant and inverse
2 matrix multiplication
3 add and element by element multiplication
0 end program
Enter matrix option: 3
Enter matrix 1: m31
Enter matrix 2: m32
Matrix addition answer:
answer =
2 4 2
0 4 0
4 2 2
Matrix element by element multiplication answer:
answer =
1 4 1
0 4 0
4 1 1
Matrix options:
1 determinant and inverse
2 matrix multiplication
3 add and element by element multiplication
0 end program
Enter matrix option: 3
Enter matrix 1: m32
Enter matrix 2: m31
Matrix addition answer:
answer =
2 4 2
0 4 0
4 2 2
Matrix element by element multiplication answer:
answer =
1 4 1
0 4 0
4 1 1
Matrix options:
1 determinant and inverse
2 matrix multiplication
3 add and element by element multiplication
0 end program
Enter matrix option: 4
option: 4 is invalid
Enter matrix option: 0
Matrix program ended
Program Requirements
Functions are used extensively in programming for common operations to avoid repeating code (for example, Matlab functions), and to make programs easier to understand. For assignment 4 you are required to use two functions:
getoption
checkoption
The getoption function is called before the while loop to get the first option, and at the end of the while loop to get the next option. It is not passed any parameters, and returns the option entered by the user. It stays in a loop until the user enters a valid option (integer 0 through 3). Your program will only be tested with integers.
The checkoption function verifies that the option can be performed. It is passed the option (1 through 3), matrix1 and matrix2. For option 1 (where only one matrix is used), you can pass it 0 for matrix2. It returns 1 if the option can be performed, 0 if it cannot.
The size of an array can be determined by the Matlab size function:
[r1 c1] = size(matrix1);
r1 = number of rows of matrix1
c1 = number of columns of matrix1
Match the above output. Note that for option 1, the program does not ask for matrix2. When the program ends, display “Matrix program ended” as above.
Error checking
Note the error checking in the example output:
For Option 1, the matrix must be square, and the determinant > 1.0e-12
For Option 2, check that the inner matrix dimensions agree (columns of matrix 1 = rows of matrix 2)
For Option 3, check that the number of rows and columns of matrix 1 are the same as matrix 2
Check that the user entered a valid option (integer from 0 through 3). You can assume that the user will enter an integer.
Explanation / Answer
MATLAB script
% Two matrices for operations
a = [ 1 2 3; 2 3 4; 1 2 5]
b = [ 2 1 3 ; 5 0 -2; 2 3 -1]
% Printing the three options
fprintf("Option 1: Determinant and Inverse ");
fprintf("Option 2: matrix multiplication ");
fprintf("Option 3: Addition and element by element multiplication ");
fprintf("Option 0: End Program ");
% Take input from user
option = input(" Enter Option 1 or 2 or 3 ");
% While loop code
while(option != 0)
option = input(" Enter Option 1 or 2 or 3 ");
if option==1
det(a)
inv(a)
else if option==2
res = a * b
fprintf("%f", res)
else if option==3
res = a + b
fprintf("%f", res)
res = a .* b
fprintf("%f", res)
else
fprintf("Invalid option ")
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.