0% found this document useful (0 votes)
12 views27 pages

Ce007 S2 Gaddi MP2.1-1

The document outlines a machine problem submission for a course on numerical solutions to civil engineering problems, detailing algorithms for solving systems of linear equations using methods such as Gauss Elimination, Gauss Jordan, LU Decomposition, and iterative methods. It includes programming examples for Gaussian elimination and LU decomposition, as well as specific machine problems involving Hilbert matrices and various numerical methods. The author, Marken John B. Gaddi, pledges academic integrity in completing the coursework.

Uploaded by

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

Ce007 S2 Gaddi MP2.1-1

The document outlines a machine problem submission for a course on numerical solutions to civil engineering problems, detailing algorithms for solving systems of linear equations using methods such as Gauss Elimination, Gauss Jordan, LU Decomposition, and iterative methods. It includes programming examples for Gaussian elimination and LU decomposition, as well as specific machine problems involving Hilbert matrices and various numerical methods. The author, Marken John B. Gaddi, pledges academic integrity in completing the coursework.

Uploaded by

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

TECHNOLOGICAL INSTITUTE OF THE PHILIPINES

938 Aurora Blvd., Cubao, Quezon City

CE 007 (Numerical Solutions to CE Problems)

Machine Problem 2.1


Numerical Solutions of Equations and Systems of Equations

Submitted by:
Gaddi, Marken John B.
1811969

ACADEMIC INTEGRITY PLEDGE

I swear on my honor that I did not use any inappropriate aid, nor give such to others, in accomplishing
this coursework. I understand that cheating and/or plagiarism is a major offense, as stated in TIP
Memorandum No. P-04, s. 2017-2018, and that I will be sanctioned appropriately once I have committed
such acts.

Marken John B. Gaddi


1811969
MACHINE EXERISE:

a. Make an Algorithm for solving systems of linear algebraic equations using the methods below:

- Gauss Elimination

Step 1. Start
Step 2. Declare the variables and read the order of the matrix n.
Step 3. Take the coefficients of the linear equation as:
Do for k=1 to n
Do for j=1 to n+1
Read a[k][j]
End for j
End for k
Step 4. Do for k=1 to n-1
Do for i=k+1 to n
Do for j=k+1 to n+1
a[i][j] = a[i][j] – a[i][k] /a[k][k] * a[k][j]
End for j
End for i
End for k
Step 5. Compute x[n] = a[n][n+1]/a[n][n]
Step 6. Do for k=n-1 to 1
sum = 0
Do for j=k+1 to n
sum = sum + a[k][j] * x[j]
End for j
x[k] = 1/a[k][k] * (a[k][n+1] – sum)
End for k
Step 7: Display the result x[k]
Step 8. Stop
- Gauss Jordan
Step 1. Start
Step 2. Read the order of the matrix ‘n’ and read the coefficients of the linear
equations.
Step 3. Do for k=1 to n
Do for l=k+1 to n+1
a[k][l] = a[k][l] / a[k][k]
End for l
Set a[k][k] = 1
Do for i=1 to n
if (i not equal to k) then,
Do for j=k+1 to n+1
a[i][j] = a[i][j] – (a[k][j] * a[i][k])
End for j
End for i
End for k
Step 4. Do for m=1 to n
x[m] = a[m][n+1]
Display x[m]
End for m
Step 5. Stop

- LU Decomposition Methods

1. Doolittle’s Decomposition

Step 1. Start
Step 2. Read the elements of augmented matrix into arrays a and b
Step 3. Calculate elements of L and U
Step 4. Print elements of L and U
Step 5. Find V by solving LV = B by forward substitution
Step 6. Find X by solving UX = V by backward substitution
Step 7. Print Array X as the solution
Step 8. Stop

2. Crout’s Decomposition

Crout's method decomposes a nonsingular n × n matrix A into the product of an n×n lower triangular
matrix L and an n×n unit upper triangular matrix U. A unit triangular matrix is a triangular matrix with 1's
along the diagonal. Crout's algorithm proceeds as follows:
Evaluate the following pair of expressions for k = 0, . . . , n-1;

And

Note that Lik = 0 for k > i, Uik = 0 for k < i, and Ukk = 1 for k = 0, …, n-1. The matrix U forms a unit upper
triangular matrix, and the matrix L forms a lower triangular matrix. The matrix
A = LU.
After the LU decomposition of A is performed, the solution to the system of linear equations A x = L U x =
B is solved by solving the system of linear equations L y = B by forward substitution for y, and then solving
the system of linear equations U x = y by backward substitution for x.

3. Cholesky’s Decomposition
- Iterative Methods

1. Gauss-Jacobi

2. Gauss-Seidel
3. Successive Relaxation
4. Conjugate Gradient

The algorithm is detailed below for solving Ax = b where A is a real, symmetric, positive-definite matrix.
The input vector x0 can be an approximate initial solution or 0.

b. Write a program for solving the system Ax = b by

- Gaussian Elimination Algorithm

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{int n,i,j,k;
cout << setprecision(3)<<fixed;
cout.setf(ios::fixed);
cout<<"Enter the number of unknowns: ";
cin>>n;
float a[n][n+1],x[n];
cout<<"Enter elements of augmented matrix (row-wise): "<< endl;
for (i=0;i<n;i++)
for (j=0;j<=n;j++)
cin>>a[i][j];
for (i=0;i<n-1;i++)
for (k=i+1;k<n;k++)
{double t=a[k][i]/a[i][i];
for (j=0;j<=n;j++)
a[k][j]=a[k][j]-t*a[i][j];}
cout<<"\n\nThe matrix after elementary row operations to conduct gauss-
elimination is as follows:\n";
for (i=0;i<n;i++)
{for (j=0;j<=n;j++)
cout<<a[i][j]<<setw(16);
cout<<"\n";}
for (i=n-1;i>=0;i--)
{x[i]=a[i][n];
for (j=i+1;j<n;j++)
if (j!=i)
x[i]=x[i]-a[i][j]*x[j];
x[i]=x[i]/a[i][i];}
cout<<"\nThe solution in the system:\n";
for (i=0;i<n;i++)
cout<<x[i]<<endl;
return 0; }
- LU Decomposition Methods
1. Doolittle’s Decomposition

#include<iostream>
using namespace std;
void LUdecomposition(float a[10][10], float l[10][10], float u[10][10], int z) {
int w=0, q=0, y=0;
for (w=0; w<z; w++) {
for (q=0; q<z; q++) {
if (q<w)
l[q][w] = 0;
else {
l[q][w]=a[q][w];
for (y=0; y<w; y++) {
l[q][w] = l[q][w] - l[q][y] * u[y][w];}}}
for (q=0; q<z; q++) {
if (q < w)
u[w][q] = 0;
else if (q == w)
u[w][q] = 1;
else {
u[w][q] = a[w][q] / l[w][w];
for (y=0; y<w; y++) {
u[w][q] = u[w][q] - ((l[w][y] * u[y][q]) / l[w][w]);}}}}}
int main() {
float a[10][10], l[10][10], u[10][10];
int z=0, w=0, q=0;
cout << "INPUT NO. OF EQUATION: "<<endl;
cin >> z;
cout<<"INPUT COEFFICIENT OF THE EQUATION (ROW-WISE): "<<endl;
for (w=0; w<z; w++)
for (q=0; q<z; q++)
cin >> a[w][q];
LUdecomposition(a, l, u, z);
cout << "L DECOMPOSITION..."<<endl;
for (w=0; w<z; w++) {
for (q=0; q<z; q++) {
cout<<l[w][q]<<" ";}
cout << endl}
cout << "U DECOMPOSITION..."<<endl;
for (w=0; w<z; w++) {
for (q=0; q<z; q++) {
cout<<u[w][q]<<" ";}
cout << endl;}
return 0;}

- Iterative Methods
2. Conjugate Gradient

vec conjugateGradientSolver( const matrix &A, const vec &B )


{ double TOLERANCE = 1.0e-10
int n = A.size();
vec X( n, 0.0 );
vec R = B;
vec P = R;
int k = 0;
while ( k < n )
{vec Rold = R
vec AP = matrixTimesVector( A, P );
double alpha = innerProduct( R, R ) / max( innerProduct( P, AP ), NEARZERO );
X = vectorCombination( 1.0, X, alpha, P );
R = vectorCombination( 1.0, R, -alpha, AP );
if ( vectorNorm( R ) < TOLERANCE ) break;
double beta = innerProduct( R, R ) / max( innerProduct( Rold, Rold ), NEARZERO );
P = vectorCombination( 1.0, R, beta, P );
k++;}
return X;}

MACHINE PROBLEM:

1. (Hilbert matrix) Suppose A=[hij],hij=1i+j−1,i,j=1,...,n. Also, let b=Ax, xi=1, 1, ... , n, n=5, 10, 20.
• Solve for Ax' = b (given any value for x(0)), and compare the results for different values of n with
x* = 1 (i.e., one vector). Discuss in detail the results obtained for each case.
• Note: Hilbert matrices are notoriously ill-conditioned matrices. Because of this, you may get
almost zero pivot during the process, and as a result, you may not be able to solve the linear
system for some large n.
2. Solve the equations Ax = b where

- Gauss Elimination

A= [1.44, -0.36, 5.52, 0.00; -0.36, 10.33, -7.78, 0.00; 5.52, -7.78, 28.40,9.00; 0.00, 0.00, 9.00,
61.00]
b = [0.04; -2.15; 0; 0.88]
Ab = [A,b]
Ab(1,:)= Ab(1,:)/1.44
Ab(2,:)= Ab(2,:)+ Ab(1,:)*0.36
Ab(3,:)= Ab(3,:) - Ab(1,:)*5.52
Ab(2,:)= Ab(2,:)/10.24
Ab(3,:) = Ab(3,:) + Ab(2,:)*6.4
Ab(3,:) = Ab(3,:)/3.24
Ab(4,:) = Ab(4,:) - Ab(3,:)*9
Ab(4,:)= Ab(4,:)/36
x = zeros(4,1);
x(4)= Ab(4,end);
x(3) = (Ab(3,end)-Ab(3,4)*x(4));
x(2) = (Ab(2,end) - Ab(2,3)*x(3));
x(1) = (Ab(1,end) - Ab(1,2)*x(2) - Ab(1,3)*x(3))
- Gauss-Jordan

A= [1.44, -0.36, 5.52, 0.00; -0.36, 10.33, -7.78, 0.00; 5.52, -7.78, 28.40,9.00; 0.00, 0.00, 9.00,
61.00]
b = [0.04; -2.15; 0; 0.88]
Ab = [A,b]
X = rref(Ab)
x = X(:,5)
- LU Decomposition Methods
1. Doolittle’s Decomposition

A= [1.44, -0.36, 5.52, 0.00; -0.36, 10.33, -7.78, 0.00; 5.52, -7.78, 28.40,9.00; 0.00, 0.00,
9.00, 61.00]
b = [0.04; -2.15; 0; 0.88]
M = sym(A)
[L,U]=lu(M)
Y = (L^-1)*b
X = (U^-1)*Y
2. Crout’s Decomposition

A= [1.44, -0.36, 5.52, 0.00; -0.36, 10.33, -7.78, 0.00; 5.52, -7.78, 28.40,9.00; 0.00, 0.00,
9.00, 61.00]
b = [0.04; -2.15; 0; 0.88]
L = chol(A,'lower')
U = chol(A,'upper')
Y = (L^-1)*b
X = (U^-1)*Y
3. Cholesky’s Decomposition

A= [1.44, -0.36, 5.52, 0.00; -0.36, 10.33, -7.78, 0.00; 5.52, -7.78, 28.40,9.00; 0.00, 0.00,
9.00, 61.00]
b = [0.04; -2.15; 0; 0.88]
L = chol(A,'lower')
U = chol(A,'upper')
Y = (L^-1)*b
X = (U^-1)*Y
- Iterative Methods

1. Gauss-Jacobi

A=[1.44, -0.36, 5.52, 0.00; -0.36, 10.33, -7.78, 0.00; 5.52, -7.78, 28.40,9.00; 0.00, 0.00, 9.00,
61.00]
b=[0.04; -2.15; 0; 0.88]'
x=[0 0 0 0]'
n=size(x,1);
normVal=Inf;
tol=1e-5; itr=0;
while normVal>tol
xold=x;

for i=1:n
sigma=0;

for j=1:n

if j~=i
sigma=sigma+A(i,j)*x(j);
end

end

x(i)=(1/A(i,i))*(b(i)-sigma);
end

itr=itr+1;
normVal=abs(xold-x);
end
%%
fprintf('Solution of the system is : \n%f\n%f\n%f\n%f in %d iterations',x,itr);

2. Gauss-Seidel

A=[1.44, -0.36, 5.52, 0.00; -0.36, 10.33, -7.78, 0.00; 5.52, -7.78, 28.40,9.00; 0.00, 0.00, 9.00,
61.00]
b=[0.04; -2.15; 0; 0.88]'
x=[0 0 0 0]'
n=size(x,1);
normVal=Inf;
tol=1e-5; itr=0;
while normVal>tol
x_old=x;
for i=1:n

sigma=0;

for j=1:i-1
sigma=sigma+A(i,j)*x(j);
end

for j=i+1:n
sigma=sigma+A(i,j)*x_old(j);
end

x(i)=(1/A(i,i))*(b(i)-sigma);
end

itr=itr+1;
normVal=norm(x_old-x);
end
fprintf('Solution of the system is : \n%f\n%f\n%f\n%f in %d iterations',x,itr);
3. Successive Relaxation

function x = SOR( A ,B )
disp ( ' Enter system of equations in the form of AX=B')
A = input ( ' Enter matrix A : \')
[na , ma ] = size (A);
if na ~= ma
disp('ERROR:matrix A should be a square matrix')
return
end
B = input ( ' Enter matrix B : ')
[nb , mb ] = size (B);
if nb ~= na || mb~=1
disp( 'ERROR:input error..pls re-enter data')
return
end
w=input('Enter the relaxation parameter ');
D = diag(diag(A));
L = tril(A)- D;
U = triu(A)- D;
e= max(eig(inv( D+w*L) * ( D*(1-w) - w*U)));

if abs(e) >= 1
disp ('Since the modulus of largest eigen value of iterative matrix is not less than 1')
disp ('This process is not convergent. Please try some other process.')
return
end
r = input ( 'Any initial guess for X? (y/n): ','s');
switch r
case 'y'
X0 = input('pls enter initial guess for X :\n')
[nx, mx] = size(X0);
if nx ~= na || mx ~= 1
disp( 'ERROR: pls check input')
return
end
otherwise
X0 = ones(na,1);
end
t = input ( 'enter the error allowed in final ans: ');
tol = t*ones(na,1);
k= 1;

X( : , 1 ) = X0;
err= 1000000000*rand(na,1);
while sum(abs(err) >= tol) ~= zeros(na,1)
X ( : ,k+ 1 ) = inv(D+w*L) * ( D*(1-w) - w*U)*X( : ,k) + inv(D+ w*L)*B;
err = X( :,k+1) - X( :, k);
k = k + 1;
end
fprintf (' The final ans obtaibed after %d itaration which is \n', k)
X( : ,k)
4. Conjugate Gradient

function[x]=conjgrad(A,b,x0,ni)
A=[1.44, -0.36, 5.52, 0.00; -0.36, 10.33, -7.78, 0.00; 5.52, -7.78, 28.40,9.00; 0.00, 0.00, 9.00,
61.00]
b=[0.04; -2.15; 0; 0.88]
x0=[0;0;0;0]
ni=100
x=[];
r=[];
p=[];
alfa=[];
beta=[];
n=size(A);
n=n(1,1);
k=1;
for nn=1:n
x(nn,k)=x0(nn,1);
end
r(:,k)=b-A*x(:,k);
p(:,k)=r(:,k);
for k=1:ni
alfa(:,k)=r(:,k)'*r(:,k)/(p(:,k)'*A*p(:,k));
x(:,(k+1))=x(:,k)+alfa(:,k)*p(:,k);
r(:,(k+1))=r(:,k)-alfa(:,k)*A*p(:,k);
if (norm(r(:,(k+1)))<1e-10)
break;
end
beta(:,k)=r(:,(k+1))'*r(:,(k+1))/(r(:,k)'*r(:,k));
p(:,(k+1))=r(:,(k+1))+beta(:,k)*p(:,k);
end
x=x(:,(k+1));
- References:

• https://www.codewithc.com/gauss-elimination-method-algorithm-
• https://www.codewithc.com/gauss-jordan-method-algorithm-flowchart/
• https://www.codewithc.com/lu-decomposition-algorithm-flowchart/
• https://www.codewithc.com/gauss-seidel-jacobi-method-algorithm-flowchart/
• https://www.geeksforgeeks.org/doolittle-algorithm-lu-
decomposition/#:~:text=Doolittle%27s%20method%20provides%20an%20alternative,th
e%20hassle%20of%20Gaussian%20Elimination.&text=We%20then%20systematically%2
0solve%20for,multiplications%20necessary%20for%20A%3DLU
• https://algowiki-
project.org/en/Cholesky_decomposition#:~:text=The%20Cholesky%20decomposition%2
0algorithm%20was,he%20was%20killed%20in%20battle.&text=Originally%2C%20the%2
0Cholesky%20decomposition%20was,real%20symmetric%20positive%20definite%20ma
trices
• http://www.mymathlib.com/matrices/linearsystems/crout.html

You might also like