Jaccobi_solution.cpp
上传用户:jinqiu1010
上传日期:2021-09-06
资源大小:3082k
文件大小:1k
开发平台:

C/C++

  1. // Jaccobi_solution.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. //#include"P_Q_solution.cpp"
  5. #define n 2
  6. int main(int argc, char* argv[])
  7. {
  8. int i,j;
  9.     float P[n]={1,1.5};
  10. float Q[n]={1,2};
  11. float e[n]={1,1};
  12.     float f[n]={0};
  13. float G[n][n]={{2,2},{1.5,20}},B[n][n]={{0.2,2.5},{0.15,2}};
  14. float Jac[2*n][2*n];
  15. //void P_Q(P[n],Q[n],e[n],f[n],G[n],B[n]);
  16. for(i=0;i<n;i++)
  17. {
  18. for(j=0;j<n;j++)
  19. {
  20. if(j!=i)
  21. {
  22. float a=0.0,b=0.0;
  23. a=(G[i][j]*e[i]+B[i][j]*f[i]);
  24. b=(B[i][j]*e[i]-G[i][j]*f[i]);
  25. Jac[2*i][2*j]=0.0-a;
  26. Jac[2*i][2*j+1]=b;
  27. Jac[2*i+1][2*j]=b;
  28. Jac[2*i][2*j+1]=a;
  29. }
  30. else
  31. {
  32. float sum1=0.0;
  33. float sum2=0.0;
  34. int k;
  35. for(k=0;k<n;k++)
  36. {
  37. sum1=sum1+(G[i][k]*e[k]-B[i][k]*f[k]);
  38. sum2=sum2+(B[i][k]*e[k]+G[i][k]*f[k]);
  39. }
  40.                    Jac[2*i][2*j]=0.0-sum1-G[i][i]*e[i]-B[i][i]*f[i];
  41.    Jac[2*i][2*j+1]=0.0-sum2+B[i][i]*e[i]-G[i][i]*f[i];
  42.     Jac[2*i+1][2*j]=sum2+B[i][i]*e[i]-G[i][i]*f[i];
  43.    Jac[2*i+1][2*j+1]=0.0-sum1+G[i][i]*e[i]+B[i][i]*f[i];
  44. }
  45. }
  46. }
  47.               printf("nJaccobi矩阵是:n");
  48.              for(i=0;i<2*n;i++)
  49.  {
  50.               for(j=0;j<2*n;j++)
  51.   {
  52.              printf("%f",Jac[i][j]);
  53.             printf("  ");
  54.   }
  55.                 printf("n");
  56.  }
  57. return 0;
  58. }