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

EXPERIMENT NO.

10
CALCULATE THE INTERPOLATING/EXTRAPOLATING VALUES OF A FUNCTION USING NEWTONS
BACKWARD METHOD.
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
void main()
{
clrscr();
float ax[10],ay[10],diffs[20][30],x,nr=1.0,dr=1.0,p=0.0,h=0.0,yp=0.0;
int n,I,j,k;
cout<<enter the no. of terms :\t;
cin>>n;
cout<<enter the initial value of x :;
cin>>ax[0];
cout<<enter the value of h :;
cin>>h;
for(i=1;i<n;i++)
ax[i]=ax[0]+(i*h);
cout<<enter the values y:\n;
for(i=0;i<n;i++)
{
cin>>ay[i];
}
cout<<endl<<x << y<<endl;
for(i=0;i<n;i++)
{
cout<<ax[i]<< <<ay[i]<<endl;
}
cout<<\nenter the value of x for which y is to be found: ;
cin>>x;
for(i=0;i<n-1;i++)
{
diffs[i][1]=ay[i+1]-ay[i];
cout<<setprecision(2)<<diffs[i][1]<<\t;
}
cout<<DEL;
cout<<endl;
for(j=2;j<n;j++)
{
for(i=0;i<n-j;i++)

{
diffs[i][j]=diffs[i+1][j-1]-diffs[i][j-1];
cout<<setprecision(2)<<diffs[i][j]<<\t;
}
cout<<DEL<<j;
cout<<endl;
}
for(i=0;i<n;i++)
{
if(ax[i]<x)
continue;
}
i--;
p=(x-ax[i])/h;
yp=ay[i];
for(k=1;k<n;k++)
{
nr*=p+k-1;
dr*=k;
for(j=n-k-1;j>=0;j--)
{
yp+=(nr/dr)*diffs[j][k];
break;
}
}
cout<<\nwhen x= <<setw(6)<<x<<\ty=<<setw(6)<<setprecision(4)<<yp<<endl;
getch();
}

OUTPUT

EXTRAPOLATION:

INTERPOLATION:

You might also like