/*
 * -----------------------
 *      Lagrange보간     *
 * -----------------------
 */


#include <stdio.h>

double lagrange(double *,double *,int,double);

int main(void)
{
    static double x[]={0.0,1.0,3.0,6.0,7.0},
                  y[]={0.8,3.1,4.5,3.9,2.8};

    double t;

    printf("      x      y\n");
    for (t=0.0;t<=7.0;t=t+.5)
        printf("%7.2f%7.2f\n",t,lagrange(x,y,5,t));

 return 0;
}
double lagrange(double x[],double y[],int n,double t)
{
    int i,j;
    double s,p;

    s=0.0;
    for (i=0;i<n;i++){
        p=y[i];
        for (j=0;j<n;j++){
            if (i!=j)
                p=p*(t-x[j])/(x[i]-x[j]);
        }
        s=s+p;
    }
    return s;
}


//  Lagrange보간   :  몇 쌍의 x, y 데이터가 주어졌을 때, 이 점들을 지나는 보간 다항식을

  Lagrange보간으로 구하고 주어진 점 이외의 점 데이터를 구한다. n-1차의 다항식이 된다.

+ Recent posts