Numerical analysis cubic spline interpolation and realization

 analysis:

The first question, given that the first boundary condition

The second question, given that the second boundary condition

We want to follow the steps of, respectively Cubic spline interpolation to the first class and the second boundary condition

In order not to repeat the calculation, and easy to expand, we use the C ++ programming cycle can be achieved.

 (This is certainly not the hand count, the hand count will hand acid)

  1 #include <cstdio>
  2 #include <iostream>
  3 
  4 using namespace std;
  5 
  6 const int N = 5;
  7 
  8 int main()
  9 {
 10     // 录入数据
 11     double x[N]={0.25,0.30,0.39,0.45,0.53};
 12     double y[N]={0.5,0.5477,0.6245,0.6708,0.7280};
 13     // 输出数据
 14     printf("X(i),i=0...%d\n",N-1);
 15     for(int i=0;i<N;++i)
 16     {
 17         printf("%f ",x[i]);
 18     }
 19     printf("\n\nY(i),i=0...%d\n",N-1);
 20     for(int i=0;i<N;++i)
 21     {
 22         printf("%f ",y[i]);
 23     }
 24     printf("\n\n");
 25 
 26     // 计算h[i]
 27     double h[N];
 28     for(int i=0;i<N-1;++i)
 29     {
 30         h[i]=x[i+1]-x[i];
 31     }
 32     // 输出数据
 33     printf("h(i),i=0...%d\n",N-2);
 34     for(int i=0;i<N-1;++i)
 35     {
 36         printf("%f ",h[i]);
 37     }
 38     putchar('\n');
 39     putchar('\n');
 40 
 41     // 计算u[i],r[i]
 42     double u[N],r[N];
 43     for(int i=1;i<N;++i)
 44     {
 45         u[i]=h[i-1]/(h[i-1] + h[i]);
 46         r[i]=h[i]/(h[i-1] + h[i]);
 47     }
 48     // 输出数据
 49     printf("u(i),i=1...%d\n",N-1);
 50     for(int i=1;i<N;++i)
 51     {
 52         printf("%f ",u[i]);
 53     }
 54     putchar('\n');
 55     putchar('\n');
 56     printf("r(i),i=1...%d\n",N-1);
 57     for(int i=1;i<N;++i)
 58     {
 59         printf("%f ",r[i]);
 60     }
 61     putchar('\n');
 62     putchar('\n');
 63 
 64     // 计算f[x(i-1),x(i)]
 65     // 用f[i]表示f[x(i-1),x(i)]
 66     double f[N+1];
 67     for(int i=1;i<N;++i)
 68     {
 69         f[i]=(y[i] - y[i-1])/(x[i] - x[i-1]);
 70     }
 71     // 输出数据
 72     printf("f[x(i),x(i-1)],i=1...%d\n",N-1);
 73     for(int i=1;i<N;++i)
 74     {
 75          the printf ( " % F " , F [I]);
 76      }
 77      the putchar ( ' \ n- ' );
 78      the putchar ( ' \ n- ' );
 79  
80      // record both derivative 
81      F [ 0 ] = . 1 ;
 82      F [N] = 0.6868 ;
 83  
84      // calculate D (I) 
85      Double D [N + . 1 ];
 86      D [ 0 ] = . 6 * (F [ . 1]-f[0])/h[0];
 87     for(int i=1;i<N-1;++i)
 88     {
 89         d[i]=6*(f[i+1]-f[i])/(h[i-1] + h[i]);
 90     }
 91     d[N]=6*(f[N]-f[N-1])/h[N-2];
 92 
 93     printf("d[i],i=0...%d\n",N);
 94     for(int i=0;i<=N;++i)
 95     {
 96         printf("%f ",d[i]);
 97     }
 98 
 99     // AX=B
100     // X=[A^(-1)]*B
101     return 0;
102 }

 

 

After obtaining the above data, the matrix is ​​solved to obtain substituting all the values ​​M (i), the inverse matrix multiplication of the matrix corresponding to the operation, or in python matlab can be implemented, not repeated here realized, just above the resulting calculation data, written in the corresponding file, and then read in python numpy call library function or be implemented by matlab.

 

After all the data has been obtained, into the cubic spline function is also desired

 

Guess you like

Origin www.cnblogs.com/jishuren/p/12392668.html