To do this problem, you should first go through the code for the GaussianElimina
ID: 3685376 • Letter: T
Question
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 b 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.)
This is GaussianElimination function:
function x=GaussianElimination(A,b)
[m,n]=size(A);
if m~=n
error('A matrix needs to be square');
end
Ab=[A b];
for i = 1:n-1
pivot = Ab(i,i);
for k = i+1:n
ratio=Ab(k,i)/pivot;
Ab(k,i:n+1) = Ab(k,i:n+1) - Ab(i,i:n+1)*ratio;
end
end
x = zeros(n,1); % preallocate memory for and initialize x
x(n) = Ab(n,n+1)/Ab(n,n);
for i=n-1:-1:1
x(i) = (Ab(i,n+1) - Ab(i,i+1:n)*x(i+1:n))/Ab(i,i);
end
Explanation / Answer
function x = GaussianElimination(A,b);
n = length(b);
x = zeros(n,1);
for k=1:n-1
% forward elimination
for i=k+1:n
x_mul = A(i,k)/A(k,k);
for j=k+1:n
A(i,j) = A(i,j)-x_mul*A(k,j);
end
b(i) = b(i)-x_mul*b(k);
end
end
% back substitution
x(n) = b(n)/A(n,n);
for i=n-1:-1:1
sum = b(i);
for j=i+1:n
sum = sum-A(i,j)*x(j);
end
x(i) = sum/A(i,i);
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.