You are on page 1of 4

#include<stdio.

h>
#include<math.h>
#define n 6

int main()
{
float x[n]={0.30,0.32,0.34,0.36,0.38,0.40},
y[n]={1.7596,1.7698,1.7804,1.7912,1.8024,1.8139},
f[20][20],a=0.33,h,u,p=1.00,sum;
int i,j,k;

h=x[1]-x[0];

//Calculating the forward difference table


for(i=0;i<n;i++)
{
f[i][0]=y[i];
}
for(j=1;j<n;j++)
{
for(i=0;i<n-j;i++)
{
f[i][j]=f[i+1][j-1]-f[i][j-1];
}
}
//Display the forward difference table
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
printf(" %0.4f",f[i][j]);
}
printf("\n");
}
//Cheaking noise level
printf("Enter the column where the noise level occurs, if not press 0:\n");
scanf("%d",&k);
if(k==0)
{
k=n;
}
else{
k=k-2;
}

//Calculating the value of u


u=(a-x[1])/h;
//Calculating the value of f(a) using Newton Forward Interpolation Formula
sum=y[1];
for(i=1;i<=k;i++)
{
p=p*(u-i+1);
sum=sum+(p*f[1][i])/tgamma(i+1);
}

printf("f(%0.2f)=%0.4f(correct to 4DP)",a,sum);
return 0;
}
/* Output
1.7596 0.0102 0.0004 -0.0002 0.0004 -0.0007
1.7698 0.0106 0.0002 0.0002 -0.0003
1.7804 0.0108 0.0004 -0.0001
1.7912 0.0112 0.0003
1.8024 0.0115
1.8139
Enter the column where the noise level occurs, if not press 0:
3
f(0.33)=1.7751(correct to 4DP)
*/

/*NABANNA GHOSH
MTUG/020/20
Lagrange's interpolation rule*/

#include<stdio.h>
#include<math.h>
#define n 4
int main()
{
float x[20]={2.0,3.0,5.0,7.0},y[20]={0.301,0.477,0.699,0.845},
k=3.5,wx=1.0,w[20],sum=0.0;

int i,j;

// calculating w(x),w'(x_i)
for(j=0;j<n;j++)
{
wx=wx*(k-x[j]);
w[j]=1.0;
for(i=0;i<n;i++)
{
if(i!=j)
w[j]=w[j]*(x[j]-x[i]);
}
}

//calculating f(x)
for(i=0;i<n;i++)
{
sum=sum+((wx)/((k-x[i])*w[i]))*y[i];
}
printf("\n f(%0.1f) = %0.4f",k,sum);
return 0;
}

/* OUTPUT

f(3.5) = 0.5460

*/

#include<stdio.h>
int main()
{
float x[20]={0.30,0.32,0.34,0.36,0.38,0.40},
y[20]={1.7596,1.7698,1.7804,1.7912,1.8024,1.8139},f[20][20],sum,xn=0.40,k,h=0.02,u,term;
int n=6,m,i,j;
n=n-1;
printf("\nInput x for which interpolation is required\n");
scanf("%f",&k);
for(i=0;i<=n;i++)
f[i][0]=y[i];
for(j=1;j<=n;j++)
{
for(i=j;i<=n;i++)
f[i][j]=f[i][j-1]-f[i-1][j-1];
}
printf("\nDiffernce Table is\n");
for(i=0;i<=n;i++)
{
for(j=0;j<=i;j++)
printf("%0.4f ",f[i][j]);
printf("\n");
}
printf("\nEnter the column number in which noise level appears otherwise press 0\n");
scanf("%d",&m);
if(m==0)
m=n;
else
m=m-1;
u=(k-xn)/h;
sum=y[n];
term=u;
for(j=1;j<=m;j++)
{
sum=sum+term*f[n][j];
term=term*(u+j)/(j+1);
}
printf("y(%0.2f)=%0.4f\n",k,sum);
}

/* OUTPUT

Input x for which interpolation is required


0.39

Differnce Table is
1.7596
1.7698 0.0102
1.7804 0.0106 0.0004
1.7912 0.0108 0.0002 -0.0002
1.8024 0.0112 0.0004 0.0002 0.0004
1.8139 0.0115 0.0003 -0.0001 -0.0003 -0.0007

Enter the column number in which noise level appears otherwise press 0
3
y(0.39)=1.8081

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

*/

You might also like