matlab插值与拟合.docx
《matlab插值与拟合.docx》由会员分享,可在线阅读,更多相关《matlab插值与拟合.docx(18页珍藏版)》请在冰豆网上搜索。
matlab插值与拟合
【一维插值】interp1.....................................................................................................................................1
yi=interp1(x,y,xi,method).....................................................................................................................1
例1.........................................................................................................................................................1
例2.........................................................................................................................................................2
【二维插值】interp2.....................................................................................................................................4
ZI=interp2(X,Y,Z,XI,YI,method).........................................................................................................4
插值方式比较示例................................................................................................................................4
例3.........................................................................................................................................................8
例4.........................................................................................................................................................9
【三角测量和分散数据插值】...................................................................................................................13
【数据拟合】...............................................................................................................................................16
例5........................................................................................................................................................16
例6........................................................................................................................................................17
【一维插值】
interp1
yi=interp1(x,y,xi,method)
例1
在1-12的11小时内,每隔1小时测量一次温度,测得的温度依次为:
5,8,9,15,25,29,31,30,22,25,27,24。
试估计每隔1/10小时的温度值。
建立M文件temp.m
hours=1:
12;
temps=[589152529313022252724];
h=1:
0.1:
12;
t=interp1(hours,temps,h,'spline');
plot(hours,temps,'kp',h,t,'b');
35
30
25
20
15
10
5
024681012
例2
已知飞机下轮廓线上数据如下,求x每改变0.1时的y值。
X
Y
035791112131415
01.21.72.02.12.01.81.21.01.6
建立M文件plane.m
x0=[035791112131415];
y0=[01.21.72.02.12.01.81.21.01.6];x=0:
0.1:
15;
y1=interp1(x0,y0,x,'nearest');
y2=interp1(x0,y0,x);
y3=interp1(x0,y0,x,'spline');
plot(x0,y0,'kp',x,y1,'r')
2.5
2
1.5
1
0.5
0
051015
plot(x0,y0,'kp',x,y2,'r')
2.5
2
1.5
1
0.5
0
051015
plot(x0,y0,'kp',x,y3,'r')
2.5
2
1.5
1
0.5
0
051015
【二维插值】
interp2
ZI=interp2(X,Y,Z,XI,YI,method)插值方式比较示例
用较大间隔产生peaks函数数据点[x,y]=meshgrid(-3:
1:
3);
z=peaks(x,y);
surf(x,y,z)
6
4
2
0
-2
-4
-6
4
2
4
0
-2
-2
0
2
-4-4
●产生一个较好的网格
[xi,yi]=meshgrid(-3:
0.25:
3);
●利用最近邻方式插值
zi1=interp2(x,y,z,xi,yi,'nearest');surf(xi,yi,zi1)
●双线性插值方式
zi2=interp2(x,y,z,xi,yi,'bilinear');surf(xi,yi,zi2)
●双立方插值方式
zi3=interp2(x,y,z,xi,yi,'bicubic');surf(xi,yi,zi3)
●不同插值方式构造的等高线图对比contour(xi,yi,zi1)
3
2
1
0
-1
-2
-3
-3-2-10123
contour(xi,yi,zi2)
3
2
1
0
-1
-2
-3
-3-2-10123
contour(xi,yi,zi3)
3
2
1
0
-1
-2
-3
-3-2-10123
例3
测得平板表面3*5网格点处的温度分别为:
8281808284
7963616581
8484828586
试作出平板表面的温度分布曲面z=f(x,y)的图形。
建立M文件wendu.m
xx=1:
0.2:
5;
yy=1:
0.2:
3;
zzz=interp2(x,y,temps,xi',yi,'cubic');
mesh(xi,yi,zi);
90
85
80
75
70
65
60
3
2.5
5
2
1.5
2
3
4
11
例4
某山区测得一些地点的高度如下表所示,平面区域为
该山区的地貌图和等高线图。
比较几种插值方法。
1200x4000,1200y3600
,试作出
建立M文件moutain.m
x=0:
400:
5600;
y=0:
400:
4800;
zz=[370470550600670690670620580450400300100150250;...
510620730800850870850780720650500200300350320;...
650760880970102010501020830900700300500550480350;...
740880108011301250128012301040900500700780750650550;...
830980118013201450142014001300700900850840380780750;...
88010601230139015001500140090011001060950870900930950;...
9101090127015001200110013501450120011501010880100010501100;...
9501190137015001200110015501600155013801070900105011501200;...
143014301460150015501600155016001600160015501500150015501550;...
1420143014501480150015501510143013001200980850750550500;...
138014101430145014701320128012001080940780620460370350;...
13701390141014301440114011101050950820690540380300210;...
13501370139014001410960940880800690570430290210150];figure
(1);
meshz(x,y,z)
2000
1500
1000
500
0
6000
4000
6000
4000
2000
2000
00
xx=0:
50:
5600;
yy=0:
50:
4800;
figure
(2)
z1i=interp2(x,y,z,xi,yi','nearest');
surfc(xi,yi,z1i)
figure(3)
z2i=interp2(x,y,z,xi,yi');
surfc(xi,yi,z2i)
figure(4)
z3i=interp2(x,y,z,xi,yi','cubic');
surfc(xi,yi,z3i)
figure(5)
subplot(1,2,1),contour(xi,yi,z2i,10);
subplot(1,2,2),contour(xi,yi,z3i,10);
4500
4000
3500
3000
2500
2000
1500
1000
500
0
4500
4000
3500
3000
2500
2000
1500
1000
500
0
020004000020004000
【三角测量和分散数据插值】
凸包(ConvexHulls)
loadseamount
plot(x,y,'.','markersize',10)
k=convhull(x,y);
holdon,plot(x(k),y(k),'-r'),holdoff
gridon
-47.95
-48
-48.05
-48.1
-48.15
-48.2
-48.25
-48.3
-48.35
-48.4
-48.45
210.8210.9211211.1211.2211.3211.4211.5211.6211.7211.8德洛涅三角(DelaunayTriangulation)
loadseamount
plot(x,y,'.','markersize',12)
xlabel('Longitude'),ylabel('Latitude')
i
t
i
t
gridon
-47.95
-48
-48.05
-48.1
-48.15
e
d
u
t
a
L
-48.2
-48.25
-48.3
-48.35
-48.4
-48.45
210.8210.9211211.1211.2211.3211.4211.5211.6211.7211.8
Longitude
tri=delaunay(x,y);
holdon,triplot(tri,x,y),holdoff
-47.95
-48
-48.05
-48.1
-48.15
e
d
u
t
a
L
-48.2
-48.25
-48.3
-48.35
-48.4
-48.45
210.8210.9211211.1211.2211.3211.4211.5211.6211.7211.8
Longitude
figure
hiddenon
trimesh(tri,x,y,z)
gridon
xlabel('Longitude');ylabel('Latitude');zlabel('DepthinFeet')
t
e
i
t
e
00
0
0
00
i
t
-
-
2
0
0
0
0
0
-
0
0
1
-
0
0
-
0
e
F
n
h
p
D
0
-1000
-2000
-3000
-4000
-5000
-47.8
-48
211.8
-48.2
Latitude
-48.4
-48.6210.8
211
211.4
211.2
Longitude
211.6
figure
[xi,yi]=meshgrid(210.8:
.01:
211.8,-48.5:
.01:
-47.9);
zi=griddata(x,y,z,xi,yi,'cubic');
[c,h]=contour(xi,yi,zi,'b-');
clabel(c,h)
xlabel('Longitude'),ylabel('Latitude')
-47.9
-48
-40
-48.1
-4
0
-3500
-30
e
d
u
t
a
L
-48.2
-48.3
4
0
0
0
-35-030-205
0
-2
0
-1500
02
5
0
00
-3
0
0
0
5
3
-
4
0
0
-4000
-3500
-48.4
-4
000
-48.5
210.8210.9211211.1211.2211.3211.4211.5211.6211.7211.8
Longitude
火龙尼图形(VoronoiDiagrams)loadseamount
voronoi(x,y)
gridon
i
t
xlabel('Longitude'),ylabel('Latitude')
-47.95
-48
-48.05
-48.1
-48.15
e
d
u
t
a
L
-48.2
-48.25
-48.3
-48.35
-48.4
-48.45
210.8210.9211211.1211.2211.3211.4211.5211.6211.7211.8
Longitude
【数据拟合】
例5
对下面一组数据作二次多项式拟合
x=[0.10.20.4:
.1:
1];
y=[1.9783.286.167.347.669.589.489.3011.2];A=polyfit(x,y,2);
zz=polyval(A,x);
plot(x,y,'k+',x,z,'r')
12
10
8
6
4
2
0
0.10.20.30.40.50.60.70.80.91
例6
用下面一组数据拟合
c(t)abe
0.02kt
中的参数a,b,k。
方法1:
用lsqcurvefit
建立M文件curvefun1.m
functionf=curvefun1(x,tdata)
f=x
(1)+x
(2)*exp(-0.02*x(3)*tdata)
%其中x
(1)=a;x
(2)=b;x(3)=k;
输入命令:
tdata=100:
100:
1000;
cdata=1e-03*[4.54,4.99,5.35,5.65,5.90,6.10,6.26,6.39,6.50,6.59];
x0=[0.2,0.05,0.05];
x=lsqcurvefit('curvefun1',x0,tdata,cdata)
x=
0.0063-0.00340.2542
方法2:
用lsqnonlin
建立M文件curvefun2.m
functionf=curvefun2(x)
tdata=100:
100:
1000;
cdata=1e-03*[4.54,4.99,5.35,5.65,5.90,6.10,6.26,6.39,6.50,6.59];
f=x
(1)+x
(2)*exp(-0.02*x(3)*tdata)-cdata
输入命令:
x0=[0.2,0.05,0.05];
x=lsqnonlin('curvefun2',x0)
x=
0.0063-0.00340.2542