HW2_solutions

HW2_solutions - MAE 107 – Computational Methods Summer...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: MAE 107 – Computational Methods Summer Session II 2009 Homework # 2 – Solutions 4 problems – 20 points Problem 1 – 8 points LU decomposition 1. The following MATLAB code performs the required tasks: function [L,U]=ludec(A) % Computes the LU decomposition of A using Gauss elimination % without pivoting % Input: % A: square matrix % Output: % L: lower triangular matrix with 1 on the diagonal % U: upper triangular matrix [m,n]=size(A); if(n~=m) error(’The matrix must be square’) end % Forward Elimination with partial pivoting for j=1:n, %Loop on the columns of A % Test if the pivot coefficient vanishes if(abs(A(j,j))<1e-6) error(’One of the pivot coefficient is zero - Pivoting is necessary’) end for k=j+1:n, % Loop on the rows, elements below the diagonal A(k,j)=A(k,j)/A(j,j); % A(k,j) now contains the factor %of elimination A(k,j+1:n)=A(k,j+1:n)-A(k,j)*A(j,j+1:n); % Row operation end 1 end % At the end of this loop, the elements below the diagonal in A are % the elimination factors (elements of L that are located below the diagonal) % U consists of the elements of A located on or above the main diagonal U=triu(A); % Extracts U from A L=tril(A); % Extracts L from A for j=1:n, L(j,j)=1; % Sets the diagonal elements of L to 1 end Note that the previous function stores both L and U in the array A until the very end: after elimination the subdiagonal elements of A are zeros and their value does not need to be stored. Instead we use the storage space to store the corresponding elements of L (elimination factor λ ij ). 2. We use the previous function at the command line to perform the requested decompositions: >> A1=[3 9 -1; -3 -11 8; 6 24 -24];[L1,U1]=ludec(A1) % Decompose A1 L1 = 1-1 1 2-3 1 U1 = 3 9-1-2 7-1 >> A1_test=L1*U1,A1 %% Test that the product L1*U1 is indeed equal to A1 A1_test = 3 9-1-3-11 8 6 24-24 A1 = 3 9-1-3-11 8 2 6 24-24 >> A2=[4 2 0; 0 1 2; 20 10 4];[L2,U2]=ludec(A2) % Decompose A2 L2 = 1 1 5 1 U2 = 4 2 1 2 4 >> A2_test=L2*U2,A2 % Test that the product L2*U2 is indeed equal to A2 A2_test = 4 2 1 2 20 10 4 A2 = 4 2 1 2 20 10 4 >> 3. In this question, you needed to write a function that solves Ax = b using the LU decomposition of A . You were asked to use the previous function ludec.m to obtain this decomposition. You did not need to perform the full Gauss elimination here. The following function performs the required tasks. function x=linsys_LU(A,b) % This function solves the square system Ax=b using LU % decomposition. The system Ax=b is decomposed into two triangular systems Ly=b and Ux=y % The LU decomposition is obtained from the function ludec.m % Inputs: A: square matrix 3 % b: column vector (right hand side_ % Outputs: x: column vector of the solution % Test if A and b have consistent sizes [m,n]=size(A); if(m~=n), error(’A is not square’); end if(length(b)~=n), error(’b does not have the right length’), end % Find the LU decomposition of A [L,U]=ludec(A); % Solve Ly =b using Forward Substitution y=zeros(n,1);...
View Full Document

This note was uploaded on 10/11/2011 for the course MAE 107 taught by Professor Rottman during the Summer '08 term at UCSD.

Page1 / 18

HW2_solutions - MAE 107 – Computational Methods Summer...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online