/* The following C program implements Gauss Central Difference Formula in
estimating functional values near the middle part of a given table. The tabular
values (abscissa and ordinate) are read as input. */
#include<stdio.h>
#include<conio.h>
#define max 30
void main()
{
float table[max][max];
int count, mid, i, j;
float x, n, h, y, p ;
/* Setting all the contents of the table to zero */
for(i=0; i<max; i++)
for(j=0; j<max; j++)
table[i][j]=0;
printf("\n How many values are to be entered? \n");
scanf("%d",&count);
printf("\n Enter the values of x and y in tabular format: \n\n");
for(i=0; i<=2*count-2; i+=2)
for(j=0; j<=1; j++)
scanf("%f",&table[i][j]) ;
printf("\n Find the value of y at x = ? \n");
scanf("%f",&x);
/* constructing the central difference table */
for(i=2; i<=count; i++)
for(j=i-1, n=1; n<=count+1-i; j+=2, n++)
{
table[j][i]=table[j+1][i-1] - table[j-1][i-1];
}
/* Finding the middle of the x column of the table */
if(count%2==1)
mid=count-1;
else
mid=count;
h=table[mid+2][0] - table[mid][0] ;
n=p=(x-table[mid][0])/h;
/* Using interpolation formula */
if(x>table[mid][0])
{
printf("\n APPLYING GAUSS FORWARD CENTRAL
DIFFERENCE FORMULA \n");
y=table[mid][1] + n*table[mid+1][2];
for(i=3; i<=count; i++)
if(i%2==1)
{
n*=(p-(int)i/2)/(i-1);
y+=n*table[mid][i];
}
else
{
n*=(p+(int)i/2 -1)/(i-1);
y+=n*table[mid+1][i];
}
}
else
{
printf("\n------ APPLYING GAUSS BACKWARD CENTRAL DIFFERENCE FORMULA ------\n");
y=table[mid][1] + n*table[mid-1][2];
for(i=3; i<=count; i++)
if(i%2==1)
{
n*=(p+(int)i/2)/(i-1);
y+=n*table[mid][i];
}
else
{
n*=(p-(int)i/2 +1)/(i-1);
y+=n*table[mid-1][i];
}
}
/* printing the central difference table */
printf("\n CENTRAL DIFFERENCE TABLE \n");
for(i=0; i<=2*count-2; i++)
{
if(i%2==0)
printf("%2.3f %2.3f",table[i][0], table[i][1]);
else
printf(" ");
for(j=2; j<=count; j++)
if((j%2==0 && i%2==1) || (j%2==1 && i%2==0))
{
if(i>=j-1 && i<2*count-j)
printf(" %2.3f",table[i][j]);
}
else
printf(" ");
printf("\n");
}
printf("\n\n At x=%f, y=%f \n",x,y);
getch();
}
SAMPLE OUTPUT:
Evaluating the value of y at x = 0.425, given the following table of values:
x | 0.0 | 0.2 | 0.4 | 0.6 | 0.8 |
y | 0.0000 | 0.1823 | 0.3365 | 0.4700 | 0.5878 |
this program seems to be wrong when i input the table:
ReplyDelete100 150 200 250 300 350 400
10.63 13.03 15.04 16.81 18.42 19.90 21.27
output should be 15.70 and i get -37