0% found this document useful (0 votes)
0 views

SciLab 4

The document contains multiple Scilab programs that implement the Gauss elimination method to solve systems of linear equations. Each program provides the code, displays the augmented matrix, and calculates the values of the variables involved. The equations vary in complexity and number of variables, demonstrating the application of the Gauss elimination technique.

Uploaded by

Vinay Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

SciLab 4

The document contains multiple Scilab programs that implement the Gauss elimination method to solve systems of linear equations. Each program provides the code, displays the augmented matrix, and calculates the values of the variables involved. The equations vary in complexity and number of variables, demonstrating the application of the Gauss elimination technique.

Uploaded by

Vinay Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Name - Vinay Prasad Jadhav

UID - 2021600026

Batch - AIML (C2)

SciLab 4
Gauss Elimination Method

Program 1: Write a scilab code to solve the following equations in terms of x, y, z by


using gauss elimination method.
x+y+z=3;
x+2y+3z=0;
x+3y+2z=3

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:

Write a scilab code to solve the following equations in terms of x, y, z, w by using


gauss elimination method.
A = [2 1 5 -1; 1 -3 0 -6; 0 2 -1 2; 1 4 -7 6]
B = [1; -7; 3; 5]

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:

Write a scilab code to solve the following equations in terms of x, y, z, w by using


gauss elimination method.
-3x+2y-6z=6;
5x+7y-5z=6;
x+4y-2z=8

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:

You might also like