0% found this document useful (0 votes)
2K views5 pages

Lagrange Interpolation - C Program

The document describes the Lagrange interpolation method. It involves: 1) Taking input values for x and y coordinates and storing them in arrays. 2) Calculating interpolation coefficients s and t using the input x values. 3) Computing the interpolated y value (k) for a given x value (a) using a weighted sum of the y values based on the coefficients s and t. 3) Repeating the interpolation for different x values as needed.

Uploaded by

Befzz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
2K views5 pages

Lagrange Interpolation - C Program

The document describes the Lagrange interpolation method. It involves: 1) Taking input values for x and y coordinates and storing them in arrays. 2) Calculating interpolation coefficients s and t using the input x values. 3) Computing the interpolated y value (k) for a given x value (a) using a weighted sum of the y values based on the coefficients s and t. 3) Repeating the interpolation for different x values as needed.

Uploaded by

Befzz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Lagrange interpolation

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

float x[100], y[100], a, s=1, t=1, k=0


int i, j, n, d=1
read n
False

for(i = 0; i < n; i++)


read x[i], y[i]
False

for(i = 0; i < n; i++)


print x[i], y[i]

while
d==1

read a

for(i = 0; i < n; i++)


s =1
t=1
for(i = 0; j < n; j++)
if
j!=i

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

You might also like