Three iterative solutions of equations (Jacobian Jacobi, Gaussian-gaisi_saideer, successive hyper-relaxation Sor)

Source: Internet
Author: User

Label:

The following iterative methods are used to solve the equations of linear systems

4-1 0-1 0) 0 0

-1 4-1 0-1 0 5

0-1 4-1 0-1-2

-1 0-1 4-1 0 5

0-1 0-1 4-1-2

0 0-1 0-1) 4 6

The convergence of the, and for the Mission | | xk+1-xk| | The approximate solution of 2<=0.0001 and the corresponding number of iterations.

(1) Jacobian iterative method;

(2) Gauss-Delphi iterative method;

(3) Sor iterative method (W in turn take 1.334,1.95,0.95)

Numerical calculation method of the operation, to the three iterations of the implementation of programming, so I followed the book on the formula knocked a bit, the function is basically realized.

Test data:

Input

6
4-1 0-1 0 0
-1 4-1 0-1 0
0-1 4-1 0-1
-1 0-1 4-1 0
0-1 0-1 4-1
0 0-1 0-1 4

0 5-2 5-2 6

The initial value of x is arbitrarily assigned.


#include <iostream> #include <stdio.h> #include <math.h> #include <stdlib.h> #include < string> #include <string.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <stack> #include <iomanip>using namespace std;typedef long long LL; const int Inf=0x7fffffff;const int MAXN=1000;CONST double wucha=0.0001;const int max_count=500;double A[MAXN][MAXN];    Double B[MAXN];d ouble CURX[MAXN];d ouble lastx[maxn];int n,count;double w;void init () {printf ("Enter unknown Number:");    cin>>n;    printf ("Input equation set matrix a:\n");        for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {cin>>a[i][j];    }} printf ("Input b vector: \ n");    for (int i=1;i<=n;i++) {cin>>b[i]; } Cout<<endl;}    void Initx () {//Memset (curx,-1,sizeof (CurX));    cout<< "Please assign an initial value to x:" <<endl;    for (int i=1;i<=n;i++) {cin>>curx[i];  }}bool judge () {double sum=0;  for (int i=1;i<=n;i++) {sum+= (Curx[i]-lastx[i]) * (Curx[i]-lastx[i]);    } sum=sqrt (sum);    if (Sum<=wucha) return 1; else return 0;}    void Shuchux () {cout<< "Iteration count:" <<Count<<endl;    cout<< "Approximate solution for:";    for (int i=1;i<=n;i++) {cout<<curx[i]<< ""; } Cout<<endl<<endl;}    void Jacobi () {//x Initial value is set to 0 cout<< "Jacobian iterative method:" <<endl;    INITX ();    int flag=0;    Double sum;        for (int. k=1;k<max_count;k++) {for (int i=1;i<=n;i++) {lastx[i]=curx[i];            } for (int i=1;i<=n;i++) {sum=0;                for (int j=1;j<=n;j++) {if (i==j) continue;            SUM+=A[I][J]*LASTX[J];        } curx[i]= (B[i]-sum)/a[i][i];            } if (judge ()) {flag=1;            Count=k;            Shuchux ();        Break }} if (flag==0) cout<< "error! "<<endl<<endl;} void Gaisi_saideer () {cout<< "Gauss-Delphi Iteration Method:"<<endl;    INITX ();    int flag=0;    Double sum;    Double T;        for (int k=1;k<max_count;k++) {double e=0;            for (int i=1;i<=n;i++) {t=curx[i];            sum=0;                for (int j=1;j<=n;j++) {if (i==j) continue;            SUM+=A[I][J]*CURX[J];            } curx[i]= (B[i]-sum)/a[i][i];            if (Fabs (curx[i]-t) <=e) {} else {e=fabs (curx[i]-t);            }} if (E<wucha) {flag=1;            Count=k;            Shuchux ();        Break }} if (flag==0) cout<< "error! "<<endl<<endl;}    void SOR () {cout<< "Successive hyper-relaxation iterative method (SOR):" <<endl;    INITX ();    cout<< "Input Relaxation factor W:" <<endl;    cin>>w;    int flag=0;    Double sum1,sum2;            for (int. k=1;k<max_count;k++) {for (int i=1;i<=n;i++) {lastx[i]=curx[i];        if (k==1) curx[i]=0; } for (int i=1;i<=n;i++) {sum1=0,sum2=0;            for (int j=1;j<=i-1;j++) {sum1+=a[i][j]*curx[j];//cout<<curx[j]<< "";            }//cout<<endl;            for (int j=i;j<=n;j++) {sum2+=a[i][j]*lastx[j];        } curx[i]= (B[I]-SUM1-SUM2) *w/a[i][i]+lastx[i];            } if (judge ()) {flag=1;            Count=k;            Shuchux ();        Break }} if (flag==0) cout<< "error! "<<endl<<endl;}        int main () {while (1) {init ();        Jacobi ();        Gaisi_saideer ();        SOR ();        SOR ();    SOR (); } return 0;}


Three iterative solutions of equations (Jacobian Jacobi, Gaussian-gaisi_saideer, successive hyper-relaxation Sor)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: