Gaussian Elimination in Matlab In this problem we test the performance of the th
ID: 3851133 • Letter: G
Question
Gaussian Elimination in Matlab
In this problem we test the performance of the the naive Gaussian elimination procedure
and compare it with the linear system solver implemented in Matlab (which uses scaled
partial pivoting). As a test problem, we solve the system:
where A is the Vandermonde-matrix:
where c = [c0,c1,..., cn] is a given vector. Since the main point of this problem is
to compare the two algorithms, we choose a vector b which gives a known solution
x. For example, if the solution is x = ones(n)=[1, 1, ... ,1], then the load vector is
b = A ones(n).
Solve the above system with two different algorithms:
The naive Gaussian elimination without pivoting, using the function naiv_gauss(A,b).
Gaussian elimination with pivoting in Matlab, i.e., using the command x = A;
You should test it for the following 3 cases:
i) c = [0.2,0.4,0.6,0.8,1]T
ii) c = [0.1, 0.2, 0.3, ..., 0.9. 1]t
iii) c = [0.05, 0.10, 0.15, ..., 0.90, 0.95, 1]T
Below is the MATLAB function naiv_gauss:
function x = naiv_gauss(A,b);
n = length(b);
x = zeros(n,1);
for k=1:n-1
for i=k+1:n
xmult = A(i,k)/A(k,k);
A(i,k) = xmult;
for j=k+1:n
A(i,j) = A(i,j)-xmult*A(k,j);
end
b(i) = b(i)-xmult*b(k);
end
end
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
Explanation / Answer
function [P,Q]=naiv_gauss(A,b)
% forward elimination starts here
_nLen=length(b);
_m=zeros(_nLen,1);
P=zeros(_nLen,1);
for k =1:_nLen-1;
%computing m-> kth column
_m(k+1:_nLen) = A(k+1:_nLen,k)/A(k,k);
for i=k+1:_nLen
A(i, k+1:_nLen) = A(i,k+1:_nLen)-_m(i)*A(k,k+1:_nLen);
end;
b(k+1:_nLen)=b(k+1:_nLen)-b(k)*_m(k+1:_nLen);
end
Q= triu(A);
% now backward elimination starts
P(_nLen)=b(_nLen)/A(_nLen,_nLen);
for k =_nLen-1:-1:1;
b(1:k)=b(1:k)-P(k+1)* Q(1:k,k+1);
P(k)=b(k)/Q(k,k);
end
end
A=rand(6,4);
b=rand(4,1);
[P,Q] = naiv_gauss(A,b)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.