[数值算法]线性方程组的求解---迭代法小结.
by EmilMatthew 05/9/08
当迭代法做多了之后,便感受到这种思想的好用了,今天再次实现用迭代法求解问题,感觉轻松了不少。
当某个关于x的迭代式xk=fi(xk_1)保持收敛时,则可利迭代来求出最后的结果,直到满足精度要求为止.
对于线性方程组的求解中迭代法的应用亦是如此,当矩阵的规模大于100*100时,迭代法的优势就突现出来了.
1) Jacobi
对于线性方程组,可以很方便的对第i行中的元素xi用该行中的其余x来表示,
即:
Xi_k+1=1/aii*(bi-sum(aij*Xj_k&&j1=i))
在数学上可以证明它满足迭代条件,所以,这便有了第一个求线性方程组的迭代算法:
Jacobi算法
/*
JacobiMethod, coded by EmilMathew 05/9/8, you can modify and use these code as you wish , but there is no guarantee that it can fit all you need.
*/
/*main aid function,to determine the end of the iterator*/
int preciseReached(Type* xList1,Type* xList2,int len,Type precise)
{
int i=0;
int flag;
assertF(xList1!=NULL,"in preciseReached,xList1 is NULL/n");
assertF(xList2!=NULL,"in preciseReached,xList2 is NULL/n");
flag=1;
for(i=0;i<len;i++)
if(fabs(xList1[i]-xList2[i])>precise)
{
flag=0;
break;
}
return flag;
}
void JacobiMethod(Type** inMatrixArr,Type* bList,Type* xAnsList,Type precise,int size,
FILE* outputFile)
{
int iteratorNum;
int i=0,j=0;
Type sum=0;
Type* tmpXList;
/*pointer data assertion*/
assertF(inMatrixArr!=NULL,"in JacobiMethod,matrixArr is NULL/n");
assertF(bList!=NULL,"in JacobiMethod,bList is NULL/n");
assertF(xAnsList!=NULL,"in JacobiMethod,xAnsList is NULL/n");
tmpXList=(Type*)malloc(sizeof(Type)*size);
assertF(tmpXList!=NULL,"in JacobiMethod,tmpXList is NULL/n");