
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Array Data Structure
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Spanning Tree
- DSA - Heap
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Lagrange Interpolation
For constructing new data points within a range of a discrete set of given data point, the interpolation technique is used. Lagrange interpolation technique is one of them. When the given data points are not evenly distributed, we can use this interpolation method to find the solution. For the Lagrange interpolation, we have to follow this equation.
Input and Output
Input: List of x and f(x) values. find f(3.25) x: {0,1,2,3,4,5,6} f(x): {0,1,8,27,64,125,216} Output: Result after Lagrange interpolation f(3.25) = 34.3281
Algorithm
largrangeInterpolation(x: array, fx: array, x1)
Input − x array and fx array for getting previously known data, and point x1.
Output: The value of f(x1).
Begin res := 0 and tempSum := 0 for i := 1 to n, do tempProd := 1 for j := 1 to n, do if i ≠ j, then tempProf := tempProd * (x1 – x[j])/(x[i] – x[j]) done tempPord := tempProd * fx[i] res := res + tempProd done return res End
Example
#include<iostream> #define N 6 using namespace std; double lagrange(double x[], double fx[], double x1) { double res = 0, tempSum = 0; for(int i = 1; i<=N; i++) { double tempProd = 1; //for each iteration initialize temp product for(int j = 1; j<=N; j++) { if(i != j) { //if i = j, then denominator will be 0 tempProd *= (x1 - x[j])/(x[i] - x[j]); //multiply each term using formula } } tempProd *= fx[i]; //multiply f(xi) res += tempProd; } return res; } main() { double x[N+1] = {0,1,2,3,4,5,6}; double y[N+1] = {0,1,8,27,64,125,216}; double x1 = 3.25; cout << "Result after lagrange interpolation f("<<x1<<") = " << lagrange(x, y, x1); }
Output
Result after lagrange interpolation f(3.25) = 34.3281
- Related Articles
- C++ program to implement Inverse Interpolation using Lagrange Formula
- Interpolation Search
- Bessel’s Interpolation in C++
- Interpolation Search in JavaScript
- Lagrange’s Interpolation in C++
- String Interpolation in Dart Programming
- C++ Program to Implement Interpolation Search Algorithm
- How to do string interpolation in JavaScript?
- Python Pandas - Fill NaN with Linear Interpolation
- Python Pandas - Fill NaN with Polynomial Interpolation
- Golang program to demonstrate the string interpolation
- How to get smooth interpolation when using pcolormesh (Matplotlib)?
- Python Pandas - Fill NaN values using an interpolation method
- How to implement ‘cubic’ 1-D interpolation using SciPy library?
- Comparing ‘cubic’ and ‘linear’ 1-D interpolation using SciPy library

Advertisements