您的位置:首页 > 其它

利用lagrange插值法计算函数值

2004-09-30 15:45 260 查看
x: 10 15 20
y: 1 1.1761 1.3010
求f(13);

答案:
#include <stdio.h>
#include <stdlib.h>

void main( void )
{
int n;
float *x = NULL;
float *y = NULL;
float xFound;
float yGet = 0.0;
float yGetTemp = 1.0;

printf("请输入你要输入的n的个数:");
scanf("%d", &n);

printf("请输入已知x的值(如:10 29 23 ……): ");
x = (float *)malloc(sizeof(float) * n);
for (int client = 0; client < n; client++)
scanf("%f", (x + client));

printf("/n");

printf("请输入已知Y的值(如:23 23 23 ……): ");
y = (float *)malloc(sizeof(float) * n);
for (client = 0; client < n; client++)
scanf("%f", (y + client));

printf("/n请输入要求的x的值: ");
scanf("%f", &xFound);

int temp;
for (client = 0; client < n; client++)
{
for (temp = 0; temp < n; temp++)
{
if (temp == client)
continue;
else
yGetTemp *= ( (xFound - *(x + temp)) / ( *(x + client) - *(x + temp)));
}

yGet += (yGetTemp * (*(y + client)));
yGetTemp = 1.0;
}
printf("/nThe result is:%f/n", yGet);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐