利用lagrange插值法计算函数值

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

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);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有