-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment2_14ME130_Isac.cc
239 lines (226 loc) · 5.87 KB
/
Assignment2_14ME130_Isac.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
Assignment 2 - Doo-Little & Cholesky Decomposition Method
Name: Isac Rajan
Roll No: 14ME130
Course: Applied Computational Methods in Mechanical Sciences
*/
#include <iostream>
#include <vector>
#include <ctime>
#include <cmath>
#include <fstream>
using std::vector;
using std::cout;
using std::cin;
using std::endl;
using std::ofstream;
typedef double mydoub;
typedef vector<double> vdouble1;
typedef vector<vector<double> > vdouble2;
int main()
{
clock_t start;
start = clock(); //Variable to measure CPU time
ofstream result("Assignment2_result.txt");
result << "Results: DOO-LITTLE DECOMPOSITION METHOD" << endl;
/* DOO-LITTLE DECOMPOSITION METHOD */
//Declaration & Initialization of vector A in Ax=B, L, U
vdouble2 A, L, U;
A.resize(3);
L.resize(3);
U.resize(3);
for(int i=0; i<3; i++)
{
A[i].resize(3);
L[i].resize(3);
U[i].resize(3);
}
A[0][0] = 1; A[0][1] = 4; A[0][2] = 1;
A[1][0] = 1; A[1][1] = 6; A[1][2] = -1;
A[2][0] = 2; A[2][1] = -1; A[2][2] = 2;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
{
if(i == j)
L[i][j] = 1.0; //Diagonal Elements
if(i < j)
L[i][j] = 0.0; //Upper Triangular Matrix
if(i > j)
U[i][j] = 0.0; //Lower Triangular Matrix
}
//Declaration & Initialization of vector x, z, B
vdouble1 x(3, 0), z(3, 0), B(3);
B[0] = 7;
B[1] = 13;
B[2] = 5;
result << "Contents of Matrix A:\n";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
result << A[i][j] << "\t";
result << endl;
}
result << "Contents of Matrix B:" << endl;
for(int i=0; i<3; i++)
result << B[i] << endl;
//1. Getting L and U matrix
for(int k=0; k<3; k++)
{
for(int m=k; m<3; m++)
{
mydoub sum;
sum = 0.0;
for(int j=0; j<=(k-1); j++)
sum += L[k][j]*U[j][m];
U[k][m] = A[k][m] - sum;
}
for(int i=k+1; i<3; i++)
{
mydoub sum;
sum = 0.0;
for(int j=0; j<=(k-1); j++)
sum += L[i][j]*U[j][k];
L[i][k] = (A[i][k] - sum)/U[k][k];
L[i][i] = 1;
}
}
result << "Contents of Matrix U:\n";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
result << U[i][j] << "\t";
result << endl;
}
result << "Contents of Matrix L:\n";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
result << L[i][j] << "\t";
result << endl;
}
//3. Solve LZ=B to get Z
for(int i=0; i<3; i++)
{
mydoub sum;
sum = 0.0;
for(int j=0; j<=(i-1); j++)
sum += L[i][j]*z[j];
z[i] = B[i] - sum;
}
result << "Contents of Matrix Z:" << endl;
for(int i=0; i<3; i++)
result << z[i] << endl;
//4. Solve UX=B to get X
x[2] = z[2]/U[2][2];
for(int i=1; i>=0; i--)
{
mydoub sum;
sum = 0.0;
for(int j=i+1; j<3; j++)
sum += U[i][j]*x[j];
x[i] = (z[i]-sum)/U[i][i];
}
result << "After solving, the values of x1, x2, and x3 are: " << endl;
for(int i=0; i<3; i++)
result << "x" << i+1 << " = " << x[i] << endl;
result << "CPU time: " << (double)((-start + clock())/CLOCKS_PER_SEC) << " seconds" << endl;
result << "\nVerification: Substituting results into original equation:\nError is:" << endl;
for(int i=0; i<3; i++)
result << "\tError in Equation " << i+1 << " is " << B[i] - (A[i][0]*x[0]+A[i][1]*x[1]+A[i][2]*x[2]) << endl;
/* --------------------------------------------------- */
/* CHOLESKY DECOMPOSITION METHOD */
result << "\n\n---------------------------------------" << endl;
result << "Results: CHOLESKY DECOMPOSITION METHOD" << endl;
start = clock();
//Initialization of Vector A
A[0][0] = 10; A[0][1] = -1; A[0][2] = 2;
A[1][0] = -1; A[1][1] = 12; A[1][2] = -1;
A[2][0] = 2; A[2][1] = -1; A[2][2] = 20;
//Initialization of vector B
B[0] = 11;
B[1] = 10;
B[2] = 21;
result << "Contents of Matrix A:\n";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
result << A[i][j] << "\t";
result << endl;
}
result << "Contents of Matrix B:" << endl;
for(int i=0; i<3; i++)
result << B[i] << endl;
//Checking for symmetry in Matrix A
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
if(i!=j)
if(A[i][j] != A[j][i])
{
result << "\nMatrix A is not symmetric" << endl;
return -1;
}
result << "\nCheck Result: Matrix A is symmetric" << endl;
//Checking positive definitivity of Matrix A
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
if(i==j)
if(A[i][i] <= 0)
{
result << "\nMatrix A in not Positive Definite" << endl;
return -1;
}
result << "Check Result: Matrix A is Positive Definite\n" << endl;
//1. Get U Matrix and L=U^T
for(int i=0; i<3; i++)
{
mydoub sum;
sum = 0.0;
for(int k=0; k<=i-1; k++)
sum += U[k][i]*U[k][i];
U[i][i] = sqrt(A[i][i] - sum);
for(int j=i+1; j<3; j++)
{
sum = 0.0;
for(int k=0; k<=i-1; k++)
sum += U[k][i]*U[k][j];
U[i][j] = (A[i][j] - sum)/U[i][i];
}
}
result << "Contents of Matrix U:\n";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
result << U[i][j] << "\t";
result << endl;
}
//2. Solving U^TZ = B
for(int i=0; i<3; i++)
{
mydoub sum;
sum = 0.0;
for(int j=0; j<=i-1; j++)
sum += U[j][i]*z[j];
z[i]=(B[i]-sum)/U[i][i];
}
result << "Contents of Matrix Z:" << endl;
for(int i=0; i<3; i++)
result << z[i] << endl;
//3. Solving UX=Z
for(int i=2; i>=0; i--)
{
mydoub sum;
sum = 0.0;
for(int j=i+1; j<3; j++)
sum += U[i][j]*x[j];
x[i]=(z[i]-sum)/U[i][i];
}
result << "After solving, the values of x1, x2, and x3 are: " << endl;
for(int i=0; i<3; i++)
result << "x" << i+1 << " = " << x[i] << endl;
result << "CPU time: " << (double)((-start + clock())/CLOCKS_PER_SEC) << " seconds" << endl;
result << "\nVerification: Substituting results into original equation:\nError is:" << endl;
for(int i=0; i<3; i++)
result << "\tError in Equation " << i+1 << " is " << B[i] - (A[i][0]*x[0]+A[i][1]*x[1]+A[i][2]*x[2]) << endl;
result.close();
return 0;
}