拉格朗日插值法 C语言实现

  1. /* 
  2. *作者:KDF5000 
  3. *功能:利用拉格朗日插值法求解近似值 
  4. *时间:2013.4.15 
  5. */  
  6. #include <stdio.h>  
  7. #include <stdlib.h>  
  8. #include <string.h>  
  9. //存放插值节点  
  10. struct Data{  
  11.     double x;  
  12.     double y;  
  13.     struct Data *next;  
  14. };  
  15. /**************************************************** 
  16. *LagrangeInsert() 
  17. *功能:拉格朗日插值法 
  18. *****************************************************/  
  19. double LagrangeInsert(struct Data *header,double x)  
  20. {  
  21.     Data *pi,*pj,*p;  
  22.     pi=pj=header->next;  
  23.     double temp1,temp2;  
  24.     temp1=0;                //记录内循环的积  
  25.     temp2=1;                //记录外循环的和  
  26.     while(pi!=NULL)  
  27.     {  
  28.         while(pj!=NULL)  
  29.         {  
  30.             if(pi!=pj)  
  31.                 temp2 *=(x-pj->x)/(pi->x-pj->x);  
  32.             pj = pj->next;  
  33.         }  
  34.         temp1 +=temp2*pi->y;  
  35.         temp2=1;  
  36.         pj = header->next;  
  37.         pi = pi->next;  
  38.     }  
  39.     return temp1;   //返回计算结果  
  40. }  
  41.   
  42. void main()  
  43. {  
  44.     Data *header = (Data *)malloc(sizeof(Data));  
  45.     char str[20];  
  46.     Data *p,*newData;  
  47.     char strx[20],stry[20];  
  48.     double x;  
  49.   
  50.     p=header;  
  51.     p->x=0;  
  52.     p->y=0;  
  53.     p->next=NULL;  
  54.   
  55.     //输出提示信息  
  56.     printf("*******************************************\n");  
  57.     printf("使用说明:\n1.用户输入插值点,每一行输入一组:x y;\n2.输入换行表示输入结束。\n");  
  58.     printf("*******************************************\n");  
  59.     printf("x    y\n");  
  60.   
  61.     //接收用户输入知道第一次输入非换行为止  
  62.     memset(str,0,sizeof(str));  
  63.     while(strlen(str)==0)  
  64.         gets(str);  
  65.     //数据输入完毕,输入换行结束输入  
  66.     while(strlen(str)!=0)  
  67.     {  
  68.         newData = (Data *)malloc(sizeof(Data));  
  69.         sscanf(str,"%s%s",strx,stry);     //获取输入的前两个字符串 第一个为x,第二个为y  
  70.         newData->x = strtod(strx,NULL);   //将输入转换成浮点数  
  71.         newData->y = strtod(stry,NULL);     
  72.         newData->next=NULL;  
  73.         p->next=newData;  
  74.         p = p->next;  
  75.         gets(str);  
  76.     }  
  77.     printf("请输入要计算的x值:");  
  78.     scanf("%lf",&x);  
  79.     printf("L(%f) = %f\n",x,LagrangeInsert(header,0.20));  
  80.     return ;  
[cpp]  view plain  copy
 print ?
  1. }  

2
C语言实现拉格朗日插值法的思路如下: 首先,定义一个结构体或数组来存储已知点的横纵坐标,可以命名为`Point`结构体,包含`x`和`y`两个成员。 ```c typedef struct { double x; double y; } Point; ``` 然后,实现一个函数来计算拉格朗日插值多项式的系数。该函数接受已知点的数组和点的个数作为参数,返回一个数组,存储插值多项式的系数。 ```c double* calculateCoefficients(Point* points, int numPoints) { double* coefficients = malloc(numPoints * sizeof(double)); for (int i = 0; i < numPoints; i++) { double coefficient = 1.0; for (int j = 0; j < numPoints; j++) { if (i != j) { coefficient *= (points[i].x - points[j].x); } } coefficients[i] = points[i].y / coefficient; } return coefficients; } ``` 接下来,实现一个函数来计算给定横坐标的插值结果。该函数接受已知点的数组、插值多项式的系数数组、点的个数以及待插值的横坐标作为参数,返回插值结果。 ```c double interpolate(Point* points, double* coefficients, int numPoints, double x) { double result = 0.0; for (int i = 0; i < numPoints; i++) { double term = coefficients[i]; for (int j = 0; j < numPoints; j++) { if (i != j) { term *= (x - points[j].x) / (points[i].x - points[j].x); } } result += term; } return result; } ``` 使用时,可以按照以下步骤进行: 1. 创建一个`Point`数组,存储已知点的横纵坐标。 2. 调用`calculateCoefficients`函数计算插值多项式的系数。 3. 调用`interpolate`函数计算给定横坐标的插值结果。 注意:在使用完动态分配的内存后,需要及时释放以避免内存泄漏。 希望这些代码对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值