Sunday 17 March 2013

C Program for Lagrange's Interpolation



#include<stdio.h>
#include<conio.h>
#define MAX 10
void main(){
clrscr();
float x[MAX+1], fx[MAX+1], num, den, val, y=0;
int i, j, n;
printf("\t\tLAGRANGE'S INTERPOLATION");
printf("\n\nEnter the degree of interpolation(n): ");
scanf("%d", &n);
for(i=0; i<=n; i++){
printf("\n x%d: ",i);
scanf("%f", &x[i]);
printf("\n fx%d: ",i);
scanf("%f", &fx[i]);
}
printf("\nEnter x to estimate fx: ");
scanf("%f", &val);

for (i=0; i<=n; i++){
num=1;
den=1;
for (j=0; j<=n; j++)
if(j!=i){
num *= val-x[j];
den *= x[i]-x[j];
}
y+=(num/den)*fx[i];
}

printf("\nApproximate value at %.3f: %.3f",val,y);
getch();
}

Sample Output:

LAGRANGE’S INTERPOLATION
Enter the degree of interpolation(n): 2
x0: 1
fx0: 1
x1: 2
fx1: 1.4142
x2: 3
fx2: 1.7321

Enter x to estimate fx: 2.5
Approximate value at 2.500: 1.585



©Dixit Bhatta 2013

No comments:

Post a Comment

Was this post helpful? Ask any questions you have, I will try to answer them for you.