Task 2: Create a Function for Backward Elimination (5 pts) Do not use any of MAT
ID: 3817618 • Letter: T
Question
Task 2: Create a Function for Backward Elimination (5 pts) Do not use any of MATLAB's built in functions or commands. This MUSTbe implemented using matrix and vector components via FOR loops in your construction Use backward elimination to solve Ux -y for x via the algorithm MEEN 357 Engineering Analysis for Mechanical Engineers Spring 2017 for i 1:1 where U is an upper-triangular matrix of dimension ngn, while x and y are column vectors of dimension n. Use the MATLAB command x zeros(n.1) to initialize vector x This function must have the interface x backward elim (U, and it needs to verify that Uis a square n n matrix with numeric values, while yis a column vector of like dimension n containing nu values, where n is arbitrary, if not your code should terminate with an error. It should also terminate with an error if any diagonal element of matrix U is zero valued. If there are elements in the strictly lower-triangular part of matrix U, they are to be ignored.Explanation / Answer
function x = backward_elim(U,y)
%Get dimensions of the matrix
[rows,cols] = size(U);
%Return if the matrix is not square matrix
if(rows ~= cols)
return;
end
% n is the dimension of the square matrix
n = rows;
% Return if dimension U and y doesn't match
if(n ~= size(y))
return;
end
% Return if any of the diagonal entry is zero
if any(diag(U) == 0)
return;
end
%Initialize x vector
x = zeros(n,1);
for i=n:-1:1
sum = 0;
for j=i+1:n
sum = sum + U(i,j)*x(j);
end
x(i) = (y(i)-sum)/U(i,i);
end
end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.