Here is a C program to solve interpolation formula using Lagrange interpolation method.
#include<stdio.h> #include<conio.h> int main() { int n,i,j; float x1,x[10],f[10],sum=0,pf; //clrscr(); printf("enter the order of polynomial :\n"); scanf("%d",&n); printf("enter the value to be find :\n"); scanf("%f",&x1); printf("enter values for xi & f(xi) :\n"); for(i=0;i<n;i++) { scanf("%f",&x[i]); scanf("%f",&f[i]); } sum=0; for(i=0;i<(n+1);i++) { pf=1; for(j=0;j<(n+1);j++) { if(j!=i) pf=pf*(x1-x[j])/(x[i]-x[j]); } sum=sum+f[i]*pf; } printf("f(%.2f)=%.2f\n",x1,sum); getch(); }
No comments:
Write comments