利用lagrange插值法计算函数值

博客给出了一组x和y的值,要求计算f(13)的值,并提供了用C语言编写的求解代码。代码通过用户输入n的个数、已知的x和y值以及要求的x值,最终计算并输出对应的y值。

x: 10 15 20
y: 1 1.1761 1.3010
求f(13);

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

void main( void )
{
intn;
float*x = NULL;
float*y = NULL;
floatxFound;
floatyGet = 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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值