Home » Numerical Techniques Using C++ » Newton Forward Difference Interpolation C++ Example Formula

# Newton Forward Difference Interpolation C++ Example Formula

## Newton Forward Difference Interpolation C++

Newton’s forward difference interpolation is used when the function is tabulated at equal intervals. If the data point to be interpolated lies in the upper half or in the beginning of the table then the Newton’s forward difference interpolation is used because it gives the better approximation.

In order to interpolate at any point (say x) between X0 and Xn, Newton’s forward interpolation formula takes the form,

## Formula  y(x)=(y0+u.∆y0+(u(u-1) ∆^2 y0)) / (2!+(u(u-1)(u-2) ∆^3 y0)) / (3!+⋯+(u(u-1)(u-2)…(u-n-1) ∆^n y0)) / n!

Where,

‘h’ is the interval between data points

’∆‘ is the forward difference symbol

## C++ Program Newton Forward Difference

```/* info: https://wikkihut.com/wp-content/plugins/syntax-highlighting-code-block/vendor/scrivo/highlight-php/styles/atom-one-dark-reasonable.css */
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```//Program: Newton's Forward Difference interpolation
//wikkihut.com

#include<iostream>
#include<bits/stdc++.h>		//for specifying format
using namespace std;

int factorial(int n){
int fact=1;
while(n){

fact=fact*n;
n--;
}
return fact;
}

int main(){
float x,y,p,diff;
float X,f,f2=0,u,h;
int i, j=1,n,k=1;
cout<<"Enter the number of observations: ";
cin>>n;
cout<<"\nEnter the values of xi's and f(xi's)\n";
for(i=1; i<=n; i++){
cout<<"x["<<i<<"]: ";
cin>>x[i];
cout<<"f["<<i<<"]: ";
cin>>y[i];
}

cout<<"\nx[i's]\tf[i's]\n";
for(i=1; i<=n; i++){
cout<<x[i]<<"\t"<<y[i]<<"\n";
}
cout<<"\nEnter the value of x at which you want to interpolate: ";
cin>>X;
int m=n;
h=x-x;
u=(X-x)/h;
//saving (y) because the array y[] changes and we have add it at the last result
f=y;
cout<<fixed<<setprecision(2);
cout<<"Forward Difference table for the given set of points is\n";
do{
cout<<"diff.("<<j++<<") f(x)\n";
for(i=1; i<n; i++){
p[i]=( ( y[i+1]-y[i] ) );
cout<<p[i]<<"\n";
//this assignment is done because we are computing the new difference array using previous one (delta f(x)'s)
y[i]=p[i];
}
diff[k++]=p;
n--;
cout<<"\n";
}while(n!=1);

float df=u;
float l=1;
for(int i=1; i<m; i++ ){
if(i>1)
df=df/factorial(i);

f2=f2+(df*diff[i]);
for(int j=0; j<=i; j++){
l=l*(u-j);
}
df=l;
}
f=f+f2;
cout<<"\nf("<<X<<") = "<<f;
return 0;
}
``````

## Formation of Forward Difference table

Before studying the Newton’s Forward Difference interpolation, we should know how to form a forward difference table. Therefore, first we will see how to form a forward difference table.

### General Example

Consider the following general set of data points.

The forward difference table for the above set of data points is given below:

example,

## Example Newton Forward Difference Interpolation

Q. Consider the following set of data points and form a forward difference table.

Ans. The forward difference table for the given set of data points is given below:

Now we are ready to learn the Newton’s Forward Difference interpolation.

Newton’s forward difference interpolation Example

Q. Given the set of data points as:

Find y (2.35)?

Ans. We have,

x = 2.35,

h = x1 – x0 = 2.25 – 2.0 = 0.25

u = (x-x0)/h = (2.35-2.0)/0.25 = 0.35/0.25 = 1.4

Now, the forward difference table is as:

= 10.484 + 0.0364 + 0.00056 + 0.000448                         = 10.52141