C语言实现牛顿插值公式

#include<stdio.h>
#include<stdlib.h>
int main() {
int x[10], n;
double y[10];
int ax;
double ay;
double e;
printf(“请输入x与y的对数n:”);
scanf_s("%d", &n);
printf(“请输入x:”);
scanf_s("%d", &ax);
ay = 0;
double t = 1;
double m=0;
int d = 1;
for (int i = 0; i < n; i++) {
printf(“请输入对应的x,y:”);
scanf_s("%d%lf", &x[i], &y[i]);
}
for (int c = 0; c <=n; c++) {
for (int k = 0; k < c; k++) {
for (int j = 0; j < c; j++) {
if (j != k)
t = t * (x[k] - x[j]);
}
if (c >1)
ay = ay + y[k] / t;
t = 1;
}if(c>=1)
for (int z = 0; z <c-1; z++) {

		e = ax - x[z];
		d *= e;
	}
	m+=ay * d;
	d = 1;
	ay = 0;
}
printf("%lf", m+y[0]);
system("pause");//visual studio的Hape操作
return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_41281130/article/details/88894186