2、设计y=x^(1/2),在x=100,121,144三点处的Newton和Lagrange的二次插值多项式,并用此多项式计算 的近似值。

#include <stdio.h>

#include <math.h>

int main()

{

    int x=115,x0=100,x1=121,x2=144;

    double y,y0,y1,y2,h,h1;

    float f1,f2,f3,l0,l1,l2;

    y0=sqrt(x0);

    y1=sqrt(x1);

    y2=sqrt(x2);

    f1=(float)(y0-y1)/(x0-x1);

    f2=(float)(y1-y2)/(x1-x2);

    f3=(f1-f2)/(x0-x2);

    h=y0+(x-x0)*f1+(x-x0)*(x-x1)*f3;

    printf("Newton插值为:%f\n",h);

 

    l0=(float)(x-x1)*(x-x2)/((x0-x1)*(x0-x2));

    l1=(float)(x-x0)*(x-x2)/((x1-x0)*(x1-x2));

    l2=(float)(x-x0)*(x-x1)/((x2-x0)*(x2-x1));

    h1=y0*l0+y1*l1+y2*l2;

    printf("Lagrange二次插值为:%f\n",h);

}

猜你喜欢

转载自blog.csdn.net/da_ye_zi/article/details/86515254