You are on page 1of 3

//April Faith L.

Manabat
//BSEE - 3
//ES 84
clear
clc
disp('This program uses Gaussian Elimination in finding the roots of the given set of equations.')
//given set of equations
disp('The given set of equations are: ')
disp(' 1.1 X1 + 3 X2 + 8 X3 + 5 X4 = 7.3')
disp(' .3 X1 - 11 X2 + 4.4 X3 + 1.25 X4 = -.33')
disp('3.75 X1 + 5.25 X2 - 10.2 X3 - 4.54 X4 = 2.5')
disp('-3.2 X1 - X2 + 1.5 X3 - 3 X4 = 5')
disp('Solve for X1, X2, X3, X4 using Gaussian Elimination: ')
A = [1.1,3,8,5;.3,-11,4.4,1.25;3.75,5.25,-10.2,-4.54;-3.2,-1,1.5,-3];
B = [7.3;-.33;2.5;5];
m=4
for f=1:m-1 //forward elimination(upper triangle)
for i=f+1:m
z=A(i,f)/A(f,f);
for j=f:4
A(i,j)=A(i,j)-z*A(f,j);
end
B(i)=B(i)-z*B(f);
end
end
X(m)=B(m)/A(m,m);//backward substitution(solving for X(m))
for i=m-1:-1:1
s = B(i);
for j=i+1:m
s = s - X(j)*A(i,j);
end
X(i) = s/A(i,i);
end
u=triu(A);
disp(u);
disp(B);
printf('\m');
for j=1:4
printf('X%g = %g\m',j, X(j));
end

//April Faith L. Manabat


//BSEE - 3
//ES 84
clear
clc
disp('This program uses LU Decomposition in finding the roots of the given set of equations :)')
//given set of equations
disp('For the given set of equations: ')
disp(' 1.1 X1 + 3 X2 + 8 X3 + 5 X4 = 7.3')
disp(' .3 X1 - 11 X2 + 4.4 X3 + 1.25 X4 = -.33')
disp(' 3.75 X1 + 5.25 X2 - 10.2X3 - 4.54 X4 = 2.5')
disp(' -3.2 X1 - X2 + 1.5 X3 - 3 X4 = 5')
disp('Solve for X1, X2, X3, X4 using LU Decomposition: ')
A = [1.1,3,8,5;.3,-11,4.4,1.25;3.75,5.25,-10.2,-4.54;-3.2,-1,1.5,-3];
B = [7.3;-.33;2.5;5];
m=4;
//solving for the lower triangle
l=A;
for f=1:4-1
for i=f+1:4
z=l(f,i)/l(f,f);
for j=f:4
l(j,i)=l(j,i)-z*l(j,f);
end
end
end
for f=1:4
for i=f+1:4
l(i,f)=l(i,f)/l(f,f)
end
l(f,f)=l(f,f)/l(f,f)
end
disp(l)
//forward substitution(solving for the values of D, [L][D]=[B])
f=1;
for i=f+1:4
S=B(i);
for j=f:i-1
S=S-l(i,j)*B(j);
end
B(i)=S;
end
for j=1:4

printf('\m');
printf('D(%g) = %g\m',j,B(j));
end
//solving for upper triangle
u=A;
for f=1:4-1
for i=f+1:4
z=u(i,f)/u(f,f);
for j=f:4
u(i,j)=u(i,j)-z*u(f,j);
end
end
end
u=triu(u);
disp(u);
//backward substitution(solving for the values of X, [U][X]=[D])
X(m)=B(m)/u(m,m);
for i=m-1:-1:1
S=B(i);
for j=i+1:4
S=S-u(i,j)*X(j);
end
X(i)=S/u(i,i);
end
for j=1:4
printf('\m');
printf('X(%g) = %g\m',j,X(j))
end

You might also like