Trend lines with moving average
Show older comments
Hi guys,
I have a data set with the following double values (Test.mat). When i plot the Data with scatter i get this figure:

I now need two types of trend lines. The first should be linear and the second should represent as many "curves" as possible. How can I achieve this with a moving average?
I need the trend line as a function so that I can insert it together with other trend lines into a new plot and compare them.
Many thanks for your help!
Accepted Answer
More Answers (2)
There are many ways to do the linear fit in MATLAB, including some in base MATLAB. Here is one way, using the fitlm function from the Statistics and Machine Learning Toolbox. I expect other folks will post answers using other functions.
load("Test.mat","x_Test","y_Test")
mdl = fitlm(x_Test,y_Test);
figure
hold on
scatter(x_Test,y_Test)
plot(sort(x_Test),predict(mdl,sort(x_Test)))
I'm unclear what you mean by the second thing you need, with "as many curves as possible". One could make a perfect fit through these points, with any number of curves (e.g. with a polynomial of extremely high order). You need to clarify what you mean here.
Also, I'm not sure what you mean by a moving average here. I know what a moving average is, but it's unclear how you want to include that along with a trend line.
Steven Lord
on 26 Feb 2023
0 votes
My first thought would be to try using the Remove Trends Live Editor Task or the trenddecomp function.
Categories
Find more on Fit Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
