matlab 插值

本文通过Matlab演示了分段线性、最近邻、三次样条和保凹凸性三次插值方法,并通过实例比较了它们在插值效果上的差异。实验显示,不同插值方法在数据平滑性和逼近真实值方面存在显著区别,尤其是在数据波动较大或边界条件变化时。
摘要由CSDN通过智能技术生成

插值

Matlab 实现分段线性插值不需要编制函数程序,Matlab 中有现成的一维插值函
数 interp1。
y=interp1(x0,y0,x,‘method’)
method 指定插值的方法,默认为线性插值。其值可为:
‘nearest’ 最近项插值
‘linear’ 线性插值
‘spline’ 逐段 3 次样条插值
‘cubic’ 保凹凸性 3 次插值。
所有的插值方法要求 x0 是单调的。
当 x0 为等距时可以用快速插值法,使用快速插值法的格式为’*nearest’、’*linear’、
‘*spline’、’*cubic’。

function y=lagrange(x0,y0,x); 
n=length(x0);m=length(x); 
for i=1:m
    z=x(i); 
    s=0.0; 
    for k=1:n 
        p=1.0; 
    for j=1:n 
        if j~=k 
            p=p*(z-x0(j))/(x0(k)-x0(j)); 
        end 
    end 
    s=p*y0(k)+s; 
    end 
    y(i)=s; 
end
clc,clear 
x0=[0 3 5 7 9 11 12 13 14 15];
y0=[0 1.2 1.7 2.0 2.1 2.0 1.8 1.2 1.0 1.6]; 
x=0:0.1:15; 
y1=lagrange(x0,y0,x); %调用前面编写的Lagrange插值函数
y2=interp1(x0,y0,x); 
y3=interp1(x0,y0,x,'spline'); 
pp1=csape(x0,y0); y4=ppval(pp1,x); 
pp2=csape(x0,y0,'second'); y5=ppval(pp2,x); 
fprintf('比较一下不同插值方法和边界条件的结果:\n') 
fprintf('x y1 y2 y3 y4 y5\n') 
xianshi=[x',y1',y2',y3',y4',y5']; 
fprintf('%f\t%f\t%f\t%f\t%f\t%f\n',xianshi') 
subplot(2,2,1), plot(x0,y0,'+',x,y1), title('Lagrange') 
subplot(2,2,2), plot(x0,y0,'+',x,y2), title('Piecewise linear') 
subplot(2,2,3), plot(x0,y0,'+',x,y3), title('Spline1') 
subplot(2,2,4), plot(x0,y0,'+',x,y4), title('Spline2') 
dyx0=ppval(fnder(pp1),x0(1)) %求x=0处的导数
ytemp=y3(131:151); 
index=find(ytemp==min(ytemp)); 
xymin=[x(130+index),ytemp(index)]

result
比较一下不同插值方法和边界条件的结果:
x y1 y2 y3 y4 y5
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.100000 -4.943720 0.040000 0.049861 0.049716 0.044073
0.200000 -8.819979 0.080000 0.098997 0.098723 0.088118
0.300000 -11.772560 0.120000 0.147411 0.147027 0.132109
0.400000 -13.931018 0.160000 0.195109 0.194630 0.176019
0.500000 -15.411743 0.200000 0.242097 0.241537 0.219819
0.600000 -16.318971 0.240000 0.288378 0.287753 0.263484
0.700000 -16.745729 0.280000 0.333957 0.333280 0.306986
0.800000 -16.774737 0.320000 0.378840 0.378124 0.350298
0.900000 -16.479241 0.360000 0.423032 0.422288 0.393392
1.000000 -15.923810 0.400000 0.466537 0.465776 0.436241
1.100000 -15.165076 0.440000 0.509361 0.508593 0.478819
1.200000 -14.252433 0.480000 0.551507 0.550743 0.521098
1.300000 -13.228686 0.520000 0.592982 0.592230 0.563050
1.400000 -12.130662 0.560000 0.633790 0.633057 0.604649
1.500000 -10.989778 0.600000 0.673935 0.673230 0.645868
1.600000 -9.832573 0.640000 0.713423 0.712751 0.686679
1.700000 -8.681200 0.680000 0.752260 0.751626 0.727055
1.800000 -7.553886 0.720000 0.790448 0.789858 0.766969
1.900000 -6.465357 0.760000 0.827994 0.827451 0.806393
2.000000 -5.427232 0.800000 0.864903 0.864410 0.845301
2.100000 -4.448386 0.840000 0.901179 0.900738 0.883666
2.200000 -3.535284 0.880000 0.936827 0.936440 0.921460
2.300000 -2.692293 0.920000 0.971852 0.971520 0.958656
2.400000 -1.921962 0.960000 1.006259 1.005981 0.995226
2.500000 -1.225278 1.000000 1.040054 1.039829 1.031145
2.600000 -0.601908 1.040000 1.073240 1.073067 1.066384
2.700000 -0.050409 1.080000 1.105822 1.105699 1.100916
2.800000 0.431580 1.120000 1.137807 1.137729 1.134714
2.900000 0.847155 1.160000 1.169198 1.169161 1.167751
3.000000 1.200000 1.200000 1.200000 1.200000 1.200000
3.100000 1.494237 1.225000 1.230219 1.230250 1.231441
3.200000 1.734308 1.250000 1.259859 1.259915 1.262087
3.300000 1.924859 1.275000 1.288925 1.289002 1.291956
3.400000 2.070648 1.300000 1.317422 1.317515 1.321069
3.500000 2.176456 1.325000 1.345355 1.345459 1.349445
3.600000 2.247018 1.350000 1.372729 1.372841 1.377103
3.700000 2.286959 1.375000 1.399549 1.399664 1.404064
3.800000 2.300741 1.400000 1.425819 1.425935 1.430347
3.900000 2.292622 1.425000 1.451545 1.451659 1.455972
4.000000 2.266621 1.450000 1.476731 1.476840 1.480957
4.100000 2.226489 1.475000 1.501383 1.501485 1.505324
4.200000 2.175692 1.500000 1.525505 1.525598 1.529091
4.300000 2.117392 1.525000 1.549102 1.549185 1.552278
4.400000 2.054444 1.550000 1.572180 1.572251 1.574905
4.500000 1.989390 1.575000 1.594742 1.594801 1.596991
4.600000 1.924459 1.600000 1.616794 1.616841 1.618556
4.700000 1.861574 1.625000 1.638341 1.638375 1.639620
4.800000 1.802357 1.650000 1.659388 1.659410 1.660202
4.900000 1.748145 1.675000 1.679939 1.679950 1.680322
5.000000 1.700000 1.700000 1.700000 1.700000 1.700000
5.100000 1.658724 1.715000 1.719574 1.719565 1.719251
5.200000 1.624880 1.730000 1.738664 1.738647 1.738075
5.300000 1.598807 1.745000 1.757269 1.757246 1.756470
5.400000 1.580641 1.760000 1.775390 1.775361 1.774431
5.500000 1.570336 1.775000 1.793028 1.792995 1.791955
5.600000 1.567683 1.790000 1.810183 1.810147 1.809039
5.700000 1.572332 1.805000 1.826856 1.826818 1.825679
5.800000 1.583812 1.820000 1.843048 1.843009 1.841871
5.900000 1.601550 1.835000 1.858758 1.858720 1.857612
6.000000 1.624894 1.850000 1.873989 1.873950 1.872899
6.100000 1.653128 1.865000 1.888739 1.888702 1.887728
6.200000 1.685493 1.880000 1.903011 1.902976 1.902096
6.300000 1.721203 1.895000 1.916803 1.916771 1.915998
6.400000 1.759462 1.910000 1.930118 1.930089 1.929432
6.500000 1.799475 1.925000 1.942955 1.942931 1.942394
6.600000 1.840469 1.940000 1.955316 1.955295 1.954880
6.700000 1.881697 1.955000 1.967200 1.967184 1.966888
6.800000 1.922455 1.970000 1.978608 1.978597 1.978412
6.900000 1.962088 1.985000 1.989541 1.989536 1.989451
7.000000 2.000000 2.000000 2.000000 2.000000 2.000000
7.100000 2.035659 2.005000 2.009983 2.009989 2.010056
7.200000 2.068604 2.010000 2.019486 2.019497 2.019612
7.300000 2.098447 2.015000 2.028500 2.028516 2.028665
7.400000 2.124877 2.020000 2.037018 2.037040 2.037208
7.500000 2.147659 2.025000 2.045033 2.045060 2.045235
7.600000 2.166637 2.030000 2.052539 2.052570 2.052742
7.700000 2.181730 2.035000 2.059526 2.059562 2.059721
7.800000 2.192931 2.040000 2.065990 2.066029 2.066169
7.900000 2.200303 2.045000 2.071921 2.071963 2.072079
8.000000 2.203975 2.050000 2.077314 2.077358 2.077445
8.100000 2.204136 2.055000 2.082160 2.082205 2.082263
8.200000 2.201030 2.060000 2.086453 2.086498 2.086526
8.300000 2.194947 2.065000 2.090185 2.090229 2.090229
8.400000 2.186220 2.070000 2.093349 2.093391 2.093366
8.500000 2.175213 2.075000 2.095937 2.095976 2.095933
8.600000 2.162316 2.080000 2.097944 2.097978 2.097922
8.700000 2.147933 2.085000 2.099360 2.099389 2.099329
8.800000 2.132479 2.090000 2.100180 2.100201 2.100148
8.900000 2.116366 2.095000 2.100396 2.100407 2.100374
9.000000 2.100000 2.100000 2.100000 2.100000 2.100000
9.100000 2.083767 2.095000 2.098992 2.098979 2.099027
9.200000 2.068032 2.090000 2.097393 2.097366 2.097475
9.300000 2.053126 2.085000 2.095232 2.095190 2.095370
9.400000 2.039341 2.080000 2.092538 2.092480 2.092738
9.500000 2.026927 2.075000 2.089339 2.089265 2.089604
9.600000 2.016081 2.070000 2.085663 2.085573 2.085995
9.700000 2.006948 2.065000 2.081538 2.081434 2.081936
9.800000 1.999614 2.060000 2.076993 2.076876 2.077453
9.900000 1.994101 2.055000 2.072056 2.071928 2.072573
10.000000 1.990373 2.050000 2.066756 2.066619 2.067320
10.100000 1.988327 2.045000 2.061121 2.060978 2.061720
10.200000 1.987799 2.040000 2.055179 2.055032 2.055800
10.300000 1.988562 2.035000 2.048958 2.048813 2.049585
10.400000 1.990331 2.030000 2.042488 2.042347 2.043102
10.500000 1.992767 2.025000 2.035795 2.035664 2.036375
10.600000 1.995482 2.020000 2.028910 2.028792 2.029431
10.700000 1.998043 2.015000 2.021859 2.021762 2.022296
10.800000 1.999983 2.010000 2.014672 2.014600 2.014995
10.900000 2.000806 2.005000 2.007376 2.007337 2.007555
11.000000 2.000000 2.000000 2.000000 2.000000 2.000000
11.100000 1.997046 1.980000 1.992444 1.992490 1.992234
11.200000 1.991427 1.960000 1.984097 1.984191 1.983662
11.300000 1.982644 1.940000 1.974218 1.974358 1.973567
11.400000 1.970227 1.920000 1.962067 1.962246 1.961232
11.500000 1.953746 1.900000 1.946903 1.947110 1.945939
11.600000 1.932829 1.880000 1.927988 1.928205 1.926971
11.700000 1.907170 1.860000 1.904580 1.904787 1.903610
11.800000 1.876546 1.840000 1.875939 1.876110 1.875140
11.900000 1.840830 1.820000 1.841326 1.841429 1.840842
12.000000 1.800000 1.800000 1.800000 1.800000 1.800000
12.100000 1.754153 1.740000 1.751566 1.751427 1.752222
12.200000 1.703516 1.680000 1.697012 1.696712 1.698421
12.300000 1.648451 1.620000 1.637667 1.637206 1.639838
12.400000 1.589463 1.560000 1.574864 1.574259 1.577713
12.500000 1.527208 1.500000 1.509935 1.509224 1.513285
12.600000 1.462486 1.440000 1.444212 1.443451 1.447794
12.700000 1.396248 1.380000 1.379024 1.378291 1.382480
12.800000 1.329586 1.320000 1.315706 1.315095 1.318583
12.900000 1.263723 1.260000 1.255587 1.255214 1.257343
13.000000 1.200000 1.200000 1.200000 1.200000 1.200000
13.100000 1.139856 1.180000 1.150090 1.150601 1.147680
13.200000 1.084801 1.160000 1.106257 1.107361 1.101054
13.300000 1.036388 1.140000 1.068714 1.070419 1.060680
13.400000 0.996167 1.120000 1.037675 1.039916 1.027117
13.500000 0.965644 1.100000 1.013355 1.015994 1.000922
13.600000 0.946220 1.080000 0.995966 0.998791 0.982654
13.700000 0.939129 1.060000 0.985722 0.988450 0.972869
13.800000 0.945361 1.040000 0.982838 0.985111 0.972127
13.900000 0.965575 1.020000 0.987526 0.988914 0.980984
14.000000 1.000000 1.000000 1.000000 1.000000 1.000000
14.100000 1.048324 1.060000 1.020474 1.018567 1.029460
14.200000 1.109568 1.120000 1.049162 1.045044 1.068564
14.300000 1.181945 1.180000 1.086278 1.079918 1.116241
14.400000 1.262704 1.240000 1.132034 1.123675 1.171419
14.500000 1.347955 1.300000 1.186645 1.176801 1.233026
14.600000 1.432481 1.360000 1.250325 1.239783 1.299991
14.700000 1.509522 1.420000 1.323286 1.313108 1.371243
14.800000 1.570545 1.480000 1.405743 1.397261 1.445709
14.900000 1.604991 1.540000 1.497910 1.492730 1.522319
15.000000 1.600000 1.600000 1.600000 1.600000 1.600000

dyx0 =

0.5007

xymin =

13.8000 0.9828
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值