/* The program for Lagrange Interpolation Polynomial which is used for unequal interval */
#include<stdio.h>
#include<math.h>
void main()
{
int i,j,n;
float x[20],y[20],u,p,p1,sum;
clrscr();
printf("Type the no of points: n=");
scanf("%d",&n);
printf("\nType the u which value will be calculated:u=");
scanf("%f",&u);
for(i=0;i<n;i++)
{
printf("x[%d]:",i+1);
scanf("%f",&x[i]);
printf("y[%d]:",i+1);
scanf("%f",&y[i]);
}
p=1;
for(i=0;i<n;i++)
p=p*(u-x[i]);
sum=0;
for(i=0;i<n;i++)
{
p1=y[i]/(u-x[i]);
for(j=0;j<n;j++)
if(i!=j)
p1=p1/(x[i]-x[j]);
sum+=p1;
}
p=p*sum;
for(i=0;i<n;i++)
printf("%8.4f\t %f\n",x[i],y[i]);
printf("The required value of the polynomial at u=%f is %8.4f\n",u,p);
getch();
}
No comments:
Post a Comment