资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
求解线性方程组求解线性方程组超松弛迭代法超松弛迭代法#include #include using namespace std;float *one_array_malloc(int n); /一维数组分配float *two_array_malloc(int m,int n); /二维数组分配float matrix_category(float* x,int n);int main()const int MAX=100;/最大迭代次数int n,i,j,k;float* a;float* x_0; /初始向量float* x_k; /迭代向量float precision; /精度float w; /松弛因子coutprecision;coutn;a=two_array_malloc(n,n+1);coutaij;x_0=one_array_malloc(n);coutx_0i;x_k=one_array_malloc(n);coutw;float temp;/迭代过程for(k=0;kMAX;k+)for(i=0;in;i+)temp=0;for(j=0;ji;j+)temp=temp+aij*x_kj;x_ki=ain-temp;temp=0;for(j=i+1;jn;j+)temp=temp+aij*x_0j;x_ki=(x_ki-temp)/aii;x_ki=(1-w)*x_0i+w*x_ki;/求两解向量的差的范数for(i=0;in;i+)x_0i=x_ki-x_0i;if(matrix_category(x_0,n)precision)break;elsefor(i=0;in;i+)x_0i=x_ki;/输出过程if(MAX=k)cout“迭代不收敛n“;cout“迭代次数为:“kendl;cout“解向量为:n“;for(i=0;in;i+)cout“x“i“: “x_kiendl;return 0;float *one_array_malloc(int n) /一维数组分配float *a;a=(float *)malloc(sizeof(float)*n);return a;float *two_array_malloc(int m,int n) /二维数组分配float *a;int i;a=(float *)malloc(m*sizeof(float *);for (i=0;im;i+)ai=(float *)malloc(n*sizeof(float);return a;float matrix_category(float* x,int n)int i;float temp=0;for(i=0;in;i+)temp=temp+fabs(xi);return temp;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号