SciLab 4
SciLab 4
UID - 2021600026
SciLab 4
Gauss Elimination Method
Code:
clc;
a = [1 1 1; 1 2 3; 1 3 2];
b = [3; 0; 3];
c = [a b];
printf("Matrix A:");
disp(a);
printf("Matrix B:");
disp(b);
printf("Matrix C Augumented matrix is:");
disp(c);
n = 3;
for i=1:n
if c(i,i) == 0
c(i,:) = c(i,:);
else
c(i,:) = c(i,:)/c(i,i);
disp(c);
for j = 1:n-1
if i+j < n+1
c(i+j,:) = c(i+j,:) - c(i+j,i)*c(i,:);
else
end
end
end
if c(1,2) == c(2,2)
c(1,:) = c(1,:) - c(2,:);
else
end
disp(c);
end
printf("Value of z
is"); z = c(3,4);
disp(z);
printf("Value of y
is"); y = c(2,4)-
c(2,3)*z;
disp(y);
printf("Value of x is"); x =
c(1,4)-c(1,3)*z-c(1,2)*y;
disp(x);
Output:
Program 2:
Write a scilab code to solve the following equations in terms of x, y, z and w by using
gauss elimination method.
2x+y+w=2;
5x-4y+z=1;
3x+2z=-2;
x+y-z+w=1
Code:
clc;
a = [2 1 0 1; 5 -4 1 0; 3 0 2 0; 1 1 -1 1];
b = [2; 1; -2; 1];
c = [a b];
printf("Matrix A:");
disp(a);
printf("Matrix B:");
disp(b);
printf("Matrix C augumented matrix is:");
disp(c);
n = 4;
for i=1:n
if c(i,i) == 0
c(i,:) = c(i,:);
else
c(i,:) = c(i,:)/c(i,i);
disp(c);
for j = 1:n-1
if i+j < n+1
c(i+j,:) = c(i+j,:) - c(i+j,i)*c(i,:);
else
end
end
end
if c(1,2) == c(2,2)
c(1,:) = c(1,:) - c(2,:);
else
end
disp(c);
end
printf("Value of w is");
w = c(4,5);
disp(w);
printf("Value of z is");
z = c(3,5)-w*c(3,4);
disp(z);
printf("Value of y is");
y = c(2,5)-c(2,4)*w-z*c(2,3);
disp(y);
printf("Value of x is");
x = c(1,5)-c(1,4)*w-c(1,3)*z-c(1,2)*y;
disp(x);
Output:
Program 3:
Code:
clc;
a = [2 1 5 -1; 1 -3 0 -6; 0 2 -1 2; 1 4 -7 6]
b = [1; -7; 3; 5];
c = [a b];
printf("Matrix A:");
disp(a);
printf("Matrix B:");
disp(b);
printf("Matrix C augumented matrix is:");
disp(c);
n = 4;
for i=1:n
if c(i,i) == 0
c(i,:) = c(i,:);
else
c(i,:) = c(i,:)/c(i,i);
disp(c);
for j = 1:n-1
if i+j < n+1
c(i+j,:) = c(i+j,:) - c(i+j,i)*c(i,:);
else
end
end
end
if c(1,2) == c(2,2)
c(1,:) = c(1,:) - c(2,:);
else
end
disp(c);
end
printf("Value of w is");
w = c(4,5);
disp(w);
printf("Value of z is");
z = c(3,5)-w*c(3,4);
disp(z);
printf("Value of y is");
y = c(2,5)-c(2,4)*w-z*c(2,3);
disp(y);
printf("Value of x is");
x = c(1,5)-c(1,4)*w-c(1,3)*z-c(1,2)*y;
disp(x);
Output:
Program 3:
Code:
clc;
a = [-3 2 -6; 5 7 -5; 1 4 -2]
b = [6; 6; 8];
c = [a b];
printf("Matrix A:");
disp(a);
printf("Matrix B:");
disp(b);
printf("Matrix C augumented matrix is:");
disp(c);
n = 3;
for i=1:n
if c(i,i) == 0
c(i,:) = c(i,:);
else
c(i,:) = c(i,:)/c(i,i);
disp(c);
for j = 1:n-1
if i+j < n+1
c(i+j,:) = c(i+j,:) - c(i+j,i)*c(i,:);
else
end
end
end
if c(1,2) == c(2,2)
c(1,:) = c(1,:) - c(2,:);
else
end
disp(c);
end
printf("Value of z is");
z = c(3,4);
disp(z);
printf("Value of y
is"); y = c(2,4)-
c(2,3)*z;
disp(y);
printf("Value of x is"); x =
c(1,4)-c(1,3)*z-c(1,2)*y;
disp(x);
Output:
Program 4:
Write a scilab code to solve the following equations in terms of x1 + x2 + x3 + x4 by using
gauss elimination method.
4x1 + x2 + x3 + x4 = 2.4;
x1 + 5x2 + 2x3 + x4 = 0.7;
2x1 - 3x2 + 3x3 + 2x4 = 3.5;
3x1 + x2 - x3 + 5x4 = 2.7
Code:
clc;
a = [4 1 1 1; 1 5 2 1; 2 -3 3 2; 3 1 -1 5]
b = [2.4; 0.7; 3.5; 2.7];
c = [a b];
printf("Matrix A:");
disp(a);
printf("Matrix B:");
disp(b);
printf("Matrix C augumented matrix is:");
disp(c);
n = 4;
for i=1:n
if c(i,i) == 0
c(i,:) = c(i,:);
else
c(i,:) = c(i,:)/c(i,i);
disp(c);
for j = 1:n-1
if i+j < n+1
c(i+j,:) = c(i+j,:) - c(i+j,i)*c(i,:);
else
end
end
end
if c(1,2) == c(2,2)
c(1,:) = c(1,:) - c(2,:);
else
end
disp(c);
end
printf("Value of x4 is");
w = c(4,5);
disp(w);
printf("Value of x3 is");
z = c(3,5)-w*c(3,4);
disp(z);
printf("Value of x2 is");
y = c(2,5)-c(2,4)*w-z*c(2,3);
disp(y);
printf("Value of x1 is");
x = c(1,5)-c(1,4)*w-c(1,3)*z-c(1,2)*y;
disp(x);
Output: