Question

In: Computer Science

Make the following matlab function. D=inverses(A) It takes the input of an nxn matrixA. First, the...

Make the following matlab function.

D=inverses(A)

It takes the input of an nxn matrixA.

First, the function has to determine whether A is invertible (you may want to use the function rank to do that). If A is not invertible, the function has to return an empty matrix D=[ ]; and terminates with a message “Matrix A is not invertible”.

If A is invertible, then the function reduces the matrix [A eye(n)] into the reduced echelon form and returns the matrix D that is the inverse of the matrix A. You can use a MATLAB built-in function rref for this part.

Solutions

Expert Solution

%matlab code

function D = inverse(A)
[n n] = size(A);
% Singular matrix found
if det(A) == 0
disp('Matrix A is not invertible')
D = [];
quit
end
D = eye(n);
  
%matrix inversion
for j = 1 : n
for i = j : n
if A(i,j) != 0
for t = 1 : n
suD = A(j,t);
A(j,t) = A(i,t);
A(i,t) = suD;
suD = D(j,t);
D(j,t) = D(i,t);
D(i,t) = suD;
end
temp = 1/A(j,j);
for t = 1 : n
A(j,t) = temp * A(j,t);
D(j,t) = temp * D(j,t);
end
for idx = 1 : n
if idx != j
temp = -1*A(idx,j);
for k = 1 : n
A(idx,t) = A(idx,t) + temp * A(j,t);
D(idx,t) = D(idx,t) + temp * D(j,t);
end
end
end
end
break
end
end
end

n = input("Enter size of matrix n: ");
A = zeros(n,n);
for i=1:n
for j=1:n
A(i,j) = input("ENter matrix value: ");
end
end
D = inverse(A)
%{
output:
Enter size of matrix n: 3
ENter matrix value: 1
ENter matrix value: 2
ENter matrix value: 3
ENter matrix value: 4
ENter matrix value: 5
ENter matrix value: 6
ENter matrix value: 7
ENter matrix value: 8
ENter matrix value: 9
D =
1.00000 0.00000 -1.30000
0.00000 0.20000 0.20000
0.00000 0.00000 0.01111
%}


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 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....
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.
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 )...
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.
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...
Write a function that takes a person’s first and last names as input and (a) uses...
Write a function that takes a person’s first and last names as input and (a) uses lists to return a list of the common letters in the first and last names (the intersection). (b) uses sets to return a set that is the intersection of the characters in the first and last names. (c) uses sets to return the set that is the symmetric difference between the first and last names. please write in python program
Matlab Code that does the following: Takes as input 3 frequencies. Create a signal that is...
Matlab Code that does the following: Takes as input 3 frequencies. Create a signal that is a mixture of the 3 signals. Create a bandpass filter that only selects the center frequency. Output the filtered signal which contains only the middle frequency. Plot in time and frequency domain the original signal and the filtered signal. Show the output for filter order 1 and 15. Upload a pdf of the image files. Each figure should have your name in the title...
takes a single string d as input which represents a date
takes a single string d as input which represents a date
Please make the following changes to the following code: #This program takes in the first and...
Please make the following changes to the following code: #This program takes in the first and last name and creates a userID printing first letter of first name and first 7 characters of last name #by Abi Santo #9/20/20 def main(): print("This Program takes your first and last name and makes a User ID") f_name = str(input("Please enter your first name ")).lower() l_name = str(input("Enter your last name: ")).lower() userID = str(f_name[:1]+str(l_name[:7])) print(userID) main() Call the function createUserName(). This function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT