Develop a C-code to meet the requirements below: Fit polynomials to the data usi
ID: 3579829 • 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: (1) use of arrays, (2) use of pointers, (3) use of structure, (4) use of union, (5) use of functions and function calls, (6) formatted output on screen and (7) 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
#include<stdio.h>
//#include<conio.h>
#include<math.h>
// structure to hold x and y values
struct t{
float x;
float y;
};
typedef struct t table;
int main()
{
int i,j,k,n;
float d[10][10],X,prod=1.0,u,fact=1.0,sum=0.0;
//clrscr();
printf("Enter the size: ");
scanf("%d",&n);
// table structure
table values[n];
printf("Enter the value of x for which y has to be calculated: ");
scanf("%f",&X);
for(i=1;i<=n;i++)
{
printf("Enter x[%d]: ",i);
scanf("%f",&values[i].x);
printf("Enter y[%d]: ",i);
scanf("%f",&values[i].y);
}
i=2;
while(X>values[i].x){
i=i+1;
}
k=i-1;
// using the formula
u=(X-values[k].x)/(values[k+1].x-values[k].x);
// building the difference table
printf("Difference Table: ");
for(j=1;j<=(n-1);j++)
{
for(i=1;i<=(n-j);i++)
{
if(j==1)
d[i][j]=values[i+1].y-values[i].y;
else
d[i][j]=d[i+1][j-1]-d[i][j-1];
}
}
// displaying the difference table
for(j=1;j<=(n-1);j++)
{
for(i=1;i<=(n-j);i++)
{
printf("%f ",d[j][i]);
}
printf(" ");
}
sum=values[k].y;
// calculating value of Y for given X.
for(i=1;i<=(n-1);i++)
{
prod=1.0;
for(j=0;j<=(i-1);j++)
prod=prod*(u-j);
fact=1.0;
for(j=1;j<=i;j++)
fact=fact*j;
sum=sum+((prod*d[k][i])/fact);
}
printf("Interpolated value=%f",sum);
// getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.