Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

NAME-AKSHAY BORA

ROLL NO.-7682
EXPERIMENT NO.-8
QUESTION : NETWON'S BACKWARD INTERPOLATION
PROGRAM:

#include<iostream>
#include<math.h>
#include<iomanip>
#include<stdlib.h>
using namespace std;
float cal(float u, int n)
{
float temp = u;
for (int i = 1; i < n; i++)
temp = temp * (u + i);
return temp;
}
int fact(int n)
{
int f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
}
int main()
{
int n,i,j,k,x0;
float h,b;
cout<<"How many Data points you want to enter?\n\tn: ";cin>>n;
float x[i],y[n][n];
cout<<"Enter the x values such that there is a fixed stepsize ."<<endl;
cout<<"The stepsize for x values:\t";cin>>h;
cout<<"The First x value is:\t";cin>>x0;
cout<<"Enter the x value and corresponding y value: "<<endl;
for (i=0;i<n;i++)
{ x[i]=x0+i*h;
cout<<"\n\tx["<<i<<"]="<<x[i];
cout<<"\ty["<<i<<"]=";cin>>y[i][0];
}
for(i=1;i<n;i++)
{
for(j=n-1;j>i-1;j--)
{
y[j][i] = y[j][i-1] - y[j-1][i-1];
}
}
cout<<endl<<" THE BACKWARD DIFFERENCE TABLE IS:- \n" << endl;
for(i=0;i<n; i++)
{
cout<<x[i]<<"\t";
for(j=0;j<=i;j++)
{
cout << setw(6)<< y[i][j]<<setw(6)<<"\t";
}
cout << endl;
}
cout<<"Give the point for interpolation :\t";cin>>b;
float sum = y[n-1][0];
float u = (b - x[n-1]) / (x[1] - x[0]);
for (int i = 1; i < n; i++)
{
sum = sum + (cal(u, i) * y[n-1][i]) /fact(i);
}
cout << "\n Value at " << b << " is " << sum << endl;
return 0;
}
RESULT:
This Program gives the BACKWARD interpolation.
How many Data points you want to enter?
n: 5
Enter the x values such that there is a fixed stepsize .
The stepsize for x values: 1
The First x value is: 1
Enter the x value and corresponding y value:
x[0]=1 y[0]=1
x[1]=2 y[1]=4
x[2]=3 y[2]=9
x[3]=4 y[3]=16
x[4]=5 y[4]=25
THE BACKWARD DIFFERENCE TABLE IS:-
1 1
2 4 3
3 9 5 2
4 16 7 2 0
5 25 9 2 0 0
Give the point for interpolation : 4.5
Value at 4.5 is 20.25
--------------------------------
Process exited after 28.67 seconds with return value 0
Press any key to continue . . .

You might also like