?? gauss-seidel.c
字號(hào):
/*
*C語言實(shí)現(xiàn)Gauss-Seidel迭代法解方程組;
*實(shí)現(xiàn)特定方程組的求解并比較他們的收斂的快慢,E=10e-6;
*author:黃翔 date:2008-6-5
*/
#include <math.h>
#include <stdio.h>
#define eps 0.000001
#define true 1
int main()
{
double A[10][10]={{4,-1,0,0,0,0,0,0,0,0}, //系數(shù)矩陣
{-1,4,-1,0,0,0,0,0,0,0},
{0,-1,4,-1,0,0,0,0,0,0},
{0,0,-1,4,-1,0,0,0,0,0},
{0,0,0,-1,4,-1,0,0,0,0},
{0,0,0,0,-1,4,-1,0,0,0},
{0,0,0,0,0,-1,4,-1,0,0},
{0,0,0,0,0,0,-1,4,-1,0},
{0,0,0,0,0,0,0,-1,4,-1},
{0,0,0,0,0,0,0,0,-1,4,}};
double B[10]={4.0,11.0,15.0,18.0,21.0,24.0,27.0,30.0,37.0,45.5};//常數(shù)項(xiàng)
double x[10]={0}; //初始解
int i,j; //用于循環(huán)條件數(shù)組下標(biāo)
int count=0; //迭代次數(shù)計(jì)數(shù)器
double M,N,G; //迭代系數(shù)
double t;
double E[10], e; //范數(shù)數(shù)組,用作停機(jī)條件判斷;
//Gauss-Seidel迭代
while(true){
for(i=0;i<10;i++){
for(j=0,M=0;j<=i-1;j++){
M-=A[i][j]*x[j];
}
for(j=i+1,N=0;j<10;j++){
N-=A[i][j]*x[j];
}
G=1/A[i][i];
t=x[i];
x[i]=G*(M+N+B[i]);
E[i]=fabs(t-x[i]);
}
for(i=0,e=E[0];i<10;i++){//
if(e<E[i]){
e=E[i];
}
}
count++;
if(e<eps) break; //
}
printf("迭代次數(shù)\t%d\n",count);
printf("求得方程組的解為:\n");
for(i=0;i<10;i++)
printf("%.10f\n",x[i]);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -