In: Computer Science
MATLAB script Gaussian Elimination without pivoting, not working no matter what I try.
So far my script is :
function [x] = myGauss( A, b )
n = size(A,1); % getting n
Ab = [A,b]; % produces the augmented
matrix
x = zeros(n,1); % solution
fprintf('Augmented matrix \n')
%FORWARD ELIMINATION
for k=1:n-1
for i=k+1:n
lambda =
A(i,k)/A(k,k);
for j=k+1:n
A(i,j) = A(i,j) - lambda*A(k,j);
end;
b(i) = b(i) -
lambda*b(i)
end;
end;
%Backwards substitution
x(n) = b(n) / A(n,n);
for k = n-1:1
for j = k+1: n
sums = b(k) - A(k,j) *
x(j);
end
x(k) = sums / A(k,k);
end
and to launch it I have:
%Question 1:
A = [2, -4, -1;
1, -3, 1;
3, -5, -3]
b = [1; 1; 1];
x = myGauss( A, b )
Any highly appreciated.
Corrected script:
function y=myyygauss(M, Y)
[m,n] = size (M);
D = [M Y];
for j = 1:n-1
for i = j+1:m
T = D(i,j)/D(j,j);
D(i,j:n+1) = D(i,j:n+1)-T*D(j,j:n+1);
end
end
x = zeros(m,1);
x(m) =D(m,n+1)/D(m,m);
for i = m-1:-1:1
x(i) = (D(i,n+1)-D(i,i+1:n)*x(i+1:n))/D(i,i);
end
y=x
end
To use the above script:
a1=[1 2 3;-4 5 6; -7 8 9];
a2=[10 20 30]';
a=myyygauss(a1,a2);
Output:
>> a
a =
0.00000
0.00000
3.33333
_____________________________________________________________
sample 2:
a1=[1 -2 -3;-4 5 6; -7 8 -9];
a2=[10 20 30]';
a=myyygauss(a1,a2);
output:
>> a
a =
-31.1111
-22.2222
1.1111
____________________________________________________________________________________
But for the given matrix A, the determinant value is equal to 0.Gauss elimination method can be applied only for linear set of equations. If the det ==0 , then the set of equations say that the system is non linear.
>> A = [2, -4, -1;
1, -3, 1;
3, -5, -3]
A =
2 -4 -1
1 -3 1
3 -5 -3
>> det(A)
ans = -8.8818e-16
This is almost equal to zero. Hence the solution cannot be obtained. But the script will work for all the linear set of equations.
Before applying gauss elimination method or any other method to find the solution of linear equations, check whether the determinant is zero or not.