Wite a matlab program to demonstrate common matrix


Write a Matlab program to demonstrate common matrix operations:

Option 1: Matrix addition and element by element multiplication (verify that dimensions are the same)

Option 2: Matrix multiplication (verify that the number of columns of matrix 1 is equal to the number of rows of matrix2)

Option 3: Transpose of a matrix

Option 4: Determinant and inverse of a matrix (display message "Singular matrix" if determinant is less than 1.0 times 10 to the minus tenth power)

Option 5: Identity, ones, zeros, random matrices (for identity matrix, create square matrix, only use number of rows (n) to create n by n matrix)

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:

      option: 6 is invalid

  • See checks listed below, and illustrated in the example output

This is sample output to the command window when you execute a correct program using the matrices in the matrix.mat file:

Matrix options:

1 add, element multiplication, 2 multiply

3 transpose,4 determinant

5 identity, ones, zeros, random matrices, 0 end program

Enter matrix option: 1

Perform matrix addition, element by element multiplication

Enter matrix 1: o1a

Enter matrix 2: o1b

matrix_add =

matrix_add =

2          0          3

3          2          4

3          5          2

matrix_mult =

1          0          2

2          1          4

0          6          1

Matrix options:

1 add, element multiplication, 2 multiply

3 transpose,4 determinant

5 identity, ones, zeros, random matrices, 0 end program

Enter matrix option: 1

Perform matrix addition, element by element multiplication

Enter matrix 1: o1a

Enter matrix 2: o1c

Dimensions do not match

Matrix options:

1 add, element multiplication, 2 multiply

3 transpose,4 determinant

5 identity, ones, zeros, random matrices, 0 end program

Enter matrix option: 2

Perform matrix multiplication

Enter matrix 1: o2a

Enter matrix 2: o2b

matrix_mult =

4          1

10        3

7          2

Matrix options:

1 add, element multiplication, 2 multiply

3 transpose,4 determinant

5 identity, ones, zeros, random matrices, 0 end program

Enter matrix option: 2

Perform matrix multiplication

Enter matrix 1: o2a

Enter matrix 2: o2c

Inner matrix dimensions must agree

Matrix options:

1 add, element multiplication, 2 multiply

3 transpose,4 determinant

5 identity, ones, zeros, random matrices, 0 end program

Enter matrix option: 3

Transpose a matrix

Enter matrix: o3a

transpose =

1          1

2          0

2          1

Matrix options:

1 add, element multiplication, 2 multiply

3 transpose,4 determinant

5 identity, ones, zeros, random matrices, 0 end program

Enter matrix option: 4

Calculate determinant and inverse of a matrix

Enter matrix: o4a

determinant =

-2

inverse =

-1.0000 0.5000 1.0000

0          0.5000          0

2.0000 -1.5000 -1.0000

Matrix options:

1 add, element multiplication, 2 multiply

3 transpose,4 determinant

5 identity, ones, zeros, random matrices, 0 end program

Enter matrix option: 4

Calculate determinant and inverse of a matrix

Enter matrix: o4b

Singular matrix no inverse

Matrix options:

1 add, element multiplication, 2 multiply

3 transpose,4 determinant

5 identity, ones, zeros, random matrices, 0 end program

Enter matrix option: 5

Create identity, ones, zeros, random matrices

Enter number of rows: 3

Enter number of columns: 2

identity_matrix =

1          0          0

0          1          0

0          0          1

ones_matrix =

1          1

1          1

1          1

 

zeros_matrix =

0          0

0          0

0          0

random_matrix =

0.6948          0.0344

0.3171          0.4387

0.9502          0.3816

Matrix options:

1 add, element multiplication, 2 multiply

3 transpose,4 determinant

5 identity, ones, zeros, random matrices, 0 end program

Enter matrix option: 6

option: 6 is invalid

Enter matrix option: 0

Matrix program ended

Program Requirements

Error checking

Note the error checking in the example output:

  • For Option 1, check that the dimensions match

  • For Option 2, check that the inner matrix dimensions agree (columns of matrix 1 = rows of matrix 2)

  • For Option 3, check that the matrix is not singular: if the absolute value of the determinant is less than 1.0e-10 it is singular. If singular, the inverse is not calculated.

  • Check that the user entered a valid option (integer from 0 through 5). You can assume that the user will enter an integer.

  • Stay in a loop asking for a user option until the user enters 0 for the option. See the example output.

Function to get option

Use a function to get the option. Stay in a loop in the function until the user enters, 0 through 5 (you can assume that the user will enter an integer). The function does not get any parameters from the script, and returns the option (1 through 5) to the script.

Request for Solution File

Ask an Expert for Answer!!
MATLAB Programming: Wite a matlab program to demonstrate common matrix
Reference No:- TGS01208790

Expected delivery within 24 Hours