Sunday 18 August 2019

Programming for Lagrange's interpolation method


/*lagrange's interpolation method*/
#include<stdio.h>
#include<math.h>
void
main()
{int i,j,n;
float x[20],y[20],x0,sum=0.0,num,deno;
printf("enter the total number of inputs:");
scanf("%d",&n);
printf("enter the value of x:");
for(i=0;i<n;i++)
{
                scanf("%f",& x[i]);
}
printf("enter the corresponding value of y:");
for(i=0;i<n;i++)
{
                scanf("%f",&y[i]);
}
printf("enter the value of x at which y is calculated:");
scanf("%f",& x0);
for(i=0;i<n;i++)
{
                num=1.0;
                deno=1.0;
                for(j=0;j<n;j++)
                {
                                if(j!=i)
                                {
                                                num=num*(x0-x[j]);
                                                deno=deno*(x[i]-x[j]);
                                }
                }
                sum=sum+(num/deno)*y[i];
}
printf("the required value of y is=%f\n",sum);
}
RESULT:
enter the total number of inputs:7
enter the value of x:2 3 4 5 6 7 8
enter the corresponding value of y:2 7 14 23 34 47 62
enter the value of x at which y is calculated:5.5
the required value of y is=28.250000
Press any key to continue

No comments:

Post a Comment

Thanks for comment.