n -> number of data sets
x[max] -> data points
f[max] -> function values at data points
xp -> point at which interpolation is required
fp -> interpolated value at xp
lf -> Lagrangian factor
*/
#include
#include
#define max 10
void main()
{
int n,i,j;
float x[max],f[max],fp,lf,sum,xp;
clrscr();
printf("\nInput number of data points, n ");
scanf("%d",&n);
printf("\nInput data points x[i] and values f[i] \n");
printf("(one set in each line)\n");
for(i=0;i
scanf("%f %f",&x[i], &f[i]);
}
printf("\nInput value at which interpolation is required, xp ");
scanf("%f",&xp);
sum=0.0;
for(i=0;i
lf=1.0;
for(j=0;j
if(i!=j)
{
lf=lf*(xp-x[j])/(x[i]-x[j]);
}
}
sum=sum+lf*f[i];
}
fp=sum;
printf("\nLagrangian Interpolation \n\n");
printf("Interpolated function value \n");
printf("at x = %f is %f \n",xp,fp);
getch();
}
/*
OUTPUT:
Input number of data points, n 4
Input data points x[i] and values f[i]
(one set in each line)
1 1
2 3
5 6
9 10
Input value at which interpolation is required, xp 6
Lagrangian Interpolation
Interpolated function value
at x = 6.000000 is 6.625000
*/
No comments:
Post a Comment