Info
This question is closed. Reopen it to edit or answer.
Plot trend line from data imported from excel
2 views (last 30 days)
Show older comments
Hi, I have used matlab for some time now, for importing data from excel and plotting it in matlab because the graphs look much better. This is the first time I have to do a trend line of the data. What is the way to do it because in insert there is no trend line option. In excel this is much simpler.
Do you have any idea how to do it?
I want to find trend line for the orange line separate from the blue line as well as a combined trend from both data sets.
I have attached a photo you can see the data there.
Best Regards,
Tsvetan
1 Comment
Star Strider
on 10 Jun 2017
Best to have clarified and added to the original post rather than posting a duplicate Question.
Answers (3)
dpb
on 10 Jun 2017
It's not much more than Excel altho a "trendline" is generally considered linear and certainly the orange curve isn't going to be well represented by such. But, basics are the same other than it takes more than the two endpoints to draw a smooth curve of higher degree than a straight line.
I'm going to use shorter variable names, though... :)
bUK=polyfit(year,UKdata,1); % trendline coefficients UK
bWW=polyfit(year,WWdata,1); % ditto worldwide...
hold on
yrs=[year(1) year(end)]; % first/last year in dataset
hLnUK=plot(yrs,polyval(bUK,yrs)); % UK trendline
hLnWW=plot(yrs,polyval(bWW,yrs)); % WW trendline
Set color, line style, etc., as desired...
If it's desired to annotate the lines, that's easy enough, too...
hTxtUK=text(year(ix),dataUK(ix),num2str('UK = %.1f*Year+%.1f',bUK);
where ix is a year index chosen to put the text at a convenient location; hard to choose when the plot is occluded by other windows. One can see there's a big spike at the RH end for WW; one presumes something similar for UK as well but not as pronounced.
"Salt to suit..." but should get you started...see the documentation of course for more details on all the above...
0 Comments
Star Strider
on 10 Jun 2017
If you just want to plot a linear least-squares fit to your data, see if the lsline (link) function will do what you want.
The polyfit and polyval functions can create polynomial fits to your data, and will return parameters for the fit.
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!