Numerical Method - Old Questions
2. Write an algorithm and a program to compute the interpolation value at a specific point,
given a set of data points, using Lagrange interpolation method.
Algorithm For Lagrange’s
interpolation
1.
Read
the number of data ‘n’.
2.
Read
the value at which value is needed (say y).
3.
Read
available data points x and f(x).
4.
for
i=0 to n
for
j=0 to n
if(i!=j)
L[i]=L[i]*(y-x[j])/(x[i]-x[j])
end
if
end
for
end
for
5.
for
i=0 to n
sum=sum+L[i]*f[i]
end
for
6.
Print
interpolation value ‘sum’ at y.
Stop
Program
#include<stdio.h>
#include<conio.h>
int
main()
{
float x[10], f[10], y, sum=0.0, l;
int n, i, j;
printf("\\nInput number of data:");
scanf("%d", &n);
printf("\\nInput data points x(i) &
f(i):\\n"); for(i=0;i<n;i++)
{
printf("x[%d]=",i);
scanf("%f", &x[i]);
printf("f[%d]=",i);
scanf("%f", &f[i]);
}
printf("\\nFunctional
value:");
scanf("%f",
&y);
for(i=0;i<n;i++)
{
l=1;
for(j=0;j<n;j++)
{
if(j!=i)
{
l=l*(y-x[j])/(x[i]-x[j]);
}
}
sum=sum+l*f[i];
}
printf("\\nValue
at %f=%f", y, sum);
getch();
return
0;
}