問題補(bǔ)充,因字?jǐn)?shù)限制,挪到這
創(chuàng)新互聯(lián)專注于當(dāng)雄網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供當(dāng)雄營銷型網(wǎng)站建設(shè),當(dāng)雄網(wǎng)站制作、當(dāng)雄網(wǎng)頁設(shè)計(jì)、當(dāng)雄網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造當(dāng)雄網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供當(dāng)雄網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
1.拉格朗日插值簡介:
對給定的n個(gè)插值節(jié)點(diǎn)x1,x2,…,xn,及其對應(yīng)的函數(shù)值y1=f(x1), y2=f(x2),…, yn=f(xn);使用拉格朗日插值公式,計(jì)算在x點(diǎn)處的對應(yīng)的函數(shù)值f(x);
2.一維拉格朗日插值c語言程序:
Int lagrange(x0, y0, n, x, y)
Float xo[], yo[], x;
Int n;
Float *y
{
Int i, j;
Float p;
*y=0;
If (n1)
{
For(i=0;in;i++)
{
P=1;
For(j=1;jn;j++)
{
If(i!=J)
P=p*(x-x0[j]/x0[i]-x0[j]);
}
*y=*y+p*y0[i];
Return(0);
}
Else
Return(-1);
}
3.例題。已知函數(shù)如下表所示,求x=0.472處的函數(shù)值:
X 0.46 0.47 0.48 0.49
Y 0.484655 0.4903745 0.502750 0.511668
計(jì)算這個(gè)問題的c語言程序如下:
#minclude stdio
#includeMnath.h
Main()
{
Float x0[4]={ 0.46, 0.47,0.48,0.49};
Float y0[4]={ 0.484655 ,0.4903745 ,0.502750 ,0.511668};
Float x, y;
Int n, rtn;
N=4;
X=0.472;
Rth=lagrange(x0,y0,n,x,y);
If(rtn=0)
{
Prinf(“Y(0.472)=:%f\n”,y);
}
Else
{
Prinf(“n must be larger than 1.\n”);
}
}
計(jì)算結(jié)果:Y(0.472)=:0.495553
4.問題補(bǔ)充
我的問題與上面的例子類似,計(jì)算三維空間一點(diǎn)(x,y,z)對應(yīng)的函數(shù)值(Vx,Vy,Vz).不同的是自變量(point_coordinate.txt)為三維空間散亂點(diǎn)(不是正方體的頂點(diǎn)),因變量(point_data.txt)為矢量(向量 )。插值算法比較多,常數(shù)法,拉格朗日插值,埃特金插值,三階樣條插值等。最簡單的就是常數(shù)法,查找離目標(biāo)點(diǎn)(x,y,z)距離最近的已知自變量(Xi,Yi,Zi),把該點(diǎn)的函數(shù)值賦給目標(biāo)點(diǎn)做函數(shù)值,求高手幫忙寫寫。
#include?stdio.h
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;
}
最簡單的做法是在結(jié)構(gòu)體里存函數(shù)指針,然后初始化結(jié)構(gòu)體的時(shí)候?qū)⒑瘮?shù)的地址賦值給它。
復(fù)雜一點(diǎn)的,就得按照c++的多態(tài)原理,用虛函數(shù)表(其實(shí)就是能記錄函數(shù)名對應(yīng)的函數(shù)地址就好)記錄每種結(jié)構(gòu)體的成員函數(shù)地址,然后每個(gè)結(jié)構(gòu)體里多存一個(gè)虛函數(shù)表的地址。。。然后調(diào)用函數(shù)的時(shí)候查到函數(shù)地址強(qiáng)轉(zhuǎn)成函數(shù)類型然后調(diào)用。。。這個(gè)可以自己研究一下c++的虛函數(shù)表機(jī)制。
void
SPL(int
n,
double
*x,
double
*y,
int
ni,
double
*xi,
double
*yi);
是你所要。
已知
n
個(gè)點(diǎn)
x,y;
x
必須已按順序排好。要插值
ni
點(diǎn),橫坐標(biāo)
xi[],
輸出
yi[]。
程序里用double
型,保證計(jì)算精度。
SPL調(diào)用現(xiàn)成的程序。
現(xiàn)成的程序很多。端點(diǎn)處理方法不同,結(jié)果會有不同。想同matlab比較,你需
嘗試
調(diào)用
spline()函數(shù)
時(shí),令
end1
為
1,
設(shè)
slope1
的值,令
end2
為
1
設(shè)
slope2
的值。
#include
stdio.h
#include
math.h
int
spline
(int
n,
int
end1,
int
end2,
double
slope1,
double
slope2,
double
x[],
double
y[],
double
b[],
double
c[],
double
d[],
int
*iflag)
{
int
nm1,
ib,
i,
ascend;
double
t;
nm1
=
n
-
1;
*iflag
=
0;
if
(n
2)
{
/*
no
possible
interpolation
*/
*iflag
=
1;
goto
LeaveSpline;
}
ascend
=
1;
for
(i
=
1;
i
n;
++i)
if
(x[i]
=
x[i-1])
ascend
=
0;
if
(!ascend)
{
*iflag
=
2;
goto
LeaveSpline;
}
if
(n
=
3)
{
d[0]
=
x[1]
-
x[0];
c[1]
=
(y[1]
-
y[0])
/
d[0];
for
(i
=
1;
i
nm1;
++i)
{
d[i]
=
x[i+1]
-
x[i];
b[i]
=
2.0
*
(d[i-1]
+
d[i]);
c[i+1]
=
(y[i+1]
-
y[i])
/
d[i];
c[i]
=
c[i+1]
-
c[i];
}
/*
----
Default
End
conditions
*/
b[0]
=
-d[0];
b[nm1]
=
-d[n-2];
c[0]
=
0.0;
c[nm1]
=
0.0;
if
(n
!=
3)
{
c[0]
=
c[2]
/
(x[3]
-
x[1])
-
c[1]
/
(x[2]
-
x[0]);
c[nm1]
=
c[n-2]
/
(x[nm1]
-
x[n-3])
-
c[n-3]
/
(x[n-2]
-
x[n-4]);
c[0]
=
c[0]
*
d[0]
*
d[0]
/
(x[3]
-
x[0]);
c[nm1]
=
-c[nm1]
*
d[n-2]
*
d[n-2]
/
(x[nm1]
-
x[n-4]);
}
/*
Alternative
end
conditions
--
known
slopes
*/
if
(end1
==
1)
{
b[0]
=
2.0
*
(x[1]
-
x[0]);
c[0]
=
(y[1]
-
y[0])
/
(x[1]
-
x[0])
-
slope1;
}
if
(end2
==
1)
{
b[nm1]
=
2.0
*
(x[nm1]
-
x[n-2]);
c[nm1]
=
slope2
-
(y[nm1]
-
y[n-2])
/
(x[nm1]
-
x[n-2]);
}
/*
Forward
elimination
*/
for
(i
=
1;
i
n;
++i)
{
t
=
d[i-1]
/
b[i-1];
b[i]
=
b[i]
-
t
*
d[i-1];
c[i]
=
c[i]
-
t
*
c[i-1];
}
/*
Back
substitution
*/
c[nm1]
=
c[nm1]
/
b[nm1];
for
(ib
=
0;
ib
nm1;
++ib)
{
i
=
n
-
ib
-
2;
c[i]
=
(c[i]
-
d[i]
*
c[i+1])
/
b[i];
}
b[nm1]
=
(y[nm1]
-
y[n-2])
/
d[n-2]
+
d[n-2]
*
(c[n-2]
+
2.0
*
c[nm1]);
for
(i
=
0;
i
nm1;
++i)
{
b[i]
=
(y[i+1]
-
y[i])
/
d[i]
-
d[i]
*
(c[i+1]
+
2.0
*
c[i]);
d[i]
=
(c[i+1]
-
c[i])
/
d[i];
c[i]
=
3.0
*
c[i];
}
c[nm1]
=
3.0
*
c[nm1];
d[nm1]
=
d[n-2];
}
else
{
b[0]
=
(y[1]
-
y[0])
/
(x[1]
-
x[0]);
c[0]
=
0.0;
d[0]
=
0.0;
b[1]
=
b[0];
c[1]
=
0.0;
d[1]
=
0.0;
}
LeaveSpline:
return
0;
}
double
seval
(int
n,
double
u,
double
x[],
double
y[],
double
b[],
double
c[],
double
d[],
int
*last)
{
int
i,
j,
k;
double
w;
i
=
*last;
if
(i
=
n-1)
i
=
0;
if
(i
0)
i
=
0;
if
((x[i]
u)
||
(x[i+1]
u))
{
i
=
0;
j
=
n;
do
{
k
=
(i
+
j)
/
2;
if
(u
x[k])
j
=
k;
if
(u
=
x[k])
i
=
k;
}
while
(j
i+1);
}
*last
=
i;
w
=
u
-
x[i];
w
=
y[i]
+
w
*
(b[i]
+
w
*
(c[i]
+
w
*
d[i]));
return
(w);
}
void
SPL(int
n,
double
*x,
double
*y,
int
ni,
double
*xi,
double
*yi)
{
double
*b,
*c,
*d;
int
iflag,last,i;
b
=
(double
*)
malloc(sizeof(double)
*
n);
c
=
(double
*)malloc(sizeof(double)
*
n);
d
=
(double
*)malloc(sizeof(double)
*
n);
if
(!d)
{
printf("no
enough
memory
for
b,c,d\n");}
else
{
spline
(n,0,0,0,0,x,y,b,c,d,iflag);
if
(iflag==0)
printf("I
got
coef
b,c,d
now\n");
else
printf("x
not
in
order
or
other
error\n");
for
(i=0;ini;i++)
yi[i]
=
seval(ni,xi[i],x,y,b,c,d,last);
free(b);free(c);free(d);
};
}
main(){
double
x[6]={0.,1.,2.,3.,4.,5};
double
y[6]={0.,0.5,2.0,1.6,0.5,0.0};
double
u[8]={0.5,1,1.5,2,2.5,3,3.5,4};
double
s[8];
int
i;
SPL(6,
x,y,
8,
u,
s);
for
(i=0;i8;i++)
printf("%lf
%lf
\n",u[i],s[i]);
return
0;
}
用GDI繪圖吧,比較簡單。繪圖的思想是讓x以固定的值在區(qū)間內(nèi)持續(xù)增長,比如x=0.1,0.2,0.3.....,以計(jì)算出的y值來確定y坐標(biāo)。用線連接所有的點(diǎn)就行了。MoveTo(),LineTo()函數(shù)你用得著,具體情況請自行查看MSDN。
網(wǎng)站名稱:c語言三維插值函數(shù)圖像 c++繪圖函數(shù)
文章位置:http://muchs.cn/article8/hgepip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站改版、網(wǎng)站建設(shè)、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)