0% found this document useful (0 votes)
0 views

forward diff

The document provides a C++ code implementation of the Newton Forward Difference Method for interpolation. It includes functions for calculating factorials, inputting data points, constructing a forward difference table, and applying the interpolation formula. The program outputs the interpolated value for a given input x based on the provided data points.

Uploaded by

shubhyadav.ngp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

forward diff

The document provides a C++ code implementation of the Newton Forward Difference Method for interpolation. It includes functions for calculating factorials, inputting data points, constructing a forward difference table, and applying the interpolation formula. The program outputs the interpolated value for a given input x based on the provided data points.

Uploaded by

shubhyadav.ngp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

C++ Code for Newton Forward Difference Method.

#include <iostream>
#include <iomanip>
using namespace std;
// Function to calculate factorial
int factorial(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
int main() {
int n;
cout << "Enter number of data points: ";
cin >> n;
double x[n], y[n][n];
// Input x and y values
cout << "Enter values of x: ";
for (int i = 0; i < n; i++) {
cin >> x[i];
}
cout << "Enter values of y: ";
for (int i = 0; i < n; i++) {
cin >> y[i][0]; // First column is given y values
}

// Construct forward difference table


for (int j = 1; j < n; j++) {
for (int i = 0; i < n - j; i++) {
y[i][j] = y[i + 1][j - 1] - y[i][j - 1];
}
}
// Display forward difference table
cout << "\nForward Difference Table:\n";
for (int i = 0; i < n; i++) {
cout << setw(5) << x[i];
for (int j = 0; j < n - i; j++) {
cout << setw(10) << y[i][j];
}
cout << endl;
}
// Get the interpolation point
double xp;
cout << "\nEnter the value of x for interpolation: ";
cin >> xp;
// Apply Newton Forward Interpolation Formula
double h = x[1] - x[0];
double p = (xp - x[0]) / h;
double yp = y[0][0]; // First term of formula
double term = 1;
for (int i = 1; i < n; i++) {
term *= (p - (i - 1)) / i;
yp += term * y[0][i];
}
cout << "\nInterpolated value at x = " << xp << " is " << yp << endl;

return 0;
}

You might also like