ME 581 Assignment #2 In the following questions write the derivations and algori
ID: 667919 • Letter: M
Question
ME 581 Assignment #2 In the following questions write the derivations and algorithm steps that you used to write your program. . Write a Gauss elimination subroutine with partial pivoting. Use it to solve the following linear system of equations of the form [A]{x} = {c} for vector {x}. The input to your program should be matrix [A], vector {c} and number of columns/row of the square matrix [A]. The output of your program should be vector {x}. Use your program to calculate {x} given that: 2. Modify your Gauss elimination subroutine in question 1 (with no partial pivoting) tor a banded [A] matrix with a bandwidth equal to b. What are the main differences between the banded Gauss and the regular Gauss elimination algorithms? What is the computational cost of each algorithm? Use your program to calculate {x} given that: 3. Use the Gauss elimination routine that you developed in Question 1 in a subroutine to find the inverse of a matrix. The input to your subroutine should be matrix [A] and the number of rows/columns of [A]. The output to the subroutine the inverse of matrix [A]. Use your program to find the inverse of the following matrix:Explanation / Answer
%Filename: Matrix.m %Description: Implements a custom Matrix class with standard matrix operations (see README.md for more details) classdef Matrix properties(SetAccess = private) r = 2; c = 2; data = zeros(2); end %end properties (private) properties determinant = NaN; invertible = 0; dominant_eigen_value = NaN; end methods % CONSTRUCTOR ------------------------------------------------------------------------------------------ function M = Matrix(mat) % Description: default constructor % Parameters: mat - a standard MATLAB matrix % Returns: an instance of the custom Matrix class if(nargin == 0) M.r = 2; M.c = 2; M.data = zeros(2); M.determinant = NaN; M.invertible = 0; end temp_size = size(mat); M.r = temp_size(1); M.c = temp_size(2); M.data = mat; if(M.r == M.c) if(det(mat) == 0) M.invertible = 0; M.determinant = 0; else M.invertible = 1; end else M.invertible = 0; M.determinant = NaN; end M.dominant_eigen_value = 0; end % DISPLAY ------------------------------------------------------------------------------------------ function display(A) % Description: Displays the contents of the Matrix % Parameters: None % Returns: A standard MATLAB matrix A.data end % GET DIMENSIONS ------------------------------------------------------------------------------------------ function dim = dims(A) % Description: Prints the matrix dimensions % Parameters: None % Returns: A 1x2 matrix where the first element is the number of rows and the second element is the number of columns % display([num2str(M.r) ' rows by ' num2str(M.c) ' columns']); dim = [A.r A.c]; end % GET ELEMENTS ------------------------------------------------------------------------------------------ function elmt = get_elements(A, i, j) % Description: Gets an element(s) of a Matrix % Parameters: i - integer or array of integers indicating the rows (: for all) % j - integer or array of integers indicating the columns (: for all) % Returns: An element or list of elements from the Matrix A elmt = A.data(i, j); end % MATRIX TRANSPOSITION ------------------------------------------------------------------------------------------ function M = transpose(A) % equivalent to (A.') % Description: Transposes the Matrix % Parameters: None % Returns: The transposition Matrix of Matrix A temp_size = A.dims(); trans_mat = zeros(temp_size(2), temp_size(1)); for(itR = 1:temp_size(2)) trans_mat(itR,:) = A.get_elements(:,itR); end M = Matrix(trans_mat); end % MATRIX ABS ------------------------------------------------------------------------------------------ function M = abs(A) dimensions = A.dims(); m = dimensions(1); n = dimensions(2); M = zeros(m,n); for(row=1:m) for(col=1:n) M(row,col) = abs(A.get_elements(row,col)); end end M = Matrix(M); end % MATRIX < ------------------------------------------------------------------------------------------ function tf = lt(A, val) % Description: Tests for equality between A and B to a factor of 0.0001 % Parameters: A - a custom Matrix object % B - a matlab Matrix object % Returns: boolean stating whether or not A == B adims = A.dims(); tf = 1; for(row=1:adims(1)) for(col=1:adims(2)) if(A.get_elements(row,col) > val) tf = 0; break; end end end end % MATRIX EQUALITY (custom, MATLAB) ------------------------------------------------------------------------------------------ function tf = eq(A,b) % Description: Tests for equality between A and B to a factor of 0.0001 % Parameters: A - a custom Matrix object % B - a matlab Matrix object % Returns: boolean stating whether or not A == B ERR = 0.0001; adims = A.dims(); bdims = size(b); a = A.get_elements(:,:); if(adims == bdims) if(all(abs(a-b) A = invP * L * U % A * X = B now becomes (L * U) * X = P * B % 1) Since (L * U) * X = (P * B), => L * (U * X) = (P * B) => L * Y = (P * B) where Y = U * X (SOLVE FOR Y using forward substitution) % 2) Since Y = U * X (SOLVE FOR X using backward substitution) % 1) solve for Y, L * Y = P*B % PB = B; PB = P * B; PBdims = PB.dims(); Y = zeros(n,PBdims(2)); % foward substitution for(c = 1:PBdims(2)) % for each column in Y Y(1,c) = PB.get_elements(1,c) ./ L.get_elements(1,1); for(r = 2:n) % elements within the column sum = 0; for(k = 1:r-1) sum = sum + (L.get_elements(r,k) .* Y(k,c)); end Y(r,c) = (PB.get_elements(r,c) - sum) ./ L.get_elements(r,r); end end % 2) solve for X, Y = U * X X = zeros(n,PBdims(2)); % backward substitution for(c = 1:PBdims(2)) % for each column in X X(n,c) = Y(n,c) ./ U.get_elements(n,n); for(r = n-1:-1:1) % elements within the column sum = 0; for(k = r+1:n) sum = sum + (U.get_elements(r,k) .* X(k,c)); end X(r,c) = (Y(r,c) - sum) ./ U.get_elements(r,r); end end X = Matrix(X); else error('Starting Matrix must be square.') end end % SOLVE LINEAR SYSTEM using Gauss-Seidel Method ------------------------------------------------------------------------------------------ % code adapted from http://college.cengage.com/mathematics/larson/elementary_linear/5e/students/ch08-10/chap_10_2.pdf, example 2 function X = gs_linsolve(A, B, its) % Description: Solve the linear system Ax = b for any matrix A using Gauss-Seidel Iterative Method % Parameters: A - an m x n custom Matrix object % B - an m x k custom Matrix object % Returns: The linear solution as an n x k custom Matrix object Adims = A.dims(); ma = Adims(1); na = Adims(2); Bdims = B.dims(); mb = Bdims(1); nb = Bdims(2); if(ma == na & ma == mb) x = zeros(na, nb); resX = zeros(na,nb); for(i = 1:its) % while(not(all(resX))) for(col = 1:nb) % each col for(row = 1:na) % each element per col % row % col row_elmts = 1:na; row_elmts = row_elmts(row_elmts~=row); row_elmts = [A.get_elements(row,row_elmts).*transpose(x(row_elmts,col)), B.get_elements(row,col)*-1]; temp = sum(row_elmts ./ (A.get_elements(row,row).*-1) ); if(abs(x(row,col) - temp) < 0.01) resX(row,col) = 1; end x(row,col) = temp; end end end X = Matrix(x); else error('For gs_linsolve(A,B), A must be n x n, B must be n x k') end end % MATRIX INVERSION ------------------------------------------------------------------------------------------ function M = inv(A) % Description: Calculates the inverse matrix of Matrix A % Parameters: A - a custom Matrix object % Returns: The inverse matrix or a matrix of inf values if not-invertible dimensions = A.dims(); m = dimensions(1); n = dimensions(2); if(m == n) % check for square matrix if(A.invertible) % calculate inverse M = ge_linsolve(A, Matrix(eye(m))); else display('The matrix is singular because it has a determinant of 0 (not invertible).') M = zeros(m) + inf; end else error('Starting Matrix must be square.') end end end % end methods end % end classdefRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.