Implementation of Lagrange Interpolation Formula in C.
Here is the code..
//AIM : TO IMPLEMENT LAGRANGE INTERPOLATION FORMULA
#include<stdio.h>
#define m 4
void main()
{ float x[m],y[m],num,den,t,fx;
int i,j;
printf("LAGRANGE,S INTERPOLATION FORMULA\n");
i=m;
printf("Enter the %d values of x\n",i);
for(i=0;i<m;i++)
scanf("%f",&x[i]);
i=m;
printf("\n\nEnter the %d values of f(x)\n",i);
for(i=0;i<m;i++)
scanf("%f",&y[i]);
printf("\n\n Enter X about which you want f(x)\n");
scanf("%f",&t);
fx=0.0;
for(i=0;i<m;i++)
{ num=1;
den=1;
for(j=0;j<m;j++)
{ if(i!=j)
{ num=num*(t-x[j]);
den=den*(x[i]-x[j]);
}
}
fx=fx+(y[i]*(num/den));
}
printf("when x=%f the f(x)=%f",t,fx);
}
For Turbo C users.
//AIM : TO IMPLEMENT LAGRANGE INTERPOLATION FORMULA
#include<stdio.h>
#include<conio.h>
#define m 4
void main()
{ float x[m],y[m],num,den,t,fx;
int i,j;
clrscr();
printf("LAGRANGE,S INTERPOLATION FORMULA\n");
i=m;
printf("Enter the %d values of x\n",i);
for(i=0;i<m;i++)
scanf("%f",&x[i]);
i=m;
printf("\n\nEnter the %d values of f(x)\n",i);
for(i=0;i<m;i++)
scanf("%f",&y[i]);
printf("\n\n Enter X about which you want f(x)\n");
scanf("%f",&t);
fx=0.0;
for(i=0;i<m;i++)
{ num=1;
den=1;
for(j=0;j<m;j++)
{ if(i!=j)
{ num=num*(t-x[j]);
den=den*(x[i]-x[j]);
}
}
fx=fx+(y[i]*(num/den));
}
printf("when x=%f the f(x)=%f",t,fx);
getch();
}
No comments:
Post a Comment