Program:: Name-Akshay Bora ROLL NO.-7682 Experiment No.-8 Question: Netwon'S Forward Interpolation

You might also like

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

NAME-AKSHAY BORA

ROLL NO.-7682
EXPERIMENT NO.-8
QUESTION : NETWON'S FORWARD INTERPOLATION
PROGRAM:
#include<iostream>
#include<iomanip>
#include<math.h>
#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;
float h,b,x0;
cout<<"\t\t This Program gives the FORWARD interpolation.\n\n";
cout<<"How many Data points you want to enter?\n\tn: ";cin>>n;
float x[i],y[n][n];
cout<<"USE the values of x such that there is a FIXED STEPSIZE."<<endl;
cout<<"\nThe stepsize for x values:\t";cin>>h;
cout<<"The First x value is:\t";cin>>x0;
cout<<"\nEnter 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=0;j<=n-i;j++)
{
y[j][i]=y[j+1][i-1]-y[j][i-1];
}
}
cout<<"\tForward Difference Table for the given Data is : "<<endl;
for (i=0;i<n;i++)
{
cout<<x[i];
for(j=0;j<n-i;j++)
{
cout<<setw(10)<<y[i][j]<<setw(10)<<"\t";
}
cout<<endl;
}
cout<<"\n\nGive the point for interpolation :\t";cin>>b;
float sum = y[0][0];
float u = (b - x[0]) / (x[1] - x[0]);
for (int i = 1; i < n; i++)
{
sum = sum + (cal(u, i) * y[0][i]) /fact(i);
}
cout << "\nThe Value at " << b << " using Forward interpolation method is : " <<
sum << endl;
return 0;
}
RESULT:
This Program gives the FORWARD interpolation.

How many Data points you want to enter?


n: 5
USE the values of x such that there is a FIXED STEPSIZE.

The stepsize for x values: 0.05


The First x value is: 0.1

Enter the x value and corresponding y value:

x[0]=0.1 y[0]=0.1003

x[1]=0.15 y[1]=0.1511

x[2]=0.2 y[2]=0.2027

x[3]=0.25 y[3]=0.2553

x[4]=0.3 y[4]=0.3093
Forward Difference Table for the given Data is :
0.1 0.1003 0.0508 0.000800014 0.000199959 0.000200108
0.15 0.1511 0.0516 0.000999972 0.000400066
0.2 0.2027 0.0526 0.00140004
0.25 0.2553 0.054
0.3 0.3093

Give the point for interpolation : 0.12

The Value at 0.12 using Forward interpolation method is : 0.120528


--------------------------------
Process exited after 41.92 seconds with return value 0
Press any key to continue . . .

You might also like