/*CSEMATTER.BLOGSPOT.IN
TO IMPLEMENT GAUSS FORWARD INTERPOLATION*/
#include<stdio.h>
#include<conio.h>
int fact(int a)
{
if(a==0)
{
return(1);
}
else
return(a*fact(a-1));
}
void main()
{
int n,i,j,k;
float ax[6],ay[6],x,val,diff[6][6],h,xs,p,res,y1,y2,y3,y4;
clrscr();
printf("enter the value of n(1 to 6)\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the value of x:\n");
scanf("%f",&ax[i]);
printf("\nenter the value of y:\n");
scanf("%f",&ay[i]);
}
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
if(j==0)
diff[i][j]=ay[i];
else if(diff[i+1][j-1]!=NULL)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
printf("\nenter the value of x to find corresponding y:\n");
scanf("%f",&val);
for(i=0;i<n;i++)
{
if(ax[i]>val)
{
break;
}
xs=ax[i];
k=i;
}
h=ax[1]-ax[0];
p=(val-xs)/h;
y1=p*diff[i][1];
y2=(p*(p-1)*diff[i-1][2])/fact(2);
y3=((p+1)*p*(p-1)*diff[i-2][3])/fact(3);
y4=((p+1)*p*(p-1)*(p-2)*diff[i-3][4])/fact(4);
res=ay[k]+y1+y2+y3+y4;
printf("the result is %f",res);
getch();
}
OUTPUT:
enter the value of n(1 to 6)
5
enter the value of x: 21
enter the value of y: 18.4708
enter the value of x: 25
enter the value of y: 17.8144
enter the value of x: 29
enter the value of y: 17.1070
enter the value of x: 33
enter the value of y: 16.3432
enter the value of x: 37
enter the value of y: 15.5154
enter the value of x to find corresponding y: 30
the result is 16.9216
TO IMPLEMENT GAUSS FORWARD INTERPOLATION*/
#include<stdio.h>
#include<conio.h>
int fact(int a)
{
if(a==0)
{
return(1);
}
else
return(a*fact(a-1));
}
void main()
{
int n,i,j,k;
float ax[6],ay[6],x,val,diff[6][6],h,xs,p,res,y1,y2,y3,y4;
clrscr();
printf("enter the value of n(1 to 6)\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the value of x:\n");
scanf("%f",&ax[i]);
printf("\nenter the value of y:\n");
scanf("%f",&ay[i]);
}
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
if(j==0)
diff[i][j]=ay[i];
else if(diff[i+1][j-1]!=NULL)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
printf("\nenter the value of x to find corresponding y:\n");
scanf("%f",&val);
for(i=0;i<n;i++)
{
if(ax[i]>val)
{
break;
}
xs=ax[i];
k=i;
}
h=ax[1]-ax[0];
p=(val-xs)/h;
y1=p*diff[i][1];
y2=(p*(p-1)*diff[i-1][2])/fact(2);
y3=((p+1)*p*(p-1)*diff[i-2][3])/fact(3);
y4=((p+1)*p*(p-1)*(p-2)*diff[i-3][4])/fact(4);
res=ay[k]+y1+y2+y3+y4;
printf("the result is %f",res);
getch();
}
OUTPUT:
enter the value of n(1 to 6)
5
enter the value of x: 21
enter the value of y: 18.4708
enter the value of x: 25
enter the value of y: 17.8144
enter the value of x: 29
enter the value of y: 17.1070
enter the value of x: 33
enter the value of y: 16.3432
enter the value of x: 37
enter the value of y: 15.5154
enter the value of x to find corresponding y: 30
the result is 16.9216
No comments:
Post a Comment