Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Assignment - 7

Uploaded by

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

Assignment - 7

Uploaded by

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

ASSIGNMENT – 7

Problem Statement: Write a C program to find the value of f(x) using Lagranges’s Formula.
Consider the following x and y value: -

x : -1 0 2 5

f(x) : 9 5 3 15

Solution:

Algorithm:

1) Start
2) Store known data points in arrays: one for x values, one for f(x) values.
3) Input the value where you want to estimate f(x).
4) Initialize total result to zero.
5) For each data point:
a. Compute its weight based on how far the input is from all other x values.
b. Multiply that weight by its corresponding f(x) value.
c. Add this contribution to the total result.
6) Display the final estimated value of f(x).
7) Stop

Program
Code:

#include <stdio.h>

int main() {
int i, j, n = 4;
double x[] = {-1, 0, 2, 5};
double y[] = {9, 5, 3, 15};
double xp, yp = 0, term;

printf("Enter the value of x for interpolation: ");


scanf("%lf", &xp);

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


term = y[i];
for (j = 0; j < n; j++) {
if (j != i) {
term *= (xp - x[j]) / (x[i] - x[j]);
}
}
yp += term;
}

printf("\nEstimated value of f(%.2lf) = %.4lf\n", xp, yp);


return 0;
}

Output:

Discussion: This C program uses Lagrange interpolation to estimate ( f(x) ) from given data
points. It loops through each point, calculates a weighted term, and sums them to compute the
interpolated value. Simple structure, ideal for fixed datasets.

You might also like