//gauss elimination method and gauss jordan method
clc; clear;
a=[12 3 -5; 1 5 3; 3 7 13]
b=[1;28;76]
n=length(b)
a=[a b]
disp(a)
for j=1:n-1. // for j=1:n
//a(j,:)=a(j,:)/a(j,j) remove //
for i=j+1:n //for i=1:n
if i~=j
m=a(i,j)/a(j,j)
a(i,:)=a(i,:)-(m*a(j,:))
end
//disp(a)
end
disp(a)
end
x=zeros(1,n)
for i=n:-1:1
ax=sum(a(i,1:n).*x)
x(i)=(a(i,n+1)-ax)/a(i,i)
disp('x('+string(i)+')='+string(x(i)))
end
// gauss Jacobi method and gauss siedel method
a=[3 -1 1;-1 4 1;-1 1 5]
b=[20;6;7]
n=length(b)
c=0
for i=1:n
s=0
for j=1:n
if j~=i
s=s+a(i,j)
end
end
if (a(i,i)>s)
c=c+1
else break
end
end
if c==n then
disp('matrix is diagonally dominant')
else
disp('matrix is diagonally not dominant')
end
x=input('Initial guess of first value ');
y=input('Initial guess of second value ');
z=input('initial guess of third value ');
n=input('Enter no. of iterations ')
for i=1:n
x(i+1)=(b(1,1)-(a(1,2)*y(i))-(a(1,3)*z(i)))/a(1,1)
y(i+1)=(b(2,1)-a(2,1)*x(i)-a(2,3)*z(i))/a(2,2)
z(i+1)=(b(3,1)-a(3,1)*x(i)-a(3,2)*y(i))/a(3,3)
if (abs(x(i+1)-(x(i)))<0.0001)& (abs(y(i+1)-(y(i)))<0.0001) &
(abs(z(i+1)-(z(i)))<0.0001)
break
end
end
disp("No. of iteration",i,x(i),y(i),z(i))