Question: The M-file given below is to implement Gauss elimination with partial pivoting. function x = GaussPivot(A,b) % GaussPivot: Gauss elimination pivoting % x = GaussPivot(A,b):
The M-file given below is to implement Gauss elimination with partial pivoting.
function x = GaussPivot(A,b)
% GaussPivot: Gauss elimination pivoting
% x = GaussPivot(A,b): Gauss elimination with pivoting.
% input:
% A = coefficient matrix
% b = right hand side vector
% output:
% x = solution vector
[m,n]=size(A);
if m~=n, error('Matrix A must be square'); end
nb=n+1;
Aug=[A b];
% forward elimination
for k = 1:n-1
% partial pivoting
[big,i]=max(abs(Aug(k:n,k)));
ipr=i+k-1;
if ipr~=k
Aug([k,ipr],:)=Aug([ipr,k],:);
end
for i = k+1:n
factor=Aug(i,k)/Aug(k,k);
Aug(i,k:nb)=Aug(i,k:nb)-factor*Aug(k,k:nb);
end
end
% back substitution
x=zeros(n,1);
x(n)=Aug(n,nb)/Aug(n,n);
for i = n-1:-1:1
x(i)=(Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i);
end
Develop an M-file function based on the given M-file to implement Gauss elimination with partial pivoting. Modify the function so that it computes and returns the determinant (with the correct sign), and detects whether the system is singular based on a near-zero determinant. For the latter, define near-zero as being when the absolute value of the determinant is below a tolerance. When this occurs, design the function so that an error message is displayed and the function terminates. Here is the function's first line: function [x, D] = GaussPivotNew(A, b, tol) where D = the determinant and tol = the tolerance.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
