文档详情

雅可比,高斯-赛德尔以及SOR迭代法.doc

发布:2017-06-04约2.06千字共4页下载文档
文本预览下载声明
昨天闲来无事,就把雅可比,高斯-赛德尔和SOR迭代法解方程组的程序写了. 把主要程序发了,可能有谁用的着. /**********雅可比迭代法**********/ void Jacobi float xk[] int i,j; ?float t 0.0; ?float tt 0.0; ?float *xl; ?xl float * malloc sizeof float * n+1 ; ?for i 1;i n+1;i++ float t 0.0; ??for j 1;j n+1;j++ if j i continue; ???t t+a[i][j]*xk[j]; xl[i] b[i]-t /a[i][i]; t 0.0; ?for i 1;i n+1;i++ tt fabs xl[i]-xk[i] ; ???tt tt*tt; ???t+ tt; t sqrt t ; ?for i 1;i n+1;i++ ??xk[i] xl[i]; ? ?if k+1 c if t p printf \nReach the given precision!\n ; ??else printf \nover the maximal count!\n ; ??printf \nCount number is %d\n,k ; else ??if t p k++; ???Jacobi xk ; else printf \nReach the given precision!\n ; ???printf \nCount number is %d\n,k ; /**********高斯-赛德尔迭代法**********/ void Gauss_Siedel float xk[] int i,j; ?float t 0.0; ?float tt 0.0; ?float *xl; ?xl float * malloc sizeof float * n+1 ; ?for i 1;i n+1;i++ t 0.0; ??tt 0.0; ??for j 1;j i;j++ t t+a[i][j]*xl[j]; ??for j i+1;j n+1;j++ tt tt+a[i][j]*xk[j]; ??xl[i] b[i]-t-tt /a[i][i]; t 0.0; ?for i 1;i n+1;i++ tt fabs xl[i]-xk[i] ; ???tt tt*tt; ???t+ tt; t sqrt t ; ?for i 1;i n+1;i++ ??xk[i] xl[i]; ? ?if k+1 c if t p printf \nReach the given precision!\n ; ??else printf \nover the maximal count!\n ; ??printf \nCount number is %d\n,k ; else ??if t p k++; ???Gauss_Siedel xk ; else printf \nReach the given precision!\n ; ???printf \nCount number is %d\n,k ; /**********SOR迭代法**********/ void SOR float xk[] int i,j; ?float t 0.0; ?float tt 0.0; ?float *xl; ?xl float * malloc sizeof float * n+1 ; ?for i 1;i n+1;i++ t 0.0; ??tt 0.0; ??for j 1;j i;j++ t t+a[i][j]*xl[j]; ??for j i;j n+1;j++ tt tt+a[i][j]*xk[j]; ??xl[i] xk[i]+w* b[i]-t-tt /a[i][i]; t 0.0; ?for i 1;i n+1;i++ tt fabs xl[i]-xk[i] ; ???tt tt*tt; ???t+ tt; t sqrt t ; ?for i 1;i n+1;i++ ??xk[i] xl[i]; ? ?if k+1 c if t p printf \nReach the given precision!\n ; ??else printf \nover the maximal count!\n ; ??printf \nCount number is %d\n,k ; else ??if t p k++; ???SOR xk ; else printf \nReach the given precision!\n ; ???printf \
显示全部
相似文档