#include<iostream>
#define N 4
using namespace std;
int main()
{
float ax[10],ay[10],dtab[10][N+1],x,h,p,y;
float y0,y1,y2,y3,y4;
int i,j,k,n;
cout<<"Enter Value of n: ";
cin>>n;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
dtab[i][j]=0.0;
cout<<"Enter Value for X and Y respectively\n";
cout<<"X Y\n";
for(i=0;i<n;i++)
cin>>ax[i]>>ay[i];
cout<<"Enter the value of X for which Y is Required: ";
cin>>x;
h=ax[1]-ax[0];
for(i=0;i<=n-1;i++)
dtab[i][1]=ay[i+1]-ay[i];
for(j=2;j<=N;j++)
{
for(i=0;i<n-j;i++)
dtab[i][j]=dtab[i+1][j-1]-dtab[i][j-1];
}
for(i=0;x>ax[i];i++);
i--;
p=(x-ax[i])/h;
y0=ay[i];
y1=p*dtab[i][1];
y2=p*(p-1)*dtab[i-1][2]/2;
y3=(p+1)*p*(p-1)*dtab[i-1][3]/6;
y4=(p+1)*p*(p-1)*(p-2)*dtab[i-2][4]/24;
y=y0+y1+y2+y3+y4;
cout<<"\nFor X="<<x<<" Y="<<y;
return 0;
}
#define N 4
using namespace std;
int main()
{
float ax[10],ay[10],dtab[10][N+1],x,h,p,y;
float y0,y1,y2,y3,y4;
int i,j,k,n;
cout<<"Enter Value of n: ";
cin>>n;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
dtab[i][j]=0.0;
cout<<"Enter Value for X and Y respectively\n";
cout<<"X Y\n";
for(i=0;i<n;i++)
cin>>ax[i]>>ay[i];
cout<<"Enter the value of X for which Y is Required: ";
cin>>x;
h=ax[1]-ax[0];
for(i=0;i<=n-1;i++)
dtab[i][1]=ay[i+1]-ay[i];
for(j=2;j<=N;j++)
{
for(i=0;i<n-j;i++)
dtab[i][j]=dtab[i+1][j-1]-dtab[i][j-1];
}
for(i=0;x>ax[i];i++);
i--;
p=(x-ax[i])/h;
y0=ay[i];
y1=p*dtab[i][1];
y2=p*(p-1)*dtab[i-1][2]/2;
y3=(p+1)*p*(p-1)*dtab[i-1][3]/6;
y4=(p+1)*p*(p-1)*(p-2)*dtab[i-2][4]/24;
y=y0+y1+y2+y3+y4;
cout<<"\nFor X="<<x<<" Y="<<y;
return 0;
}
No comments:
Post a Comment