4. (30 points) The inverse of a matrix A can be defined as the matrix X whose columns x satisfy Ax-e, where e, is thej-th column of the identity matrix. Write a function (X, condX, pvt]= invert(n, A) which accepts a matrix A of order n as input and which returns a matrix X, an approximation of the inverse of A, as well as the condition number estimate and the pivot information. Your function should call decomp.m just once and call solve.m a total of n times, once for each column of X. Leave X undefined if decomp.m detects singularity Test your program on some matrices whose elements can be exactly represented as floating-point numbers and for which you know A. There are several measures of the accuracy of the results: AX-Ill; IXA-II; IX-A. You might also use invert.m twice, once to invert A and a second time to invert X The result is a matrix Z, which should be equal to A if there were no round-off error So, another meas Write a script testinvert.m invoking invert.m to test it on five different matrices of sizes 2, 3, 4, 5, and one random of size 4, giving as an output two of the four measures of accuracy suggested above for each tested matrix. ure of accuracy Wwould be IZ-AlI Submit: - a hardcopy of your Matlab codes, also the input test matrices and the corresponding couple of accuracy measures for each test matrix

decomp

function [A,condA,pvt]= decomp(A)pvt = [0;0;0;0];s = [0;0;0;0];n= size(A);condA=cond(A);for i = 1:n(1) pvt(i)=i; smax=0; for j= 1:n(1) smax=max(smax,abs(A(i,j))); end s(i)=smax ;endfor k= 1:(n(1)-1) rmax= 0; for i= k:n(1) r =abs((A(pvt(i),k))/s(pvt(i))); if (r>rmax) rmax=r; j=i; end end h=pvt(j); pvt(j)=pvt(k); pvt(k)=h; for i=k+1:n(1) xmult= (A(pvt(i),k))/(A(pvt(k),k)); A(pvt(i),k)=xmult; for j= k+1:n(1) A(pvt(i),j)=A(pvt(i),j)-(xmult*(A(pvt(k),j))); end endend condA pvtend Solvefunction x= solve(A,b,pvt)n= size(A,1);x=zeros(1,n);for k=1:(n-1) for i= k+1: n b(pvt(i))= b(pvt(i))-A(pvt(i),k)*b(pvt(k)); endendx(n)=b(pvt(n))/A(pvt(n),n);for i = (n-1):-1:1 sum= b(pvt(i)); for j=i+1:n sum = sum-A(pvt(i),j)*(x(j)); end x(i)= sum/ A(pvt(i),i);endend4. (30 points) The inverse of a matrix A can be defined as the matrix X whose columns x satisfy Ax-e, where e, is thej-th column of the identity matrix. Write a function (X, condX, pvt]= invert(n, A) which accepts a matrix A of order n as input and which returns a matrix X, an approximation of the inverse of A, as well as the condition number estimate and the pivot information. Your function should call decomp.m just once and call solve.m a total of n times, once for each column of X. Leave X undefined if decomp.m detects singularity Test your program on some matrices whose elements can be exactly represented as floating-point numbers and for which you know A. There are several measures of the accuracy of the results: AX-Ill; IXA-II; IX-A. You might also use invert.m twice, once to invert A and a second time to invert X The result is a matrix Z, which should be equal to A if there were no round-off error So, another meas Write a script testinvert.m invoking invert.m to test it on five different matrices of sizes 2, 3, 4, 5, and one random of size 4, giving as an output two of the four measures of accuracy suggested above for each tested matrix. ure of accuracy Wwould be IZ-AlI Submit: – a hardcopy of your Matlab codes, also the input test matrices and the corresponding couple of accuracy measures for each test matrix Show transcribed image text