/*Lagrange’s Interpolation */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],f[MAX],lf=1,sum=0,xp;
clrscr();
printf("Lagrange's Interpolation\n");
printf("Enter No. of Data points : ");
scanf("%d",&n);
printf("Enter data points x(i) and function value f(i)\n");
printf("(One set in each line)\n");
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&f[i]);
printf("Enter Value of x at which Interpolation is required : ");
scanf("%f",&xp);
for(i=1;i<=n;i++)
{
lf=1;
for(j=1;j<=n;j++)
{
if(i!=j)
lf=lf*((xp-x[j])/(x[i]-x[j]));
}
sum=sum+lf*f[i];
}
printf("Interpolated Function value : \n");
printf("at x = %5.2f is %f \n",xp,sum);
getch();
}
OUTPUT
Lagrange's Interpolation
Enter No. of Data points : 4
Enter data points x(i) and function value f(i)
(One set in each line)
0 3
1 9
3 17
5 39
Enter Value of x at which Interpolation is required : 2
Interpolated Function value : at x = 2.00 is 12.700000
No comments:
Post a Comment