利用lagrange插值法计算函数值

类别:软件工程 点击:0 评论:0 推荐:

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

本文地址:http://com.8s8s.com/it/it35385.htm