#include?stdio.h
創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)網(wǎng)站建設(shè)與網(wǎng)站營銷,提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計、網(wǎng)站開發(fā)、seo優(yōu)化、網(wǎng)站排名、互聯(lián)網(wǎng)營銷、重慶小程序開發(fā)、公眾號商城、等建站開發(fā),創(chuàng)新互聯(lián)網(wǎng)站建設(shè)策劃專家,為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制解決方案,幫助客戶在新的全球化互聯(lián)網(wǎng)環(huán)境中保持優(yōu)勢。
double?Lerp(double?x0,double?y0,double?x1,double?y1,double?x)
{
double?dy?=?y1?-?y0;
if(dy?==?0){
printf("除0錯誤!\n");
return?0;
}
return?x?*?(x1?-?x0)?/?dy;
}
int?main()
{
double?x0,x1,y1,y0,x,y;
printf("Inptu?x0?y0?x1?y1?x:");
scanf("%lf?%lf?%lf?%lf?%lf",x0,y0,x1,y1,x);
y?=?Lerp(x0,y0,x1,y1,x);
printf("y?=?%lf\n",y);
return?0;
}
#includestdio.h
#includestdlib.h
#includeiostream.h
typedef struct data
{
float x;
float y;
}Data;//變量x和函數(shù)值y的結(jié)構(gòu)
Data d[20];//最多二十組數(shù)據(jù)
float f(int s,int t)//牛頓插值法,用以返回插商
{
if(t==s+1)
return (d[t].y-d[s].y)/(d[t].x-d[s].x);
else
return (f(s+1,t)-f(s,t-1))/(d[t].x-d[s].x);
}
float Newton(float x,int count)
{
int n;
while(1)
{
cout"請輸入n值(即n次插值):";//獲得插值次數(shù)
cinn;
if(n=count-1)// 插值次數(shù)不得大于count-1次
break;
else
system("cls");
}
//初始化t,y,yt。
float t=1.0;
float y=d[0].y;
float yt=0.0;
//計算y值
for(int j=1;j=n;j++)
{
t=(x-d[j-1].x)*t;
yt=f(0,j)*t;
//coutf(0,j)endl;
y=y+yt;
}
return y;
}
float lagrange(float x,int count)
{
float y=0.0;
for(int k=0;kcount;k++)//這兒默認為count-1次插值
{
float p=1.0;//初始化p
for(int j=0;jcount;j++)
{//計算p的值
if(k==j)continue;//判斷是否為同一個數(shù)
p=p*(x-d[j].x)/(d[k].x-d[j].x);
}
y=y+p*d[k].y;//求和
}
return y;//返回y的值
}
void main()
{
float x,y;
int count;
while(1)
{
cout"請輸入x[i],y[i]的組數(shù),不得超過20組:";//要求用戶輸入數(shù)據(jù)組數(shù)
cincount;
if(count=20)
break;//檢查輸入的是否合法
system("cls");
}
//獲得各組數(shù)據(jù)
for(int i=0;icount;i++)
{
cout"請輸入第"i+1"組x的值:";
cind[i].x;
cout"請輸入第"i+1"組y的值:";
cind[i].y;
system("cls");
}
cout"請輸入x的值:";//獲得變量x的值
cinx;
while(1)
{
int choice=3;
cout"請您選擇使用哪種插值法計算:"endl;
cout" (0):退出"endl;
cout" (1):Lagrange"endl;
cout" (2):Newton"endl;
cout"輸入你的選擇:";
cinchoice;//取得用戶的選擇項
if(choice==2)
{
cout"你選擇了牛頓插值計算方法,其結(jié)果為:";
y=Newton(x,count);break;//調(diào)用相應(yīng)的處理函數(shù)
}
if(choice==1)
{
cout"你選擇了拉格朗日插值計算方法,其結(jié)果為:";
y=lagrange(x,count);break;//調(diào)用相應(yīng)的處理函數(shù)
}
if(choice==0)
break;
system("cls");
cout"輸入錯誤!!!!"endl;
}
coutx" , "yendl;//輸出最終結(jié)果
}
牛頓插值法:
#includestdio.h
#includealloc.h
float Language(float *x,float *y,float xx,int n)
{
int i,j;
float *a,yy=0.0;
a=(float *)malloc(n*sizeof(float));
for(i=0;i=n-1;i++)
{
a[i]=y[i];
for(j=0;j=n-1;j++)
if(j!=i)a[i]*=(xx-x[j])/(x[i]-x[j]);
yy+=a[i];
}
free(a);
return yy;
}
void main()
{
float x[4]={0.56160,0.5628,0.56401,0.56521};
float y[4]={0.82741,0.82659,0.82577,0.82495};
float xx=0.5635,yy;
float Language(float *,float *,float,int);
yy=Language(x,y,xx,4);
printf("x=%f,y=%f\n",xx,yy);
getchar();
}
2.牛頓插值法#includestdio.h
#includemath.h
#define N 4
void Difference(float *x,float *y,int n)
{
float *f;
int k,i;
f=(float *)malloc(n*sizeof(float));
for(k=1;k=n;k++)
{
f[0]=y[k];
for(i=0;ik;i++)
f[i+1]=(f[i]-y[i])/(x[k]-x[i]);
y[k]=f[k];
}
return;
}
main()
{
int i;
float varx=0.895,b;
float x[N+1]={0.4,0.55,0.65,0.8,0.9};
float y[N+1]={0.41075,0.57815,0.69675,0.88811,1.02652};
Difference(x,(float *)y,N);
b=y[N];
for(i=N-1;i=0;i--)b=b*(varx-x[i])+y[i];
printf("Nn(%f)=%f",varx,b);
getchar();
}
留下個郵箱,我發(fā)給你:牛頓插值法的程序設(shè)計與應(yīng)用
a???b
t
c???d
就是兩次線性插值,先在x方向插出t上下方的_t1、_t2,然后再用它們插出t來
float?test(float?x,float?y)
{
float?_t1,_t2,t;
_t1?=?a+(b-a)*(x-ax)/(bx-ax);
_t2?=?c+(d-c)*(x-cx)/(dx-cx);
t?=?_t1?+(_t2-_t1)*(y?-?ay);
return?t;
}