当前位置:网站首页>[matlab] data interpolation
[matlab] data interpolation
2022-06-25 17:30:00 【No card, no change】
Data interpolation
The difference between interpolation and fitting :
- Implementation method : Interpolation requires the curve to pass through the sample points , The fitting does not need to pass through the sample points , Only the minimum overall error is required .
- The form of the result : Interpolation is a piecewise approximation of sample points , There is no same approximation function ; Function fitting uses a function to approximate , There is a complete expression .
- Focus on : Interpolation can be used to estimate the function values corresponding to some points in the interval ; Fitting can not only estimate the points in the interval , You can also predict points outside the interval .
- applications : Interpolation is often used for precise data sets ; Fitting is often used in statistical data sets .
The role of data interpolation : In a given number of data , Simulate a curve through these known data , We can get the data information of unknown points according to the curve .
interp1 function
Y1=interp1(X,Y,X1,method): One dimensional interpolation function . according to X、Y Value , The calculation function is in X1 Place the value of the . among ,X、Y It's two known vectors of equal length , Represents the sample point and the sample value respectively .X1 It's a vector or a scalar , Represents the point to be interpolated .
method For the string , Optional values are :
- linear, Default , linear interpolation , Broken line .
- nearest, Nearest interpolation , Stepped .
- pchip, Piecewise cubic polynomial interpolation , The first derivative of the interpolation function of adjacent segments at several nodes is equal , It's smooth .
- spline, Cubic spline interpolation , Piecewise construct cubic polynomials , Satisfy that there are continuous first-order and second-order derivatives at each node , Very smooth .
Why? ’pchip’ and ’spline’ Both interpolation methods use 3 Degree polynomial instead of higher degree ?
Polynomial degree is not the higher the better . The more times , The more likely it is to oscillate and deviate from the original function , This phenomenon is called Runge (Runge ) The phenomenon .
%% All interpolation methods
xdata=0:pi/6:2*pi;
ydata=sin(xdata);
x=0:pi/20:2*pi;
subplot(4,2,1)
y=interp1(xdata,ydata,x,'nearest');
plot(xdata,ydata,'p',x,y,'k-')
title('nearest')
subplot(4,2,2)
y=interp1(xdata,ydata,x,'next');
plot(xdata,ydata,'p',x,y,'k-')
title('next')
subplot(4,2,3)
y=interp1(xdata,ydata,x,'prevoius');
plot(xdata,ydata,'p',x,y,'k-')
title('prevoius')
subplot(4,2,4)
y=interp1(xdata,ydata,x,'linear');
plot(xdata,ydata,'p',x,y,'k-')
title('linear')
subplot(4,2,5)
y=interp1(xdata,ydata,x,'spline');
plot(xdata,ydata,'p',x,y,'k-')
title('spline')
subplot(4,2,6)
y=interp1(xdata,ydata,x,'pchip');
plot(xdata,ydata,'p',x,y,'k-')
title('pchip')
subplot(4,2,7)
y=interp1(xdata,ydata,x,'cubic');
plot(xdata,ydata,'p',x,y,'k-')
title('cubic')
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-MFqthilh-1655005078618)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210901162122743.png)]](/img/4a/3a9c8344773dccd893d67e73716d82.png)
Add : Newton interpolation code :( I don't know the application scenario )
function s=newtoninterp(xdata,ydata,x) % xdata and ydata For known point data ,x To request point data
%Newton interpolation at notes (xdata,ydata)
n=length(xdata);
s=ydata(1);xx=1;
for k=2:n
xx=xx.*(x-xdata(k-1));
for l=k:n
ydata(l)=(ydata(l)-ydata(k-1))/(xdata(l)-xdata(k-1));
end
s=s+xx*ydata(k);
end
end
Other usages and similar function references
interp2 function
interp2(x, y, z, x1, y1, method): Two dimensional interpolation .x、y It can be either grid data or vector ,z Corresponding to the grid data z coordinate ,x1、y1 To request data point information .
application
Braking distance of motor vehicles
Driving , Start when the driver sees the obstacle , The shortest distance required to make a judgment and take braking measures to stop is called stopping sight distance . Stopping sight distance consists of three parts : One is the driving distance within the driver's reaction time ( Reaction distance ); The second is the distance from the start of braking to the complete stop of the vehicle ( Braking distance ); The third is the safe distance between the vehicle and the obstacles when the vehicle stops . among , The braking distance is mainly related to the driving speed and the type of road surface . According to the test , When a certain type of vehicle runs on Asphalt Pavement in wet weather , Its driving speed ( Company : km/h) Distance from braking ( Company :m) The relationship is shown in the following table .
| Speed | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | 110 | 120 | 130 | 140 | 150 |
| Braking distance | 3.15 | 7.08 | 12.59 | 19.68 | 28.34 | 38.57 | 50.4 | 63.75 | 78.71 | 95.22 | 113.29 | 132.93 | 154.12 | 176.87 |
Suppose the reaction time of the driver is 10s, The safe distance is 10m. Excuse me, :
① According to the actual vision and visual habits of a driver , The effective sight distance when driving is 120m, When it is driving on the road , The maximum speed per hour should not exceed ( Result rounding )?
② If the data in the table is used as a reference , Design a maximum speed of 125km/h The highway , Then the designer should ensure that the visible distance of the driver at any point on the highway is how many meters ?
Set the speed to v v v, The stopping sight distance is d d d, The reaction distance is d 1 d_1 d1, The braking distance is d 2 d_2 d2, The safe distance is d 3 d_3 d3, The reaction time is a s a_s as, be
d = d 1 + d 2 + d 3 d=d_1+d_2+d_3 d=d1+d2+d3
among , d 1 = a s v d_1=a_sv d1=asv, d 2 d_2 d2 by v v v Function of , d 3 d_3 d3 It is known that .
First question : According to the actual vision and visual habits of a driver , When driving Effective sight distance ( That is, the maximum distance )120m, When it is driving on the road , The maximum speed per hour should not exceed ( Result rounding )?
The known reaction time is 10s, The safe distance is 10m, May adopt Method of solving equation :
10 v + d 2 + 10 = 120 10v+d_2+10=120 10v+d2+10=120
The problem is , d 2 d_2 d2 yes v v v Function of , But the functional relationship is unknown , The equation is not solvable .
Now consider Data interpolation method , Take the data in the table as a sample , Perform data interpolation , To calculate the 120m The speed index corresponding to the stopping sight distance .
Programming ideas :
First step : Establish speed and stopping sight distance vectors .
The second step : With 1 In units of , Interpolate all velocities in the sampling interval , Calculate the corresponding stopping sight distance .
The third step : Calculate the stopping sight distance 120 Corresponding speed .
Step four : Graphic display .
How to stop according to sight distance 120 Find the corresponding speed ?
First step : Let the vector representing the stopping sight distance d i d_i di subtract 120, Then take the absolute value , Get a new vector x.
The second step : take x In ascending order , And record the serial number of the smallest element , This serial number is the stopping sight distance 120 The corresponding velocity data is in the vector v i v_i vi The serial number in .
The third step : Obtain speed data according to serial number .
v=20:10:150;
vs=v*(1000/3600); % Unit conversion km/s m/s
d1=10*vs;
d2=[3.15,7.08,12.59,19.68,28.34,38.57,50.4,63.75,78.71,95.22,113.29,132.93,154.12,176.87];
d3=10;
d=d1+d2+d3;
vi=20:150;
di=interp1(v,d,vi,'spline'); % interpolation
%% The following is a selection equal to 120 Corresponding abscissa method , There's not only one way !
%% Select and 120 The abscissa of the number with the smallest difference
x=abs(di-120);
[y i]=sort(x);
vi(i(1)) % The first is the smallest % 36
plot(vi,di,vi(i(1)),di(i(1)),'rp') % Points that meet the requirements
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-pGs0eU66-1655005078619)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210901174509920.png)]](/img/ef/cee3c3f9815bb7d199f5f88a18ec5f.png)
Second questions : Design a maximum speed of 125km/h The highway , Then the designer should ensure that the visible distance of the driver at any point on the highway is how many meters ?
j=find(vi==125); % Find a top speed of 125 The index of the corresponding distance
di(j)
plot(vi,di,125,di(j),'rp')
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-iB88xVth-1655005078620)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210901174931582.png)]](/img/b8/d7e1a5f7c6f56c8312a1fb5d517ac6.png)
Sand table making problem
A ground force is divided into red and blue sides in a designated unfamiliar area ( Flat areas [0,2000]×[0,2000] Inside , Company :m) Conduct operational exercises . During the exercise , The red investigation unit has measured the elevations of some places as shown in the following table .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-u56TMchA-1655005078620)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210901175120108.png)]](/img/d0/f1115833ae53d891154f51ec723e50.png)
① According to the data in the table , Making a military sand table .
② Within the scope of the exercise , The party occupying the largest highland will gain the advantage of condescending . Which area should the red side seize first .
Their thinking :
First question : Using two-dimensional interpolation to estimate data , To facilitate the production of military sand table .
Second questions : On the basis of interpolation , Draw contour map , Find the biggest highland .
x=0:200:1800;
y=x;
z = [2000,2000,2001,1992,1954,1938,1972,1995,1999,1999;
2000,2002,2006,1908,1533,1381,1728,1959,1998,2000;
2000,2005,2043,1921, 977, 897,1310,1930,2003,2000;
1997,1978,2009,2463,2374,1445,1931,2209,2050,2003;
1992,1892,1566,1971,2768,2111,2653,2610,2121,2007;
1991,1875,1511,1556,2221,1986,2660,2601,2119,2007;
1996,1950,1797,2057,2849,2798,2608,2303,2052,2003;
1999,1999,2079,2685,3390,3384,2781,2165,2016,2000;
2000,2002,2043,2271,2668,2668,2277,2049,2003,2000;
2000,2000,2004,2027,2067,2067,2027,2004,2000,2000];
surf(x,y,z);
colormap(mycolor)
%% After interpolation
x1=0:100:1800; % With 100 interval
y1=x1';
z1=interp2(x,y,z,x1,y1,'spline');
figure(2)
surf(x1,y1,z1);
colormap(mycolor)
x1=0:50:1800; % With 50 interval
y1=x1';
z1=interp2(x,y,z,x1,y1,'spline');
figure(3)
surf(x1,y1,z1);
colormap(mycolor)
%% Draw contours
figure(4)
[c h] = contourf(x1, y1, z1, 10)
clabel(c, h) % Displays the height represented by contour lines
colorbar % Display height color table
colormap(mycolor)
%% The following is just my favorite color map
mycolorpoint=[[0 0 16];
[8 69 99];
[57 174 156];
[198 243 99];
[222 251 123];
[239 255 190]];
mycolorposition=[1 11 33 50 57 64];
mycolormap_r=interp1(mycolorposition,mycolorpoint(:,1),1:64,'linear','extrap');
mycolormap_g=interp1(mycolorposition,mycolorpoint(:,2),1:64,'linear','extrap');
mycolormap_b=interp1(mycolorposition,mycolorpoint(:,3),1:64,'linear','extrap');
mycolor=[mycolormap_r',mycolormap_g',mycolormap_b']/255;
mycolor=round(mycolor*10^4)/10^4;% Retain 4 Decimal place
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-rNYt0VvZ-1655005078621)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210901175811791.png)]](/img/f7/d98e941cd75ba4dc313ada78000c8b.png)
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-nmJlZwNO-1655005078622)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210901180022787.png)]](/img/92/acac5da7d329b87c8047af30074768.png)
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-qvqdgFG3-1655005078622)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210901181636007.png)]](/img/13/b2353daf75ed47dcb99944910202e9.png)
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-cskFz0Xp-1655005078623)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210901181649546.png)]](/img/78/c656a2f765a563f3615a344225bcb0.png)
contour Functions and contourf Function explanation
Here is only the basic usage :
[c,h] = contourf(X,Y,Z,t); X、Y For grid data or vectors ( And mesh The parameter usage in is consistent ),Z Is the function value ,t For the spacing between contour lines and the range of contour lines ( It can be understood that the larger the contour line, the denser the contour line ); The return value does not know the meaning .
clabel(c, h) : Dimension the height of the contour line ( Although I don't know the meaning of the return value , But it can be used directly ).
colorbar: Color label .
contour Function and contourf Function usage is consistent , It's just contour Lines are displayed , There is no color fill between lines ; and contourf There is a color fill between the lines .
figure('NumberTitle', 'off', 'Name', 'contourf'); % Modify the drawing window name
contourf(x1, y1, z1, 10)
title('contourf')
colormap(mycolor)
figure('NumberTitle', 'off', 'Name', 'contour');
contour(x1, y1, z1, 10)
title('contour')
colormap(mycolor)
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-x2RKuB4u-1655005078624)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210901182134775.png)]](/img/7e/e4c91a55c61051cbdf7ce93ba8ba30.png)
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-CzHgztck-1655005078624)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210901182145350.png)]](/img/7e/bd3338bb5b2fff300ac713fe546264.png)
边栏推荐
- 有关QueryInterface函数
- Mathematical modeling - linear programming
- Website arrangement of super all metal PBR multi-channel mapping materials
- TCP聊天+传输文件服务器服务器套接字v2.8 - 修复已知程序4个问题
- The role of the project manager in the project
- 什么是公链开发?公链开发项目有哪些?
- [UVM practice== > episode_2] ~ VIP, VIP development, VIP release
- 中断操作:AbortController学习笔记
- 求满足条件的最长子串长度
- cgi通过odbc连接数据库
猜你喜欢
随机推荐
汇编语言(5)寄存器(内存访问)
【编译原理】词法分析
杰理之获取复位源和唤醒的 IO 口的方法【篇】
IDEA全局搜索汉字[通俗易懂]
PSYNC command of redis
Mobx learning path - powerful "state management tool"
golang sort slice int
杰理之系统时钟设置出现复位或无效问题【篇】
【UVM实战 ===> Episode_1 】~ MCDF设计更新、AMBA标准接口、UVM验证环境更新
STM32 hardware error hardfault_ Handler processing method
【UVM实战 ===> Episode_2 】~ VIP、VIP的开发、VIP的发布
揭秘GES超大规模图计算引擎HyG:图切分
杰理之SPI 从机使用注意事项【篇】
tasklet api使用
Best practices for data relocation: using CDM to relocate offline Mysql to DWS
有关QueryInterface函数
Kotlin入门(20)几种常见的对话框
相同wifi下,笔记本连接台式机上的虚拟机
杰理之定时器使用注意事项【篇】
What are the steps for launching the mobile ERP system? It's important to keep it tight









