0% found this document useful (0 votes)
17 views2 pages

mx02 //lagrange Interpolation // (1,12.8), (2,19.6), (3, 28.5), (4, 40.2) // Approximate F at X 2.4

The document contains code to perform Lagrange interpolation on a set of data points to approximate the value of a function f at a given x value. It takes in 4 data points as (x, f(x)) pairs, then uses Lagrange interpolation formula to calculate the value of f at x=2.4 by multiplying the y values by the Lagrange basis polynomials and summing the results.

Uploaded by

aef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

mx02 //lagrange Interpolation // (1,12.8), (2,19.6), (3, 28.5), (4, 40.2) // Approximate F at X 2.4

The document contains code to perform Lagrange interpolation on a set of data points to approximate the value of a function f at a given x value. It takes in 4 data points as (x, f(x)) pairs, then uses Lagrange interpolation formula to calculate the value of f at x=2.4 by multiplying the y values by the Lagrange basis polynomials and summing the results.

Uploaded by

aef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

//mx02

//lagrange interpolation
//(1,12.8), (2,19.6), (3, 28.5), (4, 40.2)
// approximate f at x = 2.4
#include<stdio.h>
#include<math.h>
int main()
{
float x[10], y[10], temp=1, f[10], sum, p;
int i,n,j,k=0;
printf("Enter the no. of data points: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\n\nEnter the value of x%d: ", i);
scanf("%f",&x[i]);
printf("\n\nEnter the value of f(x%d): ", i);
scanf("%f",&y[i]);
}
printf("\n\nEnter the the value of x to approximate the value of f(x): ");
scanf("%f",&p);
for(i=0;i<n;i++)
{
temp = 1;
k = i;
for(j=0; j<n; j++)
{
if(k==j)
{
continue;
}
else
{
temp = temp * ((p-x[j])/(x[k]-x[j]));
}
}
f[i] = y[i] * temp;
}
for(i=0;i<n;i++)
{
sum = sum + f[i];
}

printf("\n\nf(%.1f) = %f\n", p, sum);


}

You might also like