PRACTICAL-4
AIM: To develop algorithm techniques for linear algebraic equations
PROBLEM STATEMENT:
1)solve with the Gauss elimination method
x-y+2z=3 ; x+2y+3z ; 3x-4y-5z=-13
INPUT
//gauss elimination method
clc;clear;
a=[1 -1 2; 1 2 3 ;3 -4 -5]
b=[3; 5 ;-13]
n=length(b)
a=[a b]
disp(a)
for j=1:n-1
   for i=j+1:n
     m=a(i,j)/a(j,j)
     a(i,:)=a(i,:)-(m*a(j,:))
   end
  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
PROBLEM STATEMENT:
2) using gauss Jordan method, solve following system of equations:
 x-y+2z =3; x+2y+3z =5; 3x-4y-5z =-13
 INPUT
 //gauss Jordan method
clc;clear;
a=[1 -1 2; 1 2 3 ;3 -4 -5]
b=[3; 5 ;-13]
n=length(b)
a=[a b]
disp(a)
for j=1:n
   a(j,:)=a(j,:)/a(j,j)
   for i=1+j:n
      if i~=j
      m=a(i,j)/a(j,j)
      a(i,:)=a(i,:)-(m*a(j,:))
    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
PROBLEM STATEMENT:
3) using gauss jacobi solve: 27x + 6y -z=85; 6x + 15y +2z = 72; x+y+54z = 110
INPUT:
//gauss jacobi method of iteration
clc;clear;
a=[27 6 -1 ;6 15 2; 1 1 54]
b=[85 ; 72 ; 110]
n=length(b)
c=0
for i=1:n
   s=0
   for j=1:n
      if j~=i then
      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 not diagonally dominant")
end
x=input("enter the first value")
y=input("enter the second value")
z=input("enter the third value")
n=input("enter the number 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))
PROBLEM STATEMENT:
4) using gauss seidel solve: 27x + 6y -z=85; 6x + 15y +2z = 72; x+y+54z = 110
INPUT
//gauss seidel method of iteration
clc;clear;
a=[27 6 -1 ;6 15 2; 1 1 54]
b=[85 ; 72 ; 110]
n=length(b)
c=0
for i=1:n
   s=0
   for j=1:n
      if j~=i then
      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 not diagonally dominant")
   end
x=input("enter the first value")
y=input("enter the second value")
z=input("enter the third value")
n=input("enter the number 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+1))-(a(2,3)*z(i)))/a(2,2)
   z(i+1)=(b(3,1)-(a(3,1)*x(i+1))-(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))
                                     OUTPUT
 1st OUTPUT
-1. 2. 3. 1.
2. 3. 5.
3. -4. -5. -13.
"x(3)=2"
"x(2)=0"
"x(1)=-1"
2nd OUTPUT
1. 2. 3.
1. 2. 3. 5.
3. -4. -5. -13.
"x(3)=2"
"x(2)=0"
"x(1)=-1"
3rd OUTPUT
"Matrix is diagonally dominant"
enter the first value27
enter the second value 6
Enter the third value-1
enter the number of iterations20
"no of iteration" - 12
2.4254702
3.5729574
1.9259507
4th OUTPUT
"Matrix is diagonally dominant"
enter the first value27
enter the second value6
enter the third value-1
enter the number of iterations20 "
no of iteration"- 7
2.4254578
3.5730245
1.9259527