Path: blob/master/src/C/SOR.c
1700 views
#include "SOR.h"1234double dist_square(double* a, double* b)5{6double dist = 0;78for (int i=0; i<N; ++i)9{10dist += (a[i] - b[i]) * (a[i] - b[i]);11}12return dist;13}141516double dist_square2(double* a, double* b, int NN)17{18double dist = 0;1920for (int i=0; i<NN; ++i)21{22dist += (a[i] - b[i]) * (a[i] - b[i]);23}24return dist;25}26272829double* SOR(double A[N][N], double* b, double w, double eps, int N_max)30{31double* x0 = (double*)calloc(N, sizeof(double) );32double* x1 = (double*)calloc(N, sizeof(double) );33double S;3435for (int k=0; k<N_max+1; ++k)36{37for (int i=0; i<N; ++i)38{39S = 0;40for (int j=0; j<N; ++j)41{42if (j!=i)43S += A[i][j] * x1[j];44}45x1[i] = (1-w)*x1[i] + (w / A[i][i]) * (b[i] - S);46}47if (dist_square(x0,x1) < eps*eps)48return x1;4950for (int i=0; i<N; ++i)51x0[i] = x1[i];52}53printf("Fail to converge in %d iterations", N_max);54return x1;55}565758double* SOR_trid(double A[N][N], double* b, double w, double eps, int N_max)59{60double* x0 = (double*)calloc(N, sizeof(double) );61double* x1 = (double*)calloc(N, sizeof(double) );62double S;6364for (int k=0; k<N_max+1; ++k)65{66for (int i=0; i<N; ++i)67{68if (i==0)69S = A[0][1] * x1[1];70else if (i==N-1)71S = A[N-1][N-2] * x1[N-2];72else73S = A[i][i-1] * x1[i-1] + A[i][i+1] * x1[i+1];7475x1[i] = (1-w)*x1[i] + (w / A[i][i]) * (b[i] - S);76}77if (dist_square(x0,x1) < eps*eps)78return x1;7980for (int i=0; i<N; ++i)81x0[i] = x1[i];82}83printf("Fail to converge in %d iterations", N_max);84return x1;85}868788double* SOR_abc(double aa, double bb, double cc, double* b, int NN, double w, double eps, int N_max)89{90double* x0 = (double*)calloc(NN, sizeof(double) );91double* x1 = (double*)calloc(NN, sizeof(double) );92double S;9394for (int k=0; k<N_max+1; ++k)95{96for (int i=0; i<NN; ++i)97{98if (i==0)99S = cc * x1[1];100else if (i==NN-1)101S = aa * x1[NN-2];102else103S = aa * x1[i-1] + cc * x1[i+1];104105x1[i] = (1-w)*x1[i] + (w / bb) * (b[i] - S);106}107if (dist_square2(x0,x1,NN) < eps*eps)108{109free(x0);110return x1;111}112113for (int i=0; i<NN; ++i)114x0[i] = x1[i];115}116printf("Fail to converge in %d iterations", N_max);117free(x0);118return x1;119}120121122double* SOR_aabbcc(double aa, double bb, double cc, double *b, double *x0, double *x1,123int NN, double w, double eps, int N_max)124{125/* aa, bb, cc are the matrix coefficients,126b is the vector in Ax=b,127x0 is helpful to store values,128x1 is the initial guess and the final solution,129NN is the dimension of b,x0,x1,130w, eps, N_max are the SOR parameters. */131132double S;133134for (int k=0; k<N_max+1; ++k)135{136for (int i=0; i<NN; ++i)137{138if (i==0)139S = cc * x1[1];140else if (i==NN-1)141S = aa * x1[NN-2];142else143S = aa * x1[i-1] + cc * x1[i+1];144145x1[i] = (1-w)*x1[i] + (w / bb) * (b[i] - S);146}147if (dist_square2(x0,x1,NN) < eps*eps)148{149return x1;150}151152for (int i=0; i<NN; ++i)153x0[i] = x1[i];154}155printf("Fail to converge in %d iterations", N_max);156return x1;157}158159160161void print_matrix(double arr[N][N])162{163164for (int i=0; i<N; ++i)165{166for (int j=0; j<N; ++j)167printf("%f\t", arr[i][j] );168printf("\n");169}170}171172173void print_array(double* arr)174{175for (int i=0; i<N; ++i)176printf("%f\t", arr[i] );177printf("\n");178}179180181void print_array_2(double* arr, int NN)182{183for (int i=0; i<NN; ++i)184printf("%f\t", arr[i] );185printf("\n");186}187188189