In: Advanced Math
%%% Gauss Ellimination
function [ Ahat, x]=GaussElim( A,b)
if det(A)==0
disp('Unique solution is not possible : \n');
else
[r c]=size(A);
for n=1:r
Ahat(n,:)=[A(n,:) b(n)];
end
for i=1:r
k=0;
for j=1:c
if (i==j)
Ahat(i,:)=Ahat(i,:)/Ahat(i,j);
k=1;
end
if ( j > i)
if (k==1)
Ahat(j,:)=Ahat(j,:)-Ahat(j,i)*Ahat(i,:);
end
end
end
end
x(1:r)=0;
x(r)=Ahat(end,end);
for i=r-1:-1:1
for j=c:-1:1
if (j > i)
x(i)=x(i)-x(j)*Ahat(i,j);
end
end
x(i)=x(i)+Ahat(i,end);
end
end
end
%%% Jacobi iteration
function [ x1]= jacobi ( A, b);
tol=10^(-5);
disp('Gauss jacobi method');
[r,c]=size(A);
for n=1:r
for k=1:c
if (n==k)
D(n,k)=A(n,k);
else
R(n,k)=A(n,k);
end
end
end
x(:,1)=[0 0 0]'; %%%Initial guess
disp('Solution at each iteration ');
% fprintf(' d p g \n');
for n=1:40
x(:,n+1)=inv(D)*(b-R*x(:,n));
err=x(:,n+1)-x(:,n);
if (norm(err,Inf) < tol)
%
break;
end
fprintf('iteration %d %f %f %f \n',n,x(1,n),x(2,n), x(3,n));
end
x1=x(:,end);
end