how to make trendline in matlab using polyfit command?

34 vues (au cours des 30 derniers jours)
Pritha Pande
Pritha Pande le 22 Mai 2017
I have 1:37 values in x-axis.X-axis represents years ranging from 1980 to 2016 and 1:37 values in y axis represents value of ozone data. So for this the command i am running is a=xlsread('H:\season\Ranking.xlsx','Sheet4'); x=[1:37] for i=1:37; slope(i)=polyfit(x,a(:,i)',2); end; Help me in correcting it.
  2 commentaires
Jan
Jan le 22 Mai 2017
Before we can correct this, you have to explain, what is wrong. Do you get an error message or do the results differ from yopur expectations? In the latter case, please explain both.
Pritha Pande
Pritha Pande le 22 Mai 2017
i tried running this command now, B = [1980 1981 1982 1983 1984 1985 ]; C = [0.500 0.500 0.756 0.662 0.605 0.579 ]; scatter(B,C) hold on ; my_poly=polyfit(B,C,3); X2= 1980:1985; % X data range Y2=polyval(my_poly,X2);
plot(X2,Y2); i have taken only few values from my data. i got the plot also now but they have commented that polynomial is badly conditioned. how to improve it(they asked to try centring and scaling the value),plus i want my trendline to touch every point on graph.(so that it gives clearer picture where and to what extend line is rising or falling)

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 22 Mai 2017
Modifié(e) : Star Strider le 22 Mai 2017
You only need to do this:
prms = polyfit(x,a,1);
to get a linear (first-degree polynomial) trend, with ‘prms(1)’ being the slope and ‘prms(2)’ the intercept. Here, ‘x’ and ‘a’ both have to have the same row and column sizes. (The last argument to polyfit is 2 if you want a quadratic fit.)
Use the polyval function to evaluate the line so you can plot it.
EDIT (18:34 UTC)
‘i want my trendline to touch every point on graph.(so that it gives clearer picture where and to what extend line is rising or falling)’
Use the interp1 function with 'spline' or some other method appropriate to what you want to do.
B = [1980 1981 1982 1983 1984 1985 ];
C = [0.500 0.500 0.756 0.662 0.605 0.579];
Bi = linspace(min(B), max(B));
trend_line = interp1(B, C, Bi, 'spline');
figure(1)
plot(B,C,'pg', Bi,trend_line,'-r')
grid
See the documentation on interp1 for details and method options.
  2 commentaires
Pritha Pande
Pritha Pande le 22 Mai 2017
Thank you so much.
Star Strider
Star Strider le 22 Mai 2017
As always, my pleasure.

Connectez-vous pour commenter.

Plus de réponses (1)

Alex Backer
Alex Backer le 1 Juin 2020

Catégories

En savoir plus sur Interpolation dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by