Create a function x=backsub(U,y) in matlab that accepts as input
arguments a nxn matrix U and n-vector y, and returns as output a column vector x
consisting of the values of the unknowns x1; x2; : : : ; xn.
function X = backsub(U,y)
%Input - U is an N x N nonsingular matrix
% - y is an N x 1 matrix
%Output - X is an N x 1 matrix containing the solution to UX=y.
%Initialize X and the temporary storage matrix C
[N N]=size(U);
X=zeros(N,1);
C=zeros(1,N+1);
%Form the augmented matrix: Aug=[U|y]
Aug=[U y];
for p=1:N-1
%Partial pivoting for column p
[Q,j]=max(abs(Aug(p:N,p)));
%Interchange row p and j
C=Aug(p,:);
Aug(p,:)=Aug(j+p-1,:);
Aug(j+p-1,:)=C;
if Aug(p,p)==0
'U is singular. No unique solution'
break
end
%Elimination process for column p
for k=p+1:N
m=Aug(k,p)/Aug(p,p);
Aug(k,p:N+1)=Aug(k,p:N+1)-m*Aug(p,p:N+1);
end
end
X=backsub(Aug(1:N,1:N),Aug(1:N,N+1));
Get Answers For Free
Most questions answered within 1 hours.