Question

In: Computer Science

B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix...

B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix of size 2 × 2 and returns back the inverse of that matrix if one exists. If no inverse exists, print back a suitable error message. You can safely assume that we will test your code on square matrices of size 2 × 2 only. You can always verify your answer by using the inbuilt inverse function in MATLAB, however, you cannot use the inverse function directly when writing your code. Here is the starter code that we are providing to help you get started:

%x is the input 2x2 matrix. function inverse_ret = findInverse(x)

. . .

%inverse_ret is the variable that will contain the inverse of the input x end

Solutions

Expert Solution

Inverse function:

%x is the input 2x2 matrix.
function inverse_ret = findInverse(x)
  
d = x(1,1) * x(2,2) - x(1,2) * x(2,1);
  
if( d == 0 )
disp( "The given matrix is singular (not invertible)" );
else
inverse_ret = [ x(2,2), -1 * x(1,2); -1 * x(2,1), x(1,1)] / d;
end
%inverse_ret is the variable that will contain the inverse of the input x
end

verifying code:

result :

1) if x=[4,7;2,6]

2)if x=[3,4;6,8]


Related Solutions

A. Write a function in MATLAB called findTranspose that takes in as input any matrix or...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or any vector and simply returns back the transpose of that input. You can always verify your answer by using the inbuilt transpose function in MATLAB, however, you cannot use the transpose function directly when writing your code. Here is the starter code that we are providing to help you get started %x is the input vector or matrix. You can find the %size that...
Write a Matlab function for a matrix that takes in a matrix in echelon form and...
Write a Matlab function for a matrix that takes in a matrix in echelon form and will return the row canonical form. The function cannot use rref, or any other matlab built in functions.
MATLAB Function: (Backward substitution) Write a function that takes an upper triangular matrix U, and a...
MATLAB Function: (Backward substitution) Write a function that takes an upper triangular matrix U, and a vector b as input and returns the solution of the linear system U x = b. Use this function in the case when U is made of the numbers [(3,2,1),(0,5,4),(0,0,6)], and b = [1,1,−6]^T.
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal...
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal matrix given as two vectors: n×1 vector v representing the main diagonal and (n−1)×1 vector w representing the upper diagonal. Have this function output the Cholesky factor of the matrix as a vector for the main diagonal and a vector for the upper diagonal and output the number of flops and, separately, the number of square roots used as well. Use only basic programming....
Write a recursive function (using Matlab) moreFactors(a,b,fact) that does the following: 1. Takes as an input...
Write a recursive function (using Matlab) moreFactors(a,b,fact) that does the following: 1. Takes as an input 3 positive integers. 2. Of the two integers a and b, the function returns the integer that has the most factors fact. 3. If both integers a and b have the same amount of factors fact, the function will return the larger integer. Test your function with the following: >> result=moreFactors(24,32,3) result = 24 (24 = 3^1 · 2^3 , 32 = 2^5 )...
Matlab Create/write a function that takes an input of x and y data, and a string...
Matlab Create/write a function that takes an input of x and y data, and a string (either linear? or quadratic), sets up a linear system of equations (Ax = b), and solves and plots the model.
Using Matlab, write a function that takes numeric data as its input argument and prints a...
Using Matlab, write a function that takes numeric data as its input argument and prints a message to the Command Window stating if the number is positive, or negative, or 0. This function should transfer an output value to the Workspace ONLY when the input value is negative. Please include a copiable code and the associated screenshots.
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Write a python function which takes input matrix A and applies gaussian elimination with pivoting strategy...
Write a python function which takes input matrix A and applies gaussian elimination with pivoting strategy which returns P,Q (permutation matrices) L (lower triangular matrix with 1's on the diagonal) and U (upper triangular matrix) such that (P^t)(A)(Q)=LU is correct,
#Write a function called get_integer that takes as input one #variable, my_var. If my_var can be...
#Write a function called get_integer that takes as input one #variable, my_var. If my_var can be converted to an integer, #do so and return that integer. If my_var cannot be converted #to an integer, return a message that says, "Cannot convert!" # #For example, for "5" as the value of my_var, get_integer would #return the integer 5. If the value of my_var is the string #"Boggle.", then get_integer would return a string with the #value "Cannot convert!" # #Do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT