Question: function x = ainvb (A,b) op dp op| f function x = ainvb (A,b) solve Ax = b [p, LU] plu (A); y = forsub

![= ainvb (A,b) solve Ax = b [p, LU] plu (A); y](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f542fbc77bf_49966f542fb44468.jpg)
![= forsub (LU,b,p); x = backsub (LU,y); function (p, A] plu (A)](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f542fc93709_50066f542fc0f052.jpg)
function x = ainvb (A,b) op dp op| f function x = ainvb (A,b) solve Ax = b [p, LU] plu (A); y = forsub (LU,b,p); x = backsub (LU,y); function (p, A] plu (A) function (p,A] plu (A) Perform LU decomposition with partial pivoting. % Upon return the coefficients of L and U replace those f of the input n by n nonsingular matrix A. The row interchanges performed are recorded in the 1D array p. n = size (A, 1); initialize permutation vector p P = 1:n; LU decomposition with partial pivoting for k = 1:n-1 find row index of relative maximum in column k [val,q) = max(abs (A(k:n, k))); q = q + k-1; interchange rows k and q and record this in p A([k,q), :) =A( [q, k), :); p([k, q] ) =p ( [q,k]); compute the corresponding column of L J=k+1:n; A(J,k) A(J,k) / A(k,k); update submatrix by outer product A(J,J) = A(J,J) - A(J,k) + A(K, J); end function y = forsub (A,b,p) function y = forsub (A,b,p) Given a unit lower triangular, nonsingular n by n matrix A, an n-vector b, and a permutation p return vector y which solves Ay = Pb n = length(b); permute b according to p b=b(); forward substitution y(1) = 5(1); for k = 2:n y (k) = b (k) A(k, 1:k-1) + y(1:k-1); end function x = backsub (A,b) function x = backsub (A,b) Given an upper triangular, nonsingular n by n matrix A and an n-vector b, return vector x which solves Ax = b n - length(b); x - b; x(n) - b(n) / A(n,n); for k = n-1:-1:1 x(k) = (bk) A (k,k+1:n) -x(k+1:n) ) / A (k,k); end 10. The MATLAB code given in Section 5.4 for solving linear systems of equations, using LU decomposition in outer form with partial pivoting, works well if the matrix A is nonsingular to a working precision. But if A is singular, then the exit is not graceful. Please fix this by modifying the functions ainvb and plu to include checks so that ainvb+p] will always return as follows: (i) In case of a nonsingular system, return silently with the solution x. (ii) In case of a singular system, display a message regarding the singularity and return with x assigned the value NaN. Assume that MATLAB will complain if a division by a number smaller than eps = 2.22 x 10-16 is attempted. (You want to avoid precisely this sort of complaint.) function x = ainvb (A,b) op dp op| f function x = ainvb (A,b) solve Ax = b [p, LU] plu (A); y = forsub (LU,b,p); x = backsub (LU,y); function (p, A] plu (A) function (p,A] plu (A) Perform LU decomposition with partial pivoting. % Upon return the coefficients of L and U replace those f of the input n by n nonsingular matrix A. The row interchanges performed are recorded in the 1D array p. n = size (A, 1); initialize permutation vector p P = 1:n; LU decomposition with partial pivoting for k = 1:n-1 find row index of relative maximum in column k [val,q) = max(abs (A(k:n, k))); q = q + k-1; interchange rows k and q and record this in p A([k,q), :) =A( [q, k), :); p([k, q] ) =p ( [q,k]); compute the corresponding column of L J=k+1:n; A(J,k) A(J,k) / A(k,k); update submatrix by outer product A(J,J) = A(J,J) - A(J,k) + A(K, J); end function y = forsub (A,b,p) function y = forsub (A,b,p) Given a unit lower triangular, nonsingular n by n matrix A, an n-vector b, and a permutation p return vector y which solves Ay = Pb n = length(b); permute b according to p b=b(); forward substitution y(1) = 5(1); for k = 2:n y (k) = b (k) A(k, 1:k-1) + y(1:k-1); end function x = backsub (A,b) function x = backsub (A,b) Given an upper triangular, nonsingular n by n matrix A and an n-vector b, return vector x which solves Ax = b n - length(b); x - b; x(n) - b(n) / A(n,n); for k = n-1:-1:1 x(k) = (bk) A (k,k+1:n) -x(k+1:n) ) / A (k,k); end 10. The MATLAB code given in Section 5.4 for solving linear systems of equations, using LU decomposition in outer form with partial pivoting, works well if the matrix A is nonsingular to a working precision. But if A is singular, then the exit is not graceful. Please fix this by modifying the functions ainvb and plu to include checks so that ainvb+p] will always return as follows: (i) In case of a nonsingular system, return silently with the solution x. (ii) In case of a singular system, display a message regarding the singularity and return with x assigned the value NaN. Assume that MATLAB will complain if a division by a number smaller than eps = 2.22 x 10-16 is attempted. (You want to avoid precisely this sort of complaint.)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
