newton.c

来自「这是数值方法中各种插值算法」· C语言 代码 · 共 74 行

C
74
字号
     #include <stdio.h>
     #define N 2
     double main()
      {
         double Newton(int n,double h,double x[],double y[],double xx);
         int n1=2;
         double x1[3]={0.5,0.6,0.7};
         double y1[3]={0.47943,0.56464,0.64422};
         double h1=0.1;
         double xx1=0.57891;
         double yy1=Newton(n1,h1,x1,y1,xx1);
         printf("The result is:%f\n",yy1);

       }

      double Newton(int n,double h,double x[],double y[],double xx)
        {
          int i,j,s1;
          double t,yy[N][N],s;
          double Newton=0.0;
          double k=y[0];
          t=(xx-x[0])/h;
          s=1.0;
          s1=1;
          for(i=0;i<n;i++)
            for(j=0;j<n-i;j++)
           {
              yy[i][j]=y[j+1]-y[j];
              y[j]= yy[i][j];

            }
          for(i=0;i<n;i++)
           {
              y[i+1]=yy[i][0];

            }

             y[0]=k;

          for(i=0;i<=n;i++)
             {

              if(i==0)
                {
                  s=1;
                  s1=1;
                 }
              else
                {
                  s1=s1*i;
                  s=s*(t-i+1);

                 }
             Newton=Newton+s*y[i]/s1;

             }
            return(Newton);
          }















⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?