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:
Copy the Code: Press the copy button in the code block to copy the complete code.
Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
Paste the code: Paste the copied code into Turbo C++’s editor.
Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
Enter the Input: Provide the required inputs or your desired inputs, if applicable.
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
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
Experimental & Sensor Data: To calculate the mid values of the experimental data or sensor data at uneven amount of time interval.
Computer graphics & CAD: To draw a smooth curve or surface for unevenly spaced control points in computer-aided design and graphics.
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
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
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
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.
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
How to submit your modified code?
If you are a student, teacher, or anyone who wants to submit your code for credit, then follow the steps below to easily submit your code to the dhimanp platform.
Important Note:
Code will be accepted with zero errors.
All code should be text only (no file attachments allowed).
Make sure you have all the code in Notepad.
Code should include comments and proper formatting so that your code is easily understandable.
Below are the steps:
Add Program Purpose and Your Details: Open Notepad, add the purpose of the program on line 1, and add your full Name, school or college name, and city name on line 2 as comments. You may skip the school or college name if you don’t have one.
Add Your Code: Copy your code and paste it into Notepad on line 3.
Optional for More Files: If you have more than one file, then paste each file's code into Notepad one by one, with the file name too, from line 3.
Insert code into the Page Review Input box: Copy the Notepad text content and paste this content into the review input box below.
Submit: Enter your name in the input field (name is optional here) and click on the Submit button.
1
// Program: numerical analysis lagrange interpolation formula program
2
// Submitted by: Aarav Sharma, ABC Public School, Delhi
3
4
//file1.c
5
#include <stdio.h>
6
int main()
7
{
8
// Your code here
9
return 0;
10
}
How do we verify and give credit?
After receiving your submitted code, we will review it, verify it, and run your program.
If your program is 100% safe, secure, and unique, then we will add the code as a new Page with proper definitions and description details of this code.
We will add your full name, school or college name, and city as you provide.
This process may take up to 1 week.
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.