Multiple trend-lines for one dataset

Dear community users,
I have a question related to the usage of multiple trend-lines on one dataset. Suppose I have a scatter plot with the following data:
Y=[0 0.25 0.5 0.75 1 2 3 4 5 6 7 8 9 9.25 9.5 9.75 10];
X=1:length(Y);
p = polyfit(X,Y,1);
P = polyval(p,X);
scatter(X,Y)
hold on
plot(X,P)
When plotting this data, a clear 'shape' can be seen: the plot is composed of three distinct linear section. Obviously, the actual scatter data contains lots more data points, and also a less distinct 'bending point' within the graph, however a clear shape of three linear lines can be seen.
It is very easy to plot one trend-line, however this will be wrong, as can be seen in the above plot. I want to create three distinct linear trend-lines, which I can also do by manually altering the polyfit data. However, I have to process a lot of data, and not all sets are equal in dimensions, meaning that I have to alter it manually every step.
Is there a way for MATLAB to create multiple of such plots, based on the data? Which means that if I alter the data, the resulting trend-lines will be altered automatically?
Thanks in advance!

Réponses (1)

Jackie Corbitt
Jackie Corbitt le 30 Août 2018

0 votes

This is also my question, would love input on this!

Community Treasure Hunt

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

Start Hunting!

Translated by