Online polynomial linear curve fitting in Simulink

2 次查看(过去 30 天)
How to fit curves in Simulink and still generate the code?
I have a model of a sensor in Simulink, that returns vectors "Pos_x" and "Pos_y". Each vector has size of 20x1, whose values change every one step time (1ms).
I am trying to calculate the coeffecients of the 3rd degree Polynomial y(x) = p1*x^3 + p2*x^2 + p3*x + p4 in Simulink, that fits the data.
I did not find any block in simulink that calculates the coeffecients, so I used a simple Matlab function
function [p1, p2, p3, p4] = fcn(x,y)
% f(x) = p1*x^3 + p2*x^2 + p3*x + p4
f = fit(x', y', 'Poly3'); % I have also tried "polyfit"
p1 = f.p1;
p2 = f.p2;
p3 = f.p3;
p4 = f.p4;
end
but I get the error: Function 'fit' not supported for code generation.
1- so back to my qustion, How to fit curves in Simulink and still generate the code?
2- I am also open to any other suggestion

采纳的回答

Jonas
Jonas 2021-8-31
If you have the DSP Systems Toolbox you can use the Least Squares Polynomial Fit block.
Standard Simulink does not have an alternative as far as I can see. Unless you implement a curve fitting algorithm yourself in a MATLAB Function block. If it is just for testing some stuff the DSP Systems Toolbox seems the alternative.
  2 个评论
Ehab Abdelkarim
Ehab Abdelkarim 2021-8-31
I have found the block but I could not use it, because it accepts only one input, and in my case both X and Y should be Inputs, soI have tried to manipulate the block, so that it accepts 2 inputs but it did not work for me.
Ehab Abdelkarim
Ehab Abdelkarim 2021-8-31
Thanks for the idea Jonas. finally I could manipulate the Block to adapt to my case.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

产品


版本

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by