当前位置:网站首页>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 !
边栏推荐
- 投资35.45亿元!格力集团参与小米产业基金
- WPF implements MessageBox message prompt box with mask
- There are five certificates in the soft examination advanced examination, which is more worth taking?
- 远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
- editor.md中markdown编辑器的实现
- MySQL8 Status Variables: Internal Temporary Tables and Files
- 时间转日期的sql语句应该怎么写?
- 峰值速率超2Gbps!高通在中国首发通过5G毫米波MIMO OTA测试
- After reading the thesis for three years, I learned to read the abstract today
- Leetcode Day5 delete duplicate email
猜你喜欢

Thoroughly understand bit operations - shift left, shift right

How openocd directly downloads programs to STM32 board through stlink (solved)

企业级分布式爬虫框架入门

leetcode day1 分数排名

Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)

Android-第十三节03xUtils-数据库框架(增删改查)详解

英语文章翻译-英语文章翻译软件-免费批量翻译

Getting started with saltstack

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

How to write the SQL statement of time to date?
随机推荐
博途1200/1500PLC上升沿下降沿指令编程应用技巧(bool数组)
基于C语言的信息管理系统和小游戏
架构基本概念和架构本质
Design of library management database system
Redis笔记
lua语言的左对齐函数(手写)
MySQL8 Status Variables: Internal Temporary Tables and Files
How openocd directly downloads programs to STM32 board through stlink (solved)
navicate修改数据库名的方式
leetcode day1 分数排名
个人博克系统登录点击图形验证码的集成与实现
Rust Getting Started Guide (modules and engineering structures)
英文翻译西班牙语-批量英文翻译西班牙工具免费
投资35.45亿元!格力集团参与小米产业基金
Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)
Leetcode Day1 score ranking
stc12c5a60s2功能说明(STC12C5A60S2默认触发)
Cell review: single cell methods in human microbiome research
MySQL性能测试工具sysbench学习
Thoroughly understand bit operations -- and (&), not (~), or (|), XOR (^)