Write a program to demonstrate Lagrange's interpolation formula in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (n2)Space Complexity: O (n)Open Source: MIT
1
2
            
3
        /* program to execute Lagrange's  interpolation */
4
	#include <stdio.h>
5
	#include < conio.h>
6
	#define max 50
7
	void main()
8
	{
9
	    float arrx[max + 1], arry[max + 1], num, den, x, y = 0.0;
10
	    int i, j, n;
11
	    printf("Enter  the value of n:  ");
12
	    scanf("%d", &n);
13
	    printf("Enter value for x and y: \n"); /* Input data */
14
	    for (i = 0; i <= n; i++)
15
		scanf("%f %f", &arrx[i], &arry[i]);
16
	    printf("\nEnter value of x for which value of y is needed: ");
17
	    scanf("%f", &x);
18
	    for (i = 0; i <= n; i++) /* Apply Interpolation Formula */
19
	    {
20
	        num = den = 1;
21
	        for (j = 0; j <= n; j++)
22
	            if (j != i)
23
	            {
24
	                num *= x - arrx[j];
25
	                den *= arrx[i] - arrx[j];
26
	            }
27
	        y += (num / den) * arry[i];
28
	    }
29
	    printf("Value of y = %f  , when x = %f", y, x);
30
	    getch();
31
	}
32
           
33
        

Output

1
2
3
        Enter the value of n: 4
4
	Enter values of x and y:
5
	5 150
6
	7 392
7
	11 1452
8
	13 2366
9
	17 5202
10
	
11
	Enter value of x for which value of y is needed: 9
12
	Value of y = 809.999939, when x = 9.000000  
13
           
14
            

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run Lagrange's Interpolation Formula program in C-Language?

To run Lagrange's Interpolation Formula program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More about Lagrange’s Interpolation Formula

Discovered by: English mathematician Edward Waring

Discovered Year: 1779

Modified by: French mathematician Joseph Louis Lagrange in 1795

The Lagrange Interpolation Formula is used to find missing data from table data, but the intervals may or may not be equal.

Difference: Newton Forward or Newton Backward interpolation requires equal interval values, but in Lagrange’s Interpolation formula, the interval gaps may be different or the same.

So, Lagrange’s formula provides greater flexibility for non-equidistant data points compared to Newton’s Forward/Backward methods, though it comes with higher computational cost when adding new data points.

How does Lagrange’s Interpolation Formula Work?

Make a Lagrange basis polynomial (li(x)) based upon the given data points like (x0, y0), (x1, y1), … then calculate the values with the help of the formula mentioned below

Image

Why is Lagrange’s Interpolation Formula used?

Lagrange’s Interpolation Formula is used to find estimated data based on real-world data, like measurement readings, with equal or unequal intervals, like hourly data or distance in meters, etc.

Real-life applications of Lagrange’s Interpolation Formula

  1. Experimental & Sensor Data: To calculate the mid values of the experimental data or sensor data at uneven amount of time interval.
  2. Computer graphics & CAD: To draw a smooth curve or surface for unevenly spaced control points in computer-aided design and graphics.
  3. Astronomy and Space Science: To estimate the positions of the planet and stars in space with uneven interval distance from each other than this method is used.

Limitations of Lagrange’s Interpolation Formula

  1. Recalculations if adding a new data point: If a new data point is introduced in the data table, then all the calculations and terms must be recalculated again
  2. Runge’s Phenomenon (High-Degree Problem): If the data points are at large spacing, then there is a risk of curve fluctuations when getting the result.

Alternate Methods

  1. Newton’s Divided Difference Interpolation Formula is also used to get the result based on unequal intervals, but you don’t need to recalculate from the beginning when adding a new point in the data table.
  2. Spline Interpolation (Cubic splines): If the number of points is very high and the curve fluctuation (oscillation) problem also occurs, then the curve is divided into small parts, and a smooth result is obtained with this spline interpolation method.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.