Lagrange Interpolation - C Program
Lagrange Interpolation - C Program
Aim: To calculate y value with the respect of x values using Lagrange interpolation method.
Algorithm
Step 1: Start the program
Step 2: Declare x[100], y[100], a, s=1, t=1, k=0 as float data type and i, j, n, d=1 as integer data type
Step 3: Read n and x[i] and y[i] values using for loop
Step 4: Display x & y values
Step 5: Read the value of x to find the respective value of y using condition while (d==1)
Step 6: using for loop calculate s & t values
a) initialise s = 1, t = 1
b) if j! = i
s = s * (a x[j])
t = t * (x[i] x[j])
c) k = k+ ((s/t) * y[i])
Step 7: Display the value of k
Step 8: Repeat the value (if needed)
Step 9: Stop the program
Flowchart
Start
while
d==1
read a
s = s * (a x[j])
t = t * (x[i] x[j])
k = k + ((s/t) * y[j])
print k
X
read choice
Program
#include<stdio.h>
False
if
choice = 1
Stop
#include<conio.h>
void main()
{
float x[100],y[100],a,s=1,t=1,k=0;
int n,i,j,d=1;
clrscr();
printf ("**__(57 *s)__**\n");
printf ("*\t\tLagrange Interpolation\t\t\t*\n");
printf ("**__(57 *s)__**\n\n");
printf("\n How many record you will be enter:");
scanf("%d",&n);
printf("\n Enter the respective values of the variables of x and y: \n");
for(i=0;i<n;i++)
{
scanf("%f",&x[i]);
scanf("%f",&y[i]);
}
printf("\n\n The table you entered as follows: \n\n");
for(i=0;i<n;i++)
{
printf("%0.3f\t%0.3f",x[i],y[i]);
printf("\n");
}
while(d==1)
{
printf("\n\n\n Enter the value of the x to find the respective value of y\n\n\n");
scanf("%f",&a);
for(i=0;i<n;i++)
{
s=1;
t=1;
for(j=0;j<n;j++)
{
if(j!=i)
{
s=s*(a-x[j]);
t=t*(x[i]-x[j]);
}
}
k=k+((s/t)*y[i]);
}
printf("\n**__(25 *s)__**THE RESULT**__(24 *s)__**\n");
printf("\n\n The respective value of the variable y is:%f",k);
printf("\n\n Do you want to continue?\n\n Press 1 to continue and any other key to exit");
scanf("%d",&d);
}
}
Output