You are on page 1of 3

Assignment No.

- 02
Name of Assignment: Simultaneous Equation Roll No.:
Name of Student: Date:
Problem 1: Using Gauss Elimination method with partial pivoting. Solve the following equations.
5x1 + 13x2 + 7x3 = 14
3x1 + 6x2 + 3x3 = 9
7x1 + 2x2 + 4x3 = 5
Solution:
Program
clc;
n = input ('Enter the No of Unknown = ');
for i = 1 : n
for j = 1 : n
fprintf('Enter a%d,%d = ',i,j);
a(i,j) = input('');
end
end
for i = 1 : n
fprintf('b%d = ',i);
b(i) = input('');
end
fprintf ('Matrix A =\n');
disp (a);
fprintf ('\nMatrix B =\n');
disp (b);
for i = 1 : n
m = abs(a(i,i));
mrow = i;
for j = i : n
if (m < abs(a(j,i)))
m = abs(a(j,i));
mrow = j;
end
end
m = a(mrow,i);
if mrow > i
for j = i : n
temp = a(i,j);
a(i,j) = a(mrow,j);
a(mrow,j) = temp;
end
temp = b(i);
b(i) = b(mrow);
b(mrow) = temp;
end
for j = i : n
a(i,j) = a(i,j)/m;
end
b(i) = b(i)/m;
for j = i + 1 : n
m = a(j,i);
for k = i : n
a(j,k) = a(j,k) - a(i,k)*m;
end
b(j) = b(j)-m*b(i);
end
end
fprintf(' Upper Triangular Matrix \nMatrix A = \n');
disp (a);
fprintf('\nMatrix B = \n');
disp (transpose(b));
for i = n : -1 : 1
x(i) = b(i);
for j = i+1 : n
x(i) = x(i) - a(i,j)*x(j);
end
end
for i = 1:n
fprintf('\nX%d = %f',i,x(i));
end

Output:
Enter the No of Unknown = 3
Enter a1,1 = 5
Enter a1,2 = 13
Enter a1,3 = 7
Enter a2,1 = 3
Enter a2,2 = 6
Enter a2,3 = 3
Enter a3,1 = 7
Enter a3,2 = 2
Enter a3,3 = 4
b1 = 14
b2 = 9
b3 = 5
Matrix A =
5 13 7
3 6 3
7 2 4

Matrix B =
14
9
5

Upper Triangular Matrix


Matrix A =
1.0000 0.2857 0.5714
0 1.0000 0.3580
0 0 1.0000

Matrix B =
0.7143 0.9012 -4.0000
X1 = 2.333333
X2 = 2.333333
X3 = -4.000000>>

Problem 2: Solve the above equation with the help of solver.

>> a = [5 13 7; 3 6 3; 7 2 4];
>> b = [14; 9; 5];
>> a\b

ans =

2.3333
2.3333
-4.0000

Result:
We get the value of x1= 2.333333, x2 = 2.333333 and x3 = -4.000000 and with the help of solver x1
= 2.3333, x2 = 2.3333 and x3 = -4.0000 hence the program is correct.

You might also like