You are on page 1of 4

Experiment No: 04

Experiment Name: Write a program Implementation of Newton Forward Interpolation.


Aim: 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 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..
Algorithm:
1. Start

2. Read number of data (n)

3. Read data points for x and y:

For i = 0 to n-1
Read Xi and Yi,0
Next i

4. Read calculation point where derivative is required (xp)

5. Set variable flag to 0

6. Check whether given point is valid data point or not.


If it is valid point then get its position at variable index

For i = 0 to n-1

If |xp - Xi| < 0.0001


index = i
flag = 1
break from loop
End If

Next i

7. If given calculation point (xp) is not in


x-data then terminate the process.

If flag = 0
Print "Invalid Calculation Point"
Exit
End If
1
8. Generate forward difference table

For i = 1 to n-1

For j = 0 to n-1-i
Yj,i = Yj+1,i-1 - Yj,i-1
Next j

Next i

9. Calculate finite difference: h = X1 - X0

10. Set sum = 0 and sign = 1

11. Calculate sum of different terms in formula


to find derivatives using Newton's forward
difference formula:

For i = 1 to n-1-index
term = (Yindex, i)i / i
sum = sum + sign * term
sign = -sign
Next i

12. Divide sum by finite difference (h) to get result

first_derivative = sum/h

13. Display value of first_derivative

14. Stop
Program Cord:
#include <bits/stdc++.h>
using namespace std;

// calculating u mentioned in the formula


float u_cal(float u, int n)
{
float temp = u;
for (int i = 1; i < n; i++)
temp = temp * (u - i);
return temp;
2
}

// calculating factorial of given number n


int fact(int n)
{
int f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
}

int main()
{
// Number of values given
int n = 4;
float x[] = { 45, 50, 55, 60 };

// y[][] is used for difference table


// with y[][0] used for input
float y[n][n];
y[0][0] = 0.7071;
y[1][0] = 0.7660;
y[2][0] = 0.8192;
y[3][0] = 0.8660;

// Calculating the forward difference


// table
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++)
y[j][i] = y[j + 1][i - 1] - y[j][i - 1];
}

// Displaying the forward difference table


for (int i = 0; i < n; i++) {
cout << setw(4) << x[i]
<< "\t";
for (int j = 0; j < n - i; j++)
cout << setw(4) << y[i][j]
<< "\t";
3
cout << endl;
}

// Value to interpolate at
float value = 52;

// initializing u and sum


float sum = y[0][0];
float u = (value - x[0]) / (x[1] - x[0]);
for (int i = 1; i < n; i++) {
sum = sum + (u_cal(u, i) * y[0][i]) /
fact(i);
}

cout << "\n Value at " << value << " is "
<< sum << endl;
return 0;
}
Output:
45 0.7071 0.0589 -0.00569999 -0.000699997
50 0.766 0.0532 -0.00639999
55 0.8192 0.0468
60 0.866

Value at 52 is 0.788003

...Program finished with exit code 0


Press ENTER to exit console.

You might also like