To do this problem you should first go through the code for

To do this problem, you should first go through the code for the GaussianElimination function, and understand it. Then write a function that solves the system of equations for the case where the equation matrix is upper-triangular. The function header is given as: function x = SolveUpper (U,b) where the input U is an upper-triangular matrix, and 6 is a column vector; the output x is the solution to Ux = b. (Hint: if the system begins with a matrix U which is already in an upper-triangular form, we do not need to do elimination at all; hence only the back-substitute part from the GaussianElimination function is needed.)

Solution

MATLAB command that directly computes backward substitution.

Programs:
CODE 1:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function x=backsub(A,y);
% backsub: solves upper triangular system A*x = y using backwards
substitution
% usage: x=backsub(A,y);
%
% arguments:
% A (nxn) - upper triangular matrix
% y (nx1) - vector
%
% x (nx1) - solution to A*x = y

CODE 2: If you don\'t want to use direct command


% check to see that A is upper triangular
if any(any(tril(A,-1)))
   error(\'A must be upper triangular\')
end

% check dimensions of A and y
[n,m]=size(A);
if n~=m
   error(\'A must be square\');
end
if any([n 1]~=size(y))
   error(\'y must be a vector with the same # of rows as A\');
end

% perform backwards substitution
x=zeros(n,1);
x(n)=y(n)/A(n,n);
for i=n-1:-1:1
   x(i)=(y(i)-A(i,i+1:n)*x(i+1:n))/A(i,i);
end

 To do this problem, you should first go through the code for the GaussianElimination function, and understand it. Then write a function that solves the system

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site