综合

img hongweijin

利用lagrange插值法计算函数值

发表于2004/9/30 15:45:00  2516人阅读

分类: Code Set

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

阅读全文
0 0

相关文章推荐

img
取 消
img