Question

In: Computer Science

Write a MATLAB script which gets a matrix as input and check the dimensions of the...

Write a MATLAB script which gets a matrix as input and check the dimensions of the input:

Part A) If the input matrix is a square matrix, calculate the inverse of it and show it in the command window, otherwise, show the text “the input matrix is not invertible”.

Part B) Reshape the input matrix to a row vector and assign it to Y and make a vector named X with the same length as Y then make a 2D Plot and show it.

Solutions

Expert Solution

% Solution to part A

% Take an input matrix from the user

mat = input("Please enter an input matrix");

% Get the dimensions of input matrix

[rows,cols] = size(mat);

% check if the input matrix is a square matrix

% for square matrix no_of_rows = no_of_columns

if(rows ~= cols) % if no_of_rows is not equal to no_of_columns, so not a square matrix

disp("the input matrix is not invertible"); % display message that matrix is not invertible

else

% calculate inverse of matrix

matInv = inv(mat);

disp("The inverse of entered matrix is: ");

disp(matInv); % Display the inverse of matrix in command window

end

% Part B begins from here

% Reshape the matrix into a row vector

% A row vector has only one row and may have any number of columns

% Eg [1 2 3 4 5 7 2 3 4], [1 67 89 34] etc

Y = reshape(mat,1,[]); % using 1 as second argument tells that no of rows in

% resultant should be 1 and [] as third argument tells

% system to calculate number of columns itself. You can find more information by

% typing 'doc reshape' into the command window.

X = 1:length(Y); % Create a row vector X with values 1 to length of Y.

% Eg if Y=[10 20 30 32 45] then X = [1 2 3 4 5]

% Create a 2D plot

plot(X,Y,"r*"); % creates a 2D plot with red coloured stars and no lines

% Use the below command if you wish to display lines as well

%plot(X,Y,"r*-");

% You may optionally add labels to X axis and Y axis with below commands

% xlabel("Enteries in X vector");

% ylabel("Enteries in Y vector");

------------------------------------------------------------------------------------

OUTPUTS

------------------------------------------------------------------------------------

Plot for the same

Another example with a non square matrix as input

Plot for above


Related Solutions

(MATLAB) Write a script that would create a 3x5 matrix of random integers and write this...
(MATLAB) Write a script that would create a 3x5 matrix of random integers and write this new matrix to a file called “Assignment3_Question5.dat”.
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 script that computes the element level stiffness matrix and force vector for linear...
Write a Matlab script that computes the element level stiffness matrix and force vector for linear elasticity.
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...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program,...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program, quadroots.m, for finding the roots of the second- order polynomial ax2 + bx + c. Use the quadratic equation. The inputs are the coefficients a,b, and c and the outputs are z1 and z2. The program should produce (exactly) the following output in the Command window when run with (a, b, c) = (1, 2, −3):
MATLAB Write a script which asks the user of the program to provide an initial horizontal...
MATLAB Write a script which asks the user of the program to provide an initial horizontal position, initial vertical position, initial velocity, and angle. Create a time vector spanning from zero seconds to 100 seconds incremented at 0.01 seconds. Call the function that you created in the previous problem to calculate the trajectory and velocities of the projectile. Find the maximum height of the projectile and the time at which it reaches that point. Write a neat sentence stating what...
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, if-else Write a script which to do the following. Take an array of any size...
MATLAB, if-else Write a script which to do the following. Take an array of any size as input and check if all the elements are integers. (i) If not, display the error message ‘Invalid input!’. Use the error command to create a proper error message in red. (ii) If yes, count the number of rows where the sum of the elements is smaller than the average row sum. It is recommended that you use a test variable with many rows...
Write a program that first gets a list of integers from input. The input begins with...
Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain fewer than 20 integers. That list is followed by two more integers representing lower and upper bounds of a range. Your program should output all integers from the list that are within that range (inclusive of the bounds). For coding simplicity, follow each output integer by a space,...
write a matlab script for double mass spring system with animation.
write a matlab script for double mass spring system with animation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT