当前位置:网站首页>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 !
边栏推荐
- How does app automated testing achieve H5 testing
- 调用整数或字符数组函数里的参数应该传入啥
- 11. Learn MySQL union operator
- Leetcode Day1 score ranking
- Leetcode Day5 delete duplicate email
- adb remount of the / superblock failed: Permission denied
- Amazon launched Amazon one palm payment system, and the contactless palm vein recognition market is expected to explode
- Design of library management database system
- 彻底理解位运算——与(&)、非(~)、或(|)、异或(^)
- The opening price soared by 215%! Domestic signal chain chip enterprise Xinhai Technology landed on the scientific innovation board
猜你喜欢

leetcode day1 分数排名

Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation

Cloud computing notes part.1 - system management

English article translation - English article translation software - free batch translation

What parameters should be passed in calling integer or character array functions

Servlet学习笔记

Rust Getting Started Guide (crite Management)

App自动化测试是怎么实现H5测试的

Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers

Cell review: single cell methods in human microbiome research
随机推荐
Germany and Portugal have announced that they will not disable Huawei 5g equipment, but Germany will set strict restrictions!
并发程序设计,你真的懂吗?
彻底理解位运算——左移、右移
助力面板行业创新,FPGA将成屏厂TCON最佳选择?
R language and data analysis practice 11 - data deletion
leetcode day3 查找重复的电子邮箱
leetcode day5 删除重复的电子邮箱
美国将提供250亿美元补贴,鼓励英特尔等芯片制造商迁回产线
华为入股南京芯视界,布局固态激光雷达芯片领域
Rust Getting Started Guide (rustup, cargo)
After reading the thesis for three years, I learned to read the abstract today
2022年全国最新消防设施操作员(中级消防设施操作员)题库及答案
Verification of chip power consumption performance: from dilemma to transcendence
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)
Implementation of markdown editor in editor.md
NPM installing and uninstalling global packages
MySQL performance testing tool sysbench learning
Business visualization - let your flowchart "run" (4. Actual business scenario test)
亚马逊推出Amazon One手掌支付系统,非接触式掌静脉识别市场有望爆发