37 views (last 30 days)

Show older comments

i have a data set (x,y) that i have to approximate with a piecewise linear function, such that lower and upper bounds are satisfied. the image show what's the result I would like to achieve (in red the data set's points; in grey, the linear continuous approximation i would like to describe)image of result to achieve.

Using Matlab, i've divided all points in groups (one for every linear segment) and approximated every linear segment, with needed constraints, in the sense of least squares. But i need to have a continuous function and I am stuck in solving the problem. There's someone that have an idea of what i can do to solve the problem?

thanks in advance to everyone

John D'Errico
on 8 Apr 2021 at 22:33

Edited: John D'Errico
on 8 Apr 2021 at 22:35

Don't do it that way!!!!!!! You cannot achieve a continuous piecewise linear function that way. Instead, use a tool designed to solve that problem. For example:

x = rand(1,100);

y = sin(pi*x);

slm = slmengine(x,y,'knots',0:0.2:1,'degree','linear','plot','on');

So piecewise linear segments, fit using least squares. Breaks at the points 0:.2:1.

You can find my SLM toolbox for free download from the file exchange, here:

You can use the resulting structure, evaluating the function, or you could even extract the linear segments.

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

Start Hunting!