📄 niudun.c
字号:
/* 这份源代码文件已被未注册的SourceFormatX格式化过 */
/* 如果您想不再添加此类信息,请您注册这个共享软件 */
/* 更多相关信息请访问网站: http://cn.textrush.com */
/////////////////////////////////////////////////////////////
// Purpose:(x_i,y_i)的牛顿插值多项式 //
/////////////////////////////////////////////////////////////
/**
*The result :
* Input n value: 2
*Now input the (x_i,y_i),i=0,...,2:
*11 0.190809 12 0.207912 13 0.224951
*Now input the x value: 11.5
*newton(11.500000)=0.199369
*请按任意键继续. . .
*/
#include <stdio.h>
#define MAX_N 20 //定义(x_i,y_i)的最大维数
typedef struct tagPOINT //点的结构
{
double x;
double y;
} POINT;
int main()
{
int n;
int i, j;
POINT points[MAX_N + 1];
double diff[MAX_N + 1];
double x, tmp, newton = 0;
printf("\n Input n value: "); //输入被插值点的数目
scanf("%d", &n);
if (n > MAX_N)
{
printf("The input n is larger than MAX_N, please redefine the MAX_N.\n");
return 1;
}
if (n <= 0)
{
printf("Please input a number between 1 and %d.\n", MAX_N);
return 1;
}
//输入被插值点(x_i,y_i)
printf("Now input the (x_i,y_i),i=0,...,%d:\n", n);
for (i = 0; i <= n; i++)
scanf("%lf%lf", &points[i].x, &points[i].y);
printf("Now input the x value: "); //输入计算牛顿插值多项式的x值
scanf("%lf", &x);
for (i = 0; i <= n; i++)
diff[i] = points[i].y;
for (i = 0; i < n; i++)
{
for (j = n; j > i; j--)
{
diff[j] = (diff[j] - diff[j - 1]) / (points[j].x - points[j - 1-i].x);
}
//计算f(x_0,...,x_n)的差商
}
tmp = 1;
newton = diff[0];
for (i = 0; i < n; i++)
{
tmp = tmp *(x - points[i].x);
newton = newton + tmp * diff[i + 1];
}
printf("newton(%f)=%f\n", x, newton); //输出
system("pause");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -