Newton's Divided Difference Interpolation Formula
Last Updated :
14 Nov, 2022
Interpolation is an estimation of a value within two known values in a sequence of values. Newton's divided difference interpolation formula is an interpolation technique used when the interval difference is not same for all sequence of values. Suppose f(x0), f(x1), f(x2).........f(xn) be the (n+1) values of the function y=f(x) corresponding to the arguments x=x0, x1, x2...xn, where interval differences are not same Then the first divided difference is given by
f[x_0, x_1]=\frac{f(x_1)-f(x_0)}{x_1-x_0}
The second divided difference is given by
f[x_0, x_1, x_2]=\frac{f[x_1, x_2]-f[x_0, x_1]}{x_2-x_0}
and so on... Divided differences are symmetric with respect to the arguments i.e independent of the order of arguments. so, f[x0, x1]=f[x1, x0] f[x0, x1, x2]=f[x2, x1, x0]=f[x1, x2, x0] By using first divided difference, second divided difference as so on .A table is formed which is called the divided difference table. Divided difference table: 
Advantages of NEWTON'S DIVIDED DIFFERENCE INTERPOLATION FORMULA
- These are useful for interpolation.
- Through difference table, we can find out the differences in higher order.
- Differences at each stage in each of the columns are easily measured by subtracting the previous value from its immediately succeeding value.
- The differences are found out successively between the two adjacent values of the y variable till the ultimate difference vanishes or become a constant.
NEWTON'S DIVIDED DIFFERENCE INTERPOLATION FORMULA
f(x)=f(x_0)+(x-x_0)f[x_0, x_1]+(x-x_0)(x-x_1)f[x_0, x_1, x_2]+..........................+(x-x_0)(x-x_1)...(x-x_k_-_1)f[x_0, x_1, x_2...x_k]
Examples:
Input: Value at 7
Output: Value at 7 is 13.47
Below is the implementation of Newton's divided difference interpolation method.
C++
// CPP program for implementing
// Newton divided difference formula
#include <bits/stdc++.h>
using namespace std;
// Function to find the product term
float proterm(int i, float value, float x[])
{
float pro = 1;
for (int j = 0; j < i; j++) {
pro = pro * (value - x[j]);
}
return pro;
}
// Function for calculating
// divided difference table
void dividedDiffTable(float x[], float y[][10], int n)
{
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++) {
y[j][i] = (y[j][i - 1] - y[j + 1]
[i - 1]) / (x[j] - x[i + j]);
}
}
}
// Function for applying Newton's
// divided difference formula
float applyFormula(float value, float x[],
float y[][10], int n)
{
float sum = y[0][0];
for (int i = 1; i < n; i++) {
sum = sum + (proterm(i, value, x) * y[0][i]);
}
return sum;
}
// Function for displaying
// divided difference table
void printDiffTable(float y[][10],int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
cout << setprecision(4) <<
y[i][j] << "\t ";
}
cout << "\n";
}
}
// Driver Function
int main()
{
// number of inputs given
int n = 4;
float value, sum, y[10][10];
float x[] = { 5, 6, 9, 11 };
// y[][] is used for divided difference
// table where y[][0] is used for input
y[0][0] = 12;
y[1][0] = 13;
y[2][0] = 14;
y[3][0] = 16;
// calculating divided difference table
dividedDiffTable(x, y, n);
// displaying divided difference table
printDiffTable(y,n);
// value to be interpolated
value = 7;
// printing the value
cout << "\nValue at " << value << " is "
<< applyFormula(value, x, y, n) << endl;
return 0;
}
Java
// Java program for implementing
// Newton divided difference formula
import java.text.*;
import java.math.*;
class GFG{
// Function to find the product term
static float proterm(int i, float value, float x[])
{
float pro = 1;
for (int j = 0; j < i; j++) {
pro = pro * (value - x[j]);
}
return pro;
}
// Function for calculating
// divided difference table
static void dividedDiffTable(float x[], float y[][], int n)
{
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++) {
y[j][i] = (y[j][i - 1] - y[j + 1]
[i - 1]) / (x[j] - x[i + j]);
}
}
}
// Function for applying Newton's
// divided difference formula
static float applyFormula(float value, float x[],
float y[][], int n)
{
float sum = y[0][0];
for (int i = 1; i < n; i++) {
sum = sum + (proterm(i, value, x) * y[0][i]);
}
return sum;
}
// Function for displaying
// divided difference table
static void printDiffTable(float y[][],int n)
{
DecimalFormat df = new DecimalFormat("#.####");
df.setRoundingMode(RoundingMode.HALF_UP);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
String str1 = df.format(y[i][j]);
System.out.print(str1+"\t ");
}
System.out.println("");
}
}
// Driver Function
public static void main(String[] args)
{
// number of inputs given
int n = 4;
float value, sum;
float y[][]=new float[10][10];
float x[] = { 5, 6, 9, 11 };
// y[][] is used for divided difference
// table where y[][0] is used for input
y[0][0] = 12;
y[1][0] = 13;
y[2][0] = 14;
y[3][0] = 16;
// calculating divided difference table
dividedDiffTable(x, y, n);
// displaying divided difference table
printDiffTable(y,n);
// value to be interpolated
value = 7;
// printing the value
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.HALF_UP);
System.out.println("\nValue at "+df.format(value)+" is "
+df.format(applyFormula(value, x, y, n)));
}
}
// This code is contributed by mits
Python3
# Python3 program for implementing
# Newton divided difference formula
# Function to find the product term
def proterm(i, value, x):
pro = 1;
for j in range(i):
pro = pro * (value - x[j]);
return pro;
# Function for calculating
# divided difference table
def dividedDiffTable(x, y, n):
for i in range(1, n):
for j in range(n - i):
y[j][i] = ((y[j][i - 1] - y[j + 1][i - 1]) /
(x[j] - x[i + j]));
return y;
# Function for applying Newton's
# divided difference formula
def applyFormula(value, x, y, n):
sum = y[0][0];
for i in range(1, n):
sum = sum + (proterm(i, value, x) * y[0][i]);
return sum;
# Function for displaying divided
# difference table
def printDiffTable(y, n):
for i in range(n):
for j in range(n - i):
print(round(y[i][j], 4), "\t",
end = " ");
print("");
# Driver Code
# number of inputs given
n = 4;
y = [[0 for i in range(10)]
for j in range(10)];
x = [ 5, 6, 9, 11 ];
# y[][] is used for divided difference
# table where y[][0] is used for input
y[0][0] = 12;
y[1][0] = 13;
y[2][0] = 14;
y[3][0] = 16;
# calculating divided difference table
y=dividedDiffTable(x, y, n);
# displaying divided difference table
printDiffTable(y, n);
# value to be interpolated
value = 7;
# printing the value
print("\nValue at", value, "is",
round(applyFormula(value, x, y, n), 2))
# This code is contributed by mits
C#
// C# program for implementing
// Newton divided difference formula
using System;
class GFG{
// Function to find the product term
static float proterm(int i, float value, float[] x)
{
float pro = 1;
for (int j = 0; j < i; j++) {
pro = pro * (value - x[j]);
}
return pro;
}
// Function for calculating
// divided difference table
static void dividedDiffTable(float[] x, float[,] y, int n)
{
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++) {
y[j,i] = (y[j,i - 1] - y[j + 1,i - 1]) / (x[j] - x[i + j]);
}
}
}
// Function for applying Newton's
// divided difference formula
static float applyFormula(float value, float[] x,
float[,] y, int n)
{
float sum = y[0,0];
for (int i = 1; i < n; i++) {
sum = sum + (proterm(i, value, x) * y[0,i]);
}
return sum;
}
// Function for displaying
// divided difference table
static void printDiffTable(float[,] y,int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
Console.Write(Math.Round(y[i,j],4)+"\t ");
}
Console.WriteLine("");
}
}
// Driver Function
public static void Main()
{
// number of inputs given
int n = 4;
float value;
float[,] y=new float[10,10];
float[] x = { 5, 6, 9, 11 };
// y[][] is used for divided difference
// table where y[][0] is used for input
y[0,0] = 12;
y[1,0] = 13;
y[2,0] = 14;
y[3,0] = 16;
// calculating divided difference table
dividedDiffTable(x, y, n);
// displaying divided difference table
printDiffTable(y,n);
// value to be interpolated
value = 7;
// printing the value
Console.WriteLine("\nValue at "+(value)+" is "
+Math.Round(applyFormula(value, x, y, n),2));
}
}
// This code is contributed by mits
PHP
<?php
// PHP program for implementing
// Newton divided difference formula
// Function to find the product term
function proterm($i, $value, $x)
{
$pro = 1;
for ($j = 0; $j < $i; $j++)
{
$pro = $pro * ($value - $x[$j]);
}
return $pro;
}
// Function for calculating
// divided difference table
function dividedDiffTable($x, &$y, $n)
{
for ($i = 1; $i < $n; $i++)
{
for ($j = 0; $j < $n - $i; $j++)
{
$y[$j][$i] = ($y[$j][$i - 1] -
$y[$j + 1][$i - 1]) /
($x[$j] - $x[$i + $j]);
}
}
}
// Function for applying Newton's
// divided difference formula
function applyFormula($value, $x, $y,$n)
{
$sum = $y[0][0];
for ($i = 1; $i < $n; $i++)
{
$sum = $sum + (proterm($i, $value, $x) *
$y[0][$i]);
}
return $sum;
}
// Function for displaying
// divided difference table
function printDiffTable($y, $n)
{
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $n - $i; $j++)
{
echo round($y[$i][$j], 4) . "\t ";
}
echo "\n";
}
}
// Driver Code
// number of inputs given
$n = 4;
$y = array_fill(0, 10, array_fill(0, 10, 0));
$x = array( 5, 6, 9, 11 );
// y[][] is used for divided difference
// table where y[][0] is used for input
$y[0][0] = 12;
$y[1][0] = 13;
$y[2][0] = 14;
$y[3][0] = 16;
// calculating divided difference table
dividedDiffTable($x, $y, $n);
// displaying divided difference table
printDiffTable($y, $n);
// value to be interpolated
$value = 7;
// printing the value
echo "\nValue at " . $value . " is " .
round(applyFormula($value, $x,
$y, $n), 2) . "\n"
// This code is contributed by mits
?>
JavaScript
// JavaScript program for implementing
// Newton divided difference formula
// Function to find the product term
function proterm(i, value, x)
{
let pro = 1;
for (var j = 0; j < i; j++) {
pro = pro * (value - x[j]);
}
return pro;
}
// Function for calculating
// divided difference table
function dividedDiffTable(x, y, n)
{
for (var i = 1; i < n; i++) {
for (var j = 0; j < n - i; j++) {
y[j][i] = (y[j][i - 1] - y[j + 1]
[i - 1]) / (x[j] - x[i + j]);
}
}
}
// Function for applying Newton's
// divided difference formula
function applyFormula(value, x, y, n)
{
let sum = y[0][0];
for (var i = 1; i < n; i++) {
sum = sum + (proterm(i, value, x) * y[0][i]);
}
return sum;
}
// Function for displaying
// divided difference table
function printDiffTable(y, n)
{
for (var i = 0; i < n; i++) {
for (var j = 0; j < n - i; j++) {
process.stdout.write(y[i][j].toFixed(4) + "\t ");
}
process.stdout.write("\n");
}
}
// Driver Function
// number of inputs given
let n = 4;
let value, sum;
let x = [ 5, 6, 9, 11 ];
let y = [];
for (var i = 0; i < 10; i++)
y.push(new Array(10));
// y[][] is used for divided difference
// table where y[][0] is used for input
y[0][0] = 12;
y[1][0] = 13;
y[2][0] = 14;
y[3][0] = 16;
// calculating divided difference table
dividedDiffTable(x, y, n);
// displaying divided difference table
printDiffTable(y,n);
// value to be interpolated
value = 7;
// printing the value
console.log("\nValue at " + value + " is " + applyFormula(value, x, y, n));
// This code is contributed by phasing17
Output12 1 -0.1667 0.05
13 0.3333 0.1333
14 1
16
Value at 7 is 13.47
Time complexity: O(n2)
Auxiliary space: O(1)
Similar Reads
Eigenvalues and Eigenvectors Eigenvalues and eigenvectors are fundamental concepts in linear algebra, used in various applications such as matrix diagonalization, stability analysis and data analysis (e.g., PCA). They are associated with a square matrix and provide insights into its properties.Eigen value and Eigen vectorTable
10 min read
System of Linear Equations A system of linear equations is a set of two or more linear equations involving the same variables. Each equation represents a straight line or a plane and the solution to the system is the set of values for the variables that satisfy all equations simultaneously.Here is simple example of system of
5 min read
LU Decomposition LU decomposition or factorization of a matrix is the factorization of a given square matrix into two triangular matrices, one upper triangular matrix and one lower triangular matrix, such that the product of these two matrices gives the original matrix. It is a fundamental technique in linear algebr
6 min read
Doolittle Algorithm | LU Decomposition Doolittle Algorithm: The Doolittle Algorithm is a method for performing LU Decomposition, where a given matrix is decomposed into a lower triangular matrix L and an upper triangular matrix U. This decomposition is widely used in solving systems of linear equations, inverting matrices, and computing
11 min read
Limits, Continuity and Differentiability Limits, Continuity, and Differentiation are fundamental concepts in calculus. They are essential for analyzing and understanding function behavior and are crucial for solving real-world problems in physics, engineering, and economics.Table of ContentLimitsKey Characteristics of LimitsExample of Limi
10 min read
Cauchy's Mean Value Theorem Cauchy's Mean Value theorem provides a relation between the change of two functions over a fixed interval with their derivative. It is a special case of Lagrange Mean Value Theorem. Cauchy's Mean Value theorem is also called the Extended Mean Value Theorem or the Second Mean Value Theorem.According
7 min read
Lagrange's Mean Value Theorem Lagrange's Mean Value Theorem (LMVT) is a fundamental result in differential calculus, providing a formalized way to understand the behavior of differentiable functions. This theorem generalizes Rolle's Theorem and has significant applications in various fields of engineering, physics, and applied m
9 min read
Rolle's Mean Value Theorem Rolle's theorem one of the core theorem of calculus states that, for a differentiable function that attains equal values at two distinct points then it must have at least one fixed point somewhere between them where the first derivative of the function is zero.Rolle's Theorem and the Mean Value Theo
8 min read
Mathematics | Unimodal functions and Bimodal functions Before diving into unimodal and bimodal functions, it's essential to understand the term "modal." A mode refers to the value at which a function reaches a peak, typically a maximum point. The behavior of functions can vary depending on how many peaks or modes they contain, giving rise to classificat
5 min read
Surface Area and Volume of Hexagonal Prism Given a Base edge and Height of the Hexagonal prism, the task is to find the Surface Area and the Volume of hexagonal Prism. In mathematics, a hexagonal prism is a three-dimensional solid shape which have 8 faces, 18 edges, and 12 vertices. The two faces at either ends are hexagons, and the rest of
5 min read