拉格朗日插值程序C语言,拉格朗日插值法 C语言实现
/*
*作者:KDF5000
*功能:利用拉格朗日插值法求解近似值
*時間:2013.4.15
*/
#include
#include
#include
//存放插值節點
struct Data{
double x;
double y;
struct Data *next;
};
/****************************************************
*LagrangeInsert()
*功能:拉格朗日插值法
*****************************************************/
double LagrangeInsert(struct Data *header,double x)
{
Data *pi,*pj,*p;
pi=pj=header->next;
double temp1,temp2;
temp1=0; //記錄內循環的積
temp2=1;//記錄外循環的和
while(pi!=NULL)
{
while(pj!=NULL)
{
if(pi!=pj)
temp2 *=(x-pj->x)/(pi->x-pj->x);
pj = pj->next;
}
temp1 +=temp2*pi->y;
temp2=1;
pj = header->next;
pi = pi->next;
}
return temp1; //返回計算結果
}
void main()
{
Data *header = (Data *)malloc(sizeof(Data));
char str[20];
Data *p,*newData;
char strx[20],stry[20];
double x;
p=header;
p->x=0;
p->y=0;
p->next=NULL;
//輸出提示信息
printf("*******************************************\n");
printf("使用說明:\n1.用戶輸入插值點,每一行輸入一組:x y;\n2.輸入換行表示輸入結束。\n");
printf("*******************************************\n");
printf("x y\n");
//接收用戶輸入知道第一次輸入非換行為止
memset(str,0,sizeof(str));
while(strlen(str)==0)
gets(str);
//數據輸入完畢,輸入換行結束輸入
while(strlen(str)!=0)
{
newData = (Data *)malloc(sizeof(Data));
sscanf(str,"%s%s",strx,stry); //獲取輸入的前兩個字符串 第一個為x,第二個為y
newData->x = strtod(strx,NULL); //將輸入轉換成浮點數
newData->y = strtod(stry,NULL);
newData->next=NULL;
p->next=newData;
p = p->next;
gets(str);
}
printf("請輸入要計算的x值:");
scanf("%lf",&x);
printf("L(%f) = %f\n",x,LagrangeInsert(header,0.20));
return ;
}
總結
以上是生活随笔為你收集整理的拉格朗日插值程序C语言,拉格朗日插值法 C语言实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 移动web HTML5使用photosw
- 下一篇: 二维码WIFI自动连接生成格式