Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
cantaro86
GitHub Repository: cantaro86/Financial-Models-Numerical-Methods
Path: blob/master/src/C/SOR.c
1700 views
1
#include "SOR.h"
2
3
4
5
double dist_square(double* a, double* b)
6
{
7
double dist = 0;
8
9
for (int i=0; i<N; ++i)
10
{
11
dist += (a[i] - b[i]) * (a[i] - b[i]);
12
}
13
return dist;
14
}
15
16
17
double dist_square2(double* a, double* b, int NN)
18
{
19
double dist = 0;
20
21
for (int i=0; i<NN; ++i)
22
{
23
dist += (a[i] - b[i]) * (a[i] - b[i]);
24
}
25
return dist;
26
}
27
28
29
30
double* SOR(double A[N][N], double* b, double w, double eps, int N_max)
31
{
32
double* x0 = (double*)calloc(N, sizeof(double) );
33
double* x1 = (double*)calloc(N, sizeof(double) );
34
double S;
35
36
for (int k=0; k<N_max+1; ++k)
37
{
38
for (int i=0; i<N; ++i)
39
{
40
S = 0;
41
for (int j=0; j<N; ++j)
42
{
43
if (j!=i)
44
S += A[i][j] * x1[j];
45
}
46
x1[i] = (1-w)*x1[i] + (w / A[i][i]) * (b[i] - S);
47
}
48
if (dist_square(x0,x1) < eps*eps)
49
return x1;
50
51
for (int i=0; i<N; ++i)
52
x0[i] = x1[i];
53
}
54
printf("Fail to converge in %d iterations", N_max);
55
return x1;
56
}
57
58
59
double* SOR_trid(double A[N][N], double* b, double w, double eps, int N_max)
60
{
61
double* x0 = (double*)calloc(N, sizeof(double) );
62
double* x1 = (double*)calloc(N, sizeof(double) );
63
double S;
64
65
for (int k=0; k<N_max+1; ++k)
66
{
67
for (int i=0; i<N; ++i)
68
{
69
if (i==0)
70
S = A[0][1] * x1[1];
71
else if (i==N-1)
72
S = A[N-1][N-2] * x1[N-2];
73
else
74
S = A[i][i-1] * x1[i-1] + A[i][i+1] * x1[i+1];
75
76
x1[i] = (1-w)*x1[i] + (w / A[i][i]) * (b[i] - S);
77
}
78
if (dist_square(x0,x1) < eps*eps)
79
return x1;
80
81
for (int i=0; i<N; ++i)
82
x0[i] = x1[i];
83
}
84
printf("Fail to converge in %d iterations", N_max);
85
return x1;
86
}
87
88
89
double* SOR_abc(double aa, double bb, double cc, double* b, int NN, double w, double eps, int N_max)
90
{
91
double* x0 = (double*)calloc(NN, sizeof(double) );
92
double* x1 = (double*)calloc(NN, sizeof(double) );
93
double S;
94
95
for (int k=0; k<N_max+1; ++k)
96
{
97
for (int i=0; i<NN; ++i)
98
{
99
if (i==0)
100
S = cc * x1[1];
101
else if (i==NN-1)
102
S = aa * x1[NN-2];
103
else
104
S = aa * x1[i-1] + cc * x1[i+1];
105
106
x1[i] = (1-w)*x1[i] + (w / bb) * (b[i] - S);
107
}
108
if (dist_square2(x0,x1,NN) < eps*eps)
109
{
110
free(x0);
111
return x1;
112
}
113
114
for (int i=0; i<NN; ++i)
115
x0[i] = x1[i];
116
}
117
printf("Fail to converge in %d iterations", N_max);
118
free(x0);
119
return x1;
120
}
121
122
123
double* SOR_aabbcc(double aa, double bb, double cc, double *b, double *x0, double *x1,
124
int NN, double w, double eps, int N_max)
125
{
126
/* aa, bb, cc are the matrix coefficients,
127
b is the vector in Ax=b,
128
x0 is helpful to store values,
129
x1 is the initial guess and the final solution,
130
NN is the dimension of b,x0,x1,
131
w, eps, N_max are the SOR parameters. */
132
133
double S;
134
135
for (int k=0; k<N_max+1; ++k)
136
{
137
for (int i=0; i<NN; ++i)
138
{
139
if (i==0)
140
S = cc * x1[1];
141
else if (i==NN-1)
142
S = aa * x1[NN-2];
143
else
144
S = aa * x1[i-1] + cc * x1[i+1];
145
146
x1[i] = (1-w)*x1[i] + (w / bb) * (b[i] - S);
147
}
148
if (dist_square2(x0,x1,NN) < eps*eps)
149
{
150
return x1;
151
}
152
153
for (int i=0; i<NN; ++i)
154
x0[i] = x1[i];
155
}
156
printf("Fail to converge in %d iterations", N_max);
157
return x1;
158
}
159
160
161
162
void print_matrix(double arr[N][N])
163
{
164
165
for (int i=0; i<N; ++i)
166
{
167
for (int j=0; j<N; ++j)
168
printf("%f\t", arr[i][j] );
169
printf("\n");
170
}
171
}
172
173
174
void print_array(double* arr)
175
{
176
for (int i=0; i<N; ++i)
177
printf("%f\t", arr[i] );
178
printf("\n");
179
}
180
181
182
void print_array_2(double* arr, int NN)
183
{
184
for (int i=0; i<NN; ++i)
185
printf("%f\t", arr[i] );
186
printf("\n");
187
}
188
189