Develop a C-code to meet the requirements below: Fit polynomials to the data usi
ID: 3579876 • Letter: D
Question
Develop a C-code to meet the requirements below:
Fit polynomials to the data using Gregory-Newton interpolation
Your C-code must contain the following features: use of arrays, use of pointers, use of structure, use of union, use of functions and function calls, formatted output on screen and saving of the same output on a file.
Choice 2 (last name with first letter C to F): For the data showed bellow 3.5 9.1 1.8 11.0 5.2 7.7 y (x) 3.3 7.0 16.1 33.2 60.4 99.8 Develop a C-code to meet the requirements below:Explanation / Answer
Answer
#include<stdio.h>
#define Max_N 100
#define OrderOfDiff 4
void main ()
{
float arr_X[Max_N+1], arr_Y[Max_N+1], numerator=1.0, denominator=1.0, x, y, p, h, diff_table[Max_N+1][OrderOfDiff +1];
int i,j,n,k;
printf("==============================================");
printf("Pleae enter the value of n ");
scanf("%d",&n);
printf("Enter the values of x and y");
for(i=0; i<=n; i++)
scanf("%f%f", &arr_X[i], &arr_Y[i]);
printf("Enter the value of X at which value of Y is to be calculated");
scanf("%f", &x);
h=arr_X[1]-arr_X[0];
for(i=0; i<=n-1; i++)
diff_table[i][1]=arr_Y[i+1]-arr_Y[i];/*here we are creating the difference table and calculating the first order differences*/
for(j=2; j<=OrderOfDiff ; j++)/*here we are calculating higher order differences*/
for(i=0; i<=n-j; i++)
diff_table[i][j]=diff_table[i+1][j-1] - diff_table[i][j-1];
i=0;
while(!(arr_X[i]>x)) /* Finding x0 */
i++;
i--;
p=(x-arr_X[i])/h;
y=arr_Y[i];
for (k=1; k<=OrderOfDiff ; k++)
{
numerator *=p-k+1;
denominator *=k;
y +=(numerator/denominator)*diff_table[i][k];
}
printf("When x=%6.1f, y=%6.2f ",x, y);
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.