fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define N 8
  4.  
  5. void seiki(double x[N], double y[N], double A[2][N], double b[N], double S[2][3]);
  6.  
  7. void seiki(double x[N], double y[N], double A[2][N], double b[N], double S[2][3]){
  8. for(int i=0;i<N;i++){
  9. A[0][i] = pow(x[i], 2);
  10. A[1][i] = pow(x[i], 4);
  11. b[i] = y[i] -1;
  12. }
  13.  
  14. for(int n=0;n<2;n++){
  15. for(int l=0;l<2;l++){
  16. for(int m=0;m<N;m++){
  17. S[n][l] += A[n][m]*A[l][m];
  18. }
  19. }
  20. }
  21.  
  22. for(int i=0;i<2;i++){
  23. for(int j=0;j<N;j++){
  24. S[i][2] += A[i][j]*b[j];
  25. }
  26. }
  27. }
  28.  
  29. void pib(double S[2][3]){
  30. for(int j=0;j<2;j++){
  31. double tan = S[j][j];
  32. for(int i=j;i<3;i++){
  33. S[j][i]/=tan;
  34. }
  35. for(int a=1+j;a<2;a++){
  36. double tan = S[a][j];
  37. for(int b=j;b<3;b++){
  38. S[a][b] -= S[j][b]*tan;
  39. }
  40. }
  41. for(int c=0;c<j;c++){
  42. double tan = S[c][j];
  43. for(int d=j;d<3;d++){
  44. S[c][d] -= S[j][d]*tan;
  45. }
  46. }
  47. }
  48. }
  49.  
  50.  
  51. int main(void) {
  52. double x[N] = {0.15708, 0.23982, 0.37400, 0.57120, 0.82674, 1.04720, 1.23200, 1.43452};
  53. double y[N] = {0.98769, 0.97138, 0.93087, 0.84125, 0.67728, 0.50000, 0.33236, 0.13586};
  54. double A[2][N];
  55. double b[N];
  56.  
  57. double S[2][3];
  58. for(int n=0;n<2;n++){
  59. for(int m=0;m<3;m++){
  60. S[n][m] = 0;
  61. }
  62. }
  63.  
  64. seiki(x, y, A, b, S);
  65. pib(S);
  66.  
  67. for(int j=0;j<2;j++){
  68. printf("a%d = %f\n",j+1, S[j][2]);
  69. }
  70.  
  71. return 0;
  72. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
a1 = -0.497563
a2 = 0.037795