Question

In: Computer Science

Write a program which reads the matrix A (2x2 matrix taking the first two column vectors...

Write a program which reads the matrix A (2x2 matrix taking the first two column vectors from the input file) and the vector b (the third column vector) and solve for x in Ax=b for general A.

If there is a unique solution, your output should be a 2x2 matrix with two lines and two numbers per line. The output should contain numbers with up to four significant digits. If the system is unsolvable or inconsistent, then your output should be a single line printing "System inconsistent". If the system is underdetermined, your output should be a single line printing "System underdetermined".

Input format (2x3 matrix):

n11 n12 n13

n21 n22 n23

In python please

Solutions

Expert Solution

ANSWER:

  • I have provided the properly commented  and indented code so you can easily copy the code as well as check for correct indentation.
  • I have provided the output image of the code so you can easily cross-check for the correct output of the code.
  • Have a nice and healthy day!!

CODE

# import important module
import numpy as np

# reading input file "matrix.txt"
inputFile = open("matrix.txt")

# defining empty matrix A and b
A = []
b = []

# looping through each line and converting input to matrix
for line in inputFile:
    # spliting line and converting each value to float
    row = [int(c) for c in line.split()]
    # first two values to A
    A.append(row[:2])
    # and last to b
    b.append(row[2])
    
# checking if size of A and b no equal
m,n = len(A),len(A[0])
sizeB = len(b)

if (m!=n) or (n!=sizeB):
    print("System underdetermined")
else:
    # otherwise, solving equation using np.linalg.solve
    # catching error if singular matrix
    try:
        # using np.linalg.solve to solve equaion
        X2 = np.linalg.solve(A,b)
        # displaying result, diagonal matrix of X2 for 2x2 matrix display
        print("{:.4f} {:.4f}\n{:.4f} {:.4f}".format(*np.diag(X2).flatten()))
    except np.linalg.LinAlgError as err:
        if 'Singular matrix' in str(err):
            print('System inconsistent')
        else:
            # otherwise
            raise

INPUT IMAGE (matrix.txt)

OUTPUT IMAGE


Related Solutions

Given the matrix A (2x2 matrix taking the first two column vectors from the input file),...
Given the matrix A (2x2 matrix taking the first two column vectors from the input file), compute the followings. Λ: the diagonal matrix whose diagonal elements are the corresponding eignevalues Λii = λi for i=1,2 R: the 2x2 matrix whose ith column vector is the eigenvector corresponding to λi for i=1,2 RΛRT: the matrix compositions 1/0: the comparison between A and RΛRT (is A= RΛRT?) Your output should have seven lines where the first two lines correspond to the 2x2...
Please do not use vectors or any previously posted code Write a C++ program which reads...
Please do not use vectors or any previously posted code Write a C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read line by line until an end-of-transmission (^d) is encountered. You should read the list and represent it in a linked list data...
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
Given a 2x2 matrix, A = 1 4 2 -1 Find its eigen values, eigen vectors....
Given a 2x2 matrix, A = 1 4 2 -1 Find its eigen values, eigen vectors. Can matrix be diagnolized?
Write a program that prompts for and reads in the two side lengths of a right...
Write a program that prompts for and reads in the two side lengths of a right triangle (floating point numbers) and then calls a float-valued function, hypot1, that calculates and returns the length of the hypotenuse of the triangle. The program then displays the two side lengths and the hypotenuse length. Note: The hypot1 function returns the hypotenuse length – it does not display it; the function should not contain any cout nor cin statements. Math review: Recall that for...
**IN AT&T ASSEMBLY LANG** Write an assembly language program which either hardcodes or reads in two...
**IN AT&T ASSEMBLY LANG** Write an assembly language program which either hardcodes or reads in two integers, A and B, and uses them to compute the following expressions. You must use the same values for A and B throughout all three expressions. A * 5 (A + B) - (A / B) (A - B) + (A * B)
You are to write a C++ program which does the following: Reads in the size of...
You are to write a C++ program which does the following: Reads in the size of a list of characters. Reads in the list of characters. Prints the list of characters in the opposite order read in. Prints the list of characters in the order read in. Sorts the list. Prints the sorted list. You may assume there will be no more than 1000 characters in the list. (You should use a constant to make this limit easily changeable.) You...
For the following matrices, first find a basis for the column space of the matrix. Then...
For the following matrices, first find a basis for the column space of the matrix. Then use the Gram-Schmidt process to find an orthogonal basis for the column space. Finally, scale the vectors of the orthogonal basis to find an orthonormal basis for the column space. (a) [1 1 1, 1 0 2, 3 1 0, 0 0 4 ] b) [?1 6 6, 3 ?8 3, 1 ?2 6, 1 ?4 ?3 ]
write a C++ Program for matrix operations which include: 1. constructor in the form-- matrix::matrix(int numRows,...
write a C++ Program for matrix operations which include: 1. constructor in the form-- matrix::matrix(int numRows, int numColumns) 2. Implement a setter method of the form: -- void matrix::setElement(int row, int col) 3 Implement a getter method of the form: -- float matrix::getElement(int row, int col) 4. Make a threaded version of the matrix/matrix addition method. 5. Make a threaded version of the matrix/scalar multiplication method. 6. Matrix/matrix addition methods of the form: -- matrix matrix::matrixAdd(matrix inMatrix) // non-threaded version...
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT