matlab中不是可以直接轉(zhuǎn)為C嗎,先用interp2()寫個(gè)東西,再一轉(zhuǎn)不就行了么。
拜城網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)成立于2013年到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
牛頓插值法:
#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();
}
留下個(gè)郵箱,我發(fā)給你:牛頓插值法的程序設(shè)計(jì)與應(yīng)用
a???b
t
c???d
就是兩次線性插值,先在x方向插出t上下方的_t1、_t2,然后再用它們插出t來(lái)
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;
}
#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"請(qǐng)輸入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;
//計(jì)算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++)//這兒默認(rèn)為count-1次插值
{
float p=1.0;//初始化p
for(int j=0;jcount;j++)
{//計(jì)算p的值
if(k==j)continue;//判斷是否為同一個(gè)數(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"請(qǐng)輸入x[i],y[i]的組數(shù),不得超過(guò)20組:";//要求用戶輸入數(shù)據(jù)組數(shù)
cincount;
if(count=20)
break;//檢查輸入的是否合法
system("cls");
}
//獲得各組數(shù)據(jù)
for(int i=0;icount;i++)
{
cout"請(qǐng)輸入第"i+1"組x的值:";
cind[i].x;
cout"請(qǐng)輸入第"i+1"組y的值:";
cind[i].y;
system("cls");
}
cout"請(qǐng)輸入x的值:";//獲得變量x的值
cinx;
while(1)
{
int choice=3;
cout"請(qǐng)您選擇使用哪種插值法計(jì)算:"endl;
cout" (0):退出"endl;
cout" (1):Lagrange"endl;
cout" (2):Newton"endl;
cout"輸入你的選擇:";
cinchoice;//取得用戶的選擇項(xiàng)
if(choice==2)
{
cout"你選擇了牛頓插值計(jì)算方法,其結(jié)果為:";
y=Newton(x,count);break;//調(diào)用相應(yīng)的處理函數(shù)
}
if(choice==1)
{
cout"你選擇了拉格朗日插值計(jì)算方法,其結(jié)果為:";
y=lagrange(x,count);break;//調(diào)用相應(yīng)的處理函數(shù)
}
if(choice==0)
break;
system("cls");
cout"輸入錯(cuò)誤!!!!"endl;
}
coutx" , "yendl;//輸出最終結(jié)果
}
#include?stdio.h
double?Lerp(double?x0,double?y0,double?x1,double?y1,double?x)
{
double?dy?=?y1?-?y0;
if(dy?==?0){
printf("除0錯(cuò)誤!\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;
}
分享名稱:c語(yǔ)言線性插值函數(shù) 線性插值法c語(yǔ)言程序
本文地址:http://muchs.cn/article6/docohig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、建站公司、品牌網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、App開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)