当前位置:网站首页>Function fitting based on MATLAB
Function fitting based on MATLAB
2022-07-28 19:50:00 【biyezuopinvip】
Function fitting
The whole idea
Divide the given coordinate points into 6 Section , Left 、 The lower left 、 Next 、 Right 、 The upper right 、 On ,6 part .
The lower left and upper right parts are fitted with quadratic functions , Other parts are fitted with a first-order function .
Observation data
The first 1-76 Points are linearly fitted as the left part
The first 67-96 Points as the lower left part of the quadratic fitting
The first 84-215 Points as the lower part of the linear fitting
The first 216-275 Points as the right part of the linear fitting
The first 269-298 Points as the upper right part of the quadratic fitting
The first 296-382 Points as the upper part of a fitting
Code implementation
First get x、y Scatter vector of data , And then call polyfit Library function , Return the function parameters of linear regression , Then initialize the denser x Abscissa as the abscissa of the sample , Use the obtained fitting function to substitute the abscissa of the sample point to obtain the ordinate of the sample point . At this time, more dense samples pass plot The function displays , You can get the fitting effect picture
- Final effect

It can be seen that , Fit by quadratic function at the bottom left and top right , Use linear fitting of the first-order function at other positions
- Problems encountered and solutions
When the fitting coordinates are initially selected , stay 6 Curve part , The point sets selected by each part do not intersect ( Mutually exclusive ), At this time, the rendering is not ideal : as follows

It can be seen that there is a large space in the middle of the curve , To solve this problem , Constantly adjust the interval of the point set ( Expand... Appropriately ), Make the point sets of each interval partially intersect with each other , Point for example 1-76 It's the first part , spot 67-96 It's part two , At this point 67-76 Not only the points on the left part to fit linearly , It's the point in the lower left to fit twice , The effect will be much better , as follows :

- Fitting results :
On the left ( The first 1-76 A little bit ):y=0.0983x-381.095
The lower left ( The first 67-96 A little bit ):y=0.0055

+4.5245x+299.2073
Underside ( The first 84-215 A little bit ):y=0.0208x-600.9287
On the right ( The first 216-275 A little bit ):y=-0.0721x+383.0611
The upper right ( The first 269-298 A little bit ):y=-0.0056
+4.0934x-901.1372
above ( The first 296-382 A little bit ):y=0.011x-181.6471
Function interpolation
linear interpolation
The whole idea
First mark as 1 Points of are stored separately v1 Matrix , Time of each action point 、 Horizontal ordinate , Then mark each with 0 Missing points Q, Find the nearest point before and after it AB, use A and B Linear interpolation , The missing point is on the linear interpolation line AB On , And by the Q The time of is known ,AB The arrival time of is also known . Suppose the proportion of time (tQ-tA)/(tB-tA) And abscissa scale (xQ-xA)/(xB-xA) Solve the abscissa of the missing point equally , Then substitute the interpolation function , Get the coordinates of the missing points and store them in v2 Matrix , The missing point information of each behavior is finally used plot Draw the image
Code implementation
utilize coord The second column of is the abscissa , The third column is the ordinate , The first column is the time. The missing points are stored in v1 in , Missing points are stored in v2 in , The abscissa corresponding to the missing time is obtained by assuming that the object moves at a uniform speed in the projection of the abscissa , Then using interpolation function ( linear interpolation ) Get ordinate , utilize plot Functions depict curves . The interpolation function obtains the slope k And intercept b that will do
Slope k=(y2-y1)/(x2-x1)
intercept b by (x1y2-x2y1)/(x1-x2)
Temp_x,temp_y Used to temporarily store the horizontal and vertical coordinates of the missing point , Finally stored in v2 Matrix
Final effect

Cubic spline interpolation
The whole idea
- about 309 There are no missing points , There is 308 Intervals , Every interval is interpolated by cubic function
- Total needs 4*308 Parameters to be determined
- You need to 4*408 An equation
- among 309 The first is the interpolation condition of the points satisfied by the interpolation function, and the inner points satisfy : Function value 、 Derivative value 、 The second derivative is continuous
- It also needs to be 2 A boundary condition , Let the derivative at the first and last points be 0( Natural boundary starting conditions )
- The core idea is to solve this 4408 The first equation yields 4408 The result of coefficients .
- that M Is the coefficient matrix ,Y Is the result vector of the equation ,
- The coefficient matrix is filled with coefficients ( Corresponding to equation )
- And then use it M\Y You can get X, namely 4*408 Values of undetermined coefficients , Then describe these interpolation functions .
- It is also assumed that the projection of the object on the abscissa moves at a uniform speed
- Then the abscissa of the missing point can be determined by the time proportion , And then according to its range , Substitute the corresponding cubic function to obtain the value of the ordinate
Code implementation
Also save the missing points v1 matrix ,M Used to store coefficient matrix ,Y Used to store the result vector ,X Is the vector of the result coefficient of the calculated interpolation function
- Final effect
Rendering of interpolation function :

Interpolation rendering of missing points

Red is the interpolation result of missing points , Blue dots are not missing , Black is the interpolation curve
Polynomial interpolation
Realize the idea
The specific implementation considers the use of piecewise quadratic function interpolation , hold 309 Points of existence ,308 The intervals are divided into 154 Group , Each group has two intervals , Three points , Adjacent groups will share the existing interior points .
for example :x1、x2、x3 For the first set of three interpolation points ,x3、x4、x5 Is the interpolation point of the second group , Adjacent pairing x3 Shared . altogether 154*3 Two parameters need to be determined , Each quadratic function determines 3 Parameters .
There are also equations that can be listed 154*3 individual , The interpolation condition equation can be obtained by using undetermined coefficients in each interval .
Empathy , Fill in M matrix , and Y The result vector , The result vector of quadratic function coefficient is obtained by operation X
Code implementation
Used to store known points
Vx1 and vy1 Store the horizontal and vertical coordinates of the known points
Vx2 and vy2 Horizontal and vertical coordinates of missing points
The abscissa of the missing point is calculated by assuming that the projection of the object on the abscissa moves at a uniform speed
The ordinate of the missing point is calculated by interpolation function
Final effect

Comparison of interpolation methods
linear interpolation : The advantage is that the implementation is very simple , Just use the original points for linear interpolation , The disadvantage is that the connection at the segment is not smooth enough , It can only indicate the overall trend , The increase in detail is difficult to effectively express
Cubic spline interpolation : The advantage is that the effect is good , Function value at segment 、 Slope 、 Concavity and convexity are very smooth
The disadvantage is that the number of equations is relatively large 、 complex , Once there is a missing point exception , The final result will be subject to great fluctuations !
Piecewise polynomial interpolation ( secondary ): The advantage is that the form is simple , And the overall curve is relatively smooth , It should be said that it integrates the advantages of linear interpolation and cubic spline interpolation , It is better to !
边栏推荐
- 博途1200/1500PLC上升沿下降沿指令编程应用技巧(bool数组)
- Force buckle 1331. Array serial number conversion
- 在矩池云快速安装torch-sparse、torch-geometric等包
- MySQL8 Status Variables: Internal Temporary Tables and Files
- Leetcode Day2 consecutive numbers
- 利用STM32的HAL库驱动1.54寸 TFT屏(240*240 ST7789V)
- App自动化测试是怎么实现H5测试的
- source insight项目导入和使用教程
- Amazon launched Amazon one palm payment system, and the contactless palm vein recognition market is expected to explode
- String中常用的API
猜你喜欢

可转债概念表 x Notion 给你方便快捷的体验!

How navicate modifies the database name

Thoroughly understand bit operations -- and (&), not (~), or (|), XOR (^)

Force buckle 1331. Array serial number conversion

调用整数或字符数组函数里的参数应该传入啥

Saltstack configuration management

在矩池云快速安装torch-sparse、torch-geometric等包

11. Learn MySQL union operator

业务可视化-让你的流程图“Run“起来(4.实际业务场景测试)

Servlet learning notes
随机推荐
企业级分布式爬虫框架入门
串口接收应用——环形缓冲buffer
这个客制化键盘,秀翻我了~
开盘暴涨215%!国产信号链芯片企业芯海科技登陆科创板
并发程序设计,你真的懂吗?
云计算笔记part.2——应用管理
App自动化测试是怎么实现H5测试的
China's first chip stamp released: built-in 120um ultra-thin NFC chip
This customized keyboard turns me on~
Force buckle 1331. Array serial number conversion
lua语言的左对齐函数(手写)
WPF 实现带蒙版的 MessageBox 消息提示框
In order to develop high-end photoresist, Jingrui Co., Ltd. invested 75million yuan to purchase SK Hynix ASML lithography machine
Rust Getting Started Guide (crite Management)
博途1200/1500PLC上升沿下降沿指令编程应用技巧(bool数组)
美国将提供250亿美元补贴,鼓励英特尔等芯片制造商迁回产线
【NPP安装插件】
Oracle insert数据时字符串中有‘单引号问题
Design of library management database system
The United States will provide $25billion in subsidies to encourage chip manufacturers such as Intel to move back to production lines