solution_hw7 - x = back_sub(U,d); % using the back...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
Solution to homework 7 2. d) A=[1 2 0; 1 0 -1; 3 1 -1] b=[0 2 6]'; [L,U,P] = lu(A) % one way of LU decompostion is to use matlab build-in function Ainv = inv(A) % matrix inversion using matlab build-in function condA = cond(A, 'fro' ) % calculate the condition number using Frobenius norm Running the above script, matlab gives: A = 1 2 0 1 0 -1 3 1 -1 L = 1.0000 0 0 0.3333 1.0000 0 0.3333 -0.2000 1.0000 U = 3.0000 1.0000 -1.0000 0 1.6667 0.3333 0 0 -0.6000 P = 0 0 1 1 0 0 0 1 0 Ainv = -0.3333 -0.6667 0.6667 0.6667 0.3333 -0.3333 -0.3333 -1.6667 0.6667 condA = 9.4868
Background image of page 5

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

View Full DocumentRight Arrow Icon
3. the inverse function file (inverse.m) is: function [Ainv]=inverse(A) % input matrix A has to be square matrix % output matrix Ainv is the inverse matrix of input matrix [n m]=size(A); % get the size of input matrix if (n == m) % input A must be square matrix. Otherwise, A has no inversion [L U] = lu_Balhoff(A); % LU decomposition of input matrix A for i=1:n b = zeros(1,n); b(i)=1; % initialize the b vector d = forward_sub(L,b); % using forward substitute to calculate the d vector
Background image of page 6
Background image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: x = back_sub(U,d); % using the back substitute to calculate the xi vector Ainv(:,i)=x'; % put the xi into the ith column of Ainv end else fprintf( 'oops, wrong. ..' ) % if input A matrix is not square, report error message end the back substitute function file (back_sub.m) is: function [x] = back_sub(L,d) %input: L has to be a lower triangular matrix % d is the input vector where Lx = d %output: x is the output vector where Lx = d [n m]=size(L); % get the size of input matrix x(n)=d(n)/L(n,n); % calculate the nth element of x first, outside the loop for i=n-1:-1:1 % back substitute starts from bottom sum=0; for j=n:-1:i+1 % the 'summation' item starts from n to i+1 sum=sum+L(i,j)*x(j); end x(i)=(d(i)-sum)/L(i,i); end You can find the forward_sub.m on blackboard. Professor Balhoff posted it. Running the function in matlab, gives: >> inverse(A) ans = -0.3333 -0.6667 0.6667 0.6667 0.3333 -0.3333 -0.3333 -1.6667 0.6667...
View Full Document

Page1 / 7

solution_hw7 - x = back_sub(U,d); % using the back...

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

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