#include <stdio.h>
#include <math.h>
#define nd 5
double xd[nd]={0,1,2,4,5};
double yd[nd]={0,16,48,88,0};
double Lagrange(double x);
int main() {
double x=3.0,y;
y=Lagrange(x);
printf("\n x=%f : f(x)=%f\n",x,y);
}
double Lagrange(double x)
{
double p, sum;
int n,i,j;
n=nd-1;
sum=0;
for(i=0;i<=n;i++) {
p=1;
for(j=0;j<=n;j++) {
if(i!=j)
p=p*(x-xd[j])/(xd[i]-xd[j]);
printf("p값:%f\n",p);
}
sum=sum+p*yd[i];
}
return(sum);
}