ヤコビ反復、ガウスザイデル反復およびSOR反復のC ++実装について



About C Implementation Jacobi Iteration



記事のディレクトリ


ヤコビの他の2つのアルゴリズムを理解している限り、アルゴリズムは難しくありません。これらはすべてヤコビ反復に基づいて構築されているため、非常に単純です。
まず、主に入力関数と関数実現ブロックの反復呼び出しで構成されるプログラムについて説明します。 void SOR() { int w cout << 'Please enter the relaxation factor w:' << endl cin >> w while (times++ < max_times ) { // Vector multiplication according to the initial vector. Find the solution corresponding to each row for ( int i = 0 i < m i++ ) { double sum = 0 for ( int j = 0 j < m j++ ) { if ( j != i ) { sum += init[j] * mtrx[i][j] } } init[i] =init[i]+ w *( ( b[i] - sum ) / mtrx[i][i] -init[i]) } output() } }

上記は注意分析であり、以下はソースコードです

#include #include #define sz 10 using namespace std /***************************** @Jacobi iterative solution of linear equations @author:jawi @date:2020/03/03 @tip: The default input matrix of this program is an ideal matrix @The maximum error function in the program is not implemented *****************************/ /***************************************************** 3 10 -1 -2 -1 10 -2 -1 -1 5 7.2 8.3 4.2 0 0 0 20 0.01 ***********************************************/ void input() void Jacobi() void GS() void SOR() void func() void output() int m //m*n coefficient matrix int max_times// z maximum number of iterations int times = 0//Current iteration number //int mx_mis = 99999999 string choice//Function selection double mis//tolerance scope double mtrx[sz][sz]//Coefficient matrix double b[sz]//Constant matrix double init[sz]//Initial vector double ans[sz]//answer //double max_mis[sz]// int main() { input() func() return 0 } void func() { if(choice=='J') Jacobi() else if(choice=='GS') GS() else SOR() } void input() { cout<<'Please enter the iteration method (J, GS, SOR)'<<endl cin>>choice cout << 'Please enter m' << endl cin >> m cout << 'Please enter the coefficient matrix m*n(m)' << endl for ( int i = 0 i < m i++ ) { for ( int j = 0 j < m j++ ) { cin >> mtrx[i][j] } } cout << 'Please enter the constant matrix b:' << endl for ( int i = 0 i < m i++ ) { cin >> b[i] } cout << 'Please enter the initial vector' << endl for ( int i = 0 i < m i++ ) { cin >> init[i] ans[i] = init[i] } cout << 'Please enter the maximum number of iterations:' << endl cin >> max_times // cout << endl << 'Please enter the maximum error:'< // cin>>mis } void Jacobi() { while ( times++ < max_times ) { // Vector multiplication based on the initial vector. Find the solution corresponding to each row //Put the result in ans for ( int i = 0 i < m i++ ) { double sum = 0 for ( int j = 0 j < m j++ ) { if ( j != i ) { sum += init[j] * mtrx[i][j] } } ans[i] = ( b[i] - sum ) / mtrx[i][i] // max_mis[i] = fabs ( ( ans[i] - init[i] ) / init[i] ) } //After the iteration is completed once, update the init array once for the next iteration for ( int i = 0 i < m i++ ) { init[i] = ans[i] } // mx_mis=max_mis[0] // for(int i=0i // { // if(max_mis[i]>mx_mis) // mx_mis= max_mis[i] // } output() } } void GS() { while ( times++ < max_times ) { // Vector multiplication based on the initial vector. Find the solution corresponding to each row for ( int i = 0 i < m i++ ) { double sum = 0 for ( int j = 0 j < m j++ ) { if ( j != i ) { sum += init[j] * mtrx[i][j] } } init[i] = ( b[i] - sum ) / mtrx[i][i] } output() } } void SOR() { int w cout << 'Please enter the relaxation factor w:' << endl cin >> w while (times++ < max_times ) { // Vector multiplication according to the initial vector. Find the solution corresponding to each row for ( int i = 0 i < m i++ ) { double sum = 0 for ( int j = 0 j < m j++ ) { if ( j != i ) { sum += init[j] * mtrx[i][j] } } init[i] =init[i]+ w *( ( b[i] - sum ) / mtrx[i][i] -init[i]) } output() } } void output() { cout << 'Iteration' << times << 'Times, the answer is' << endl for ( int i = 0 i < m i++ ) { // cout << init[i] << endl printf ( '%.4f ', init[i] ) } printf ( ' ' ) }