数模 17插值

插值与拟合

数据插值不需要函数表达式,而拟合需要。

插值方法

  拉格朗日插值、分段线性插值、hermite、三次样条插值

hours=1:12;
temps=[5 8 9 15 25 29 31 30 22 25 27 24];
h=1:0.1:12; %从0到12,步长为0.1
t=interp1(hours,temps,h,'spline');
plot(hours,temps,'+',h,t,hours,temps,'r:') %作图
xlabel('Hour'),ylabel('Degrees Celsius')

输出:

x=1:5;
y=1:3;
temps=[82 81 80 82 84;79 63 61 65 81;84 84 82 85 86];
mesh(x,y,temps)

输出:

xi=1:0.2:5;
yi=1:0.2:3;
zi=interp2(x,y,temps,xi',yi,'cubic');
mesh(xi,yi,zi)

输出:

%程序一:插值并作海底曲面图
 x  =[129.0  140.0  103.5  88.0  185.5  195.0  105.5 157.5  107.5  77.0  81.0  162.0  162.0  117.5 ];
y =[ 7.5  141.5  23.0  147.0  22.5  137.5  85.5      -6.5  -81  3.0  56.5  -66.5  84.0  -33.5 ];
z =[ 4  8  6  8  6  8  8  9  9  8  8  9  4  9 ];
x1=75:1:200;
y1=-50:1:150;
[x1,y1]=meshgrid(x1,y1);
z1=griddata(x,y,z,x1,y1,'v4');
meshc(x1,y1,z1) 

输出:

%程序二:插值并作出水深小于5的海域范围。
x1=75:1:200;
y1=-50:1:150;
[x1,y1]=meshgrid(x1,y1);
z1=griddata(x,y,z,x1,y1,'v4');  %插值
z1(z1>=5)=nan;   %将水深大于5的置为nan,这样绘图就不会显示出来
meshc(x1,y1,z1) 

 

posted @ 2019-07-17 10:54  ivanthor  阅读(177)  评论(0)    收藏  举报