Tuesday, February 01, 2011

Demonstration of Newtons Forward Interpolation Method


#include 'stdio.h'

float table[10][12];
int coordinates , i=0, j=0;
float sum =0.0 ;
int del;
int mark;
float p,x,y;

void calculate() {
while(1) {
printf("\n Enter a X-coordinate near the starting region .(Enter 99 to terminate) ....");
scanf("%f",&x);
if(x==99)
break;
p =  (x - table[0][0])/(table[1][0] - table[0][0]);
y = table[0][1] + p*table[0][2] + (p*(p-1)/2.0)*table[0][3] + (p*(p-1)*(p-2)/6.0)*table[0][4] + (p*(p-1)*(p-2)*(p-3)/24.0)*table[0][5] ;
printf("\n Calculated approximate value of Y(%f) is %f",x,y);
}
return ;
}

void printTable() {
/* PRINTING THE TABLE */
printf("\n\n\t THE FORWARD DIFFERENCE TABLE \n\n");
 for(i=0; i< coordinates ; i++) {
              for(j=0; j< mark-i ; j++) {
                         printf("    %f", table[i][j]);}
                                      printf("\n");}
calculate();
return;
}

void main() {
printf("\tEnter the number of coordinates ... \n\n");
scanf("%d",&coordinates);
for(i=0;i<10;i++) {
 for(j=0;j<10;j++) {
            table[i][j]=0.00;
            }}

for(i=0; i< coordinates ; i++) {
printf("\n\tEnter X(%d) and Y(%d) ... ",i,i);
scanf("%f %f",&table[i][0],&table[i][1]);
}
/* CALCULATING TABLE ELEMENTS */
for(del=2 ; del<=coordinates ; del++) {
 sum = 0.0;
              for(i=0; i<=(coordinates-(del-1)) ; i++) {
                        table[i][del] = table[i+1][del-1] - table[i][del-1] ;
                                    sum = sum + table[i][del] ;}
                                                  if(sum==0) {
                                                            mark = del;
                                                            printTable();
                                                            break;  }  }
if(sum !=0) {
mark = del;
printTable();}
}

Output:

No comments:

Post a Comment

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.