putting a trendline on a semi-log plot in MatLab.

25 次查看(过去 30 天)
The values are as following,
avgspeed = [27.7846701084929; 31.1385896602218; 33.3643634350556; 37.0654321948195; 38.5042712877777; 41.3807720015859] ;
height = [13; 33; 55; 155; 245; 519];
semilogy(avgspeed, height)
How do I code to find the equation of trendline for this semi-log plot?

采纳的回答

William Rose
William Rose 2021-3-4
To add the trendline quation to the plot, see last two lines of the code below.
avgspeed = [27.78; 31.14; 33.36; 37.07; 38.50; 41.38]; %the x values
height = [13; 33; 55; 155; 245; 519]; %the raw y values
%next: make a matrix with a column of ones and a column of the x values
X=[ones(length(avgspeed),1) avgspeed]; %X=(column of ones,column of x values)
Y=log10(height); %log(height), for the regression
%next line: the standard matrix equation for a linear regression
b=inv(X'*X)*X'*Y; %b(1)=intercept, b(2)=slope
predheight=10.^(X*b); %the heights predicted by the linear regresison
semilogy(avgspeed,height,'rx',avgspeed,predheight,'r-');
xlabel('Avg.Speed'); ylabel('Height');
textstr=sprintf('Pred.Height=\n10^{(%.3f+%.3f*AvgSpeed)}',b(1),b(2));
text(27,520,textstr);
The code above produces the plot below.

更多回答(2 个)

William Rose
William Rose 2021-3-3
编辑:William Rose 2021-3-3
Here is the solution. Your question demonstrates Matlab's power and code efficiency: it takes only 6 lines of code, after the data is specified, and no toolbox functions are needed. Most of the text below is just comments.
avgspeed = [27.78; 31.14; 33.36; 37.07; 38.50; 41.38]; %the x values
height = [13; 33; 55; 155; 245; 519]; %the raw y values
%next: make a matrix with a column of ones and a column of the x values
X=[ones(length(avgspeed),1) avgspeed]; %X=(column of ones,column of x values)
Y=log10(height); %log(height), for the regression
%next line: the standard matrix equation for a linear regression
b=inv(X'*X)*X'*Y; %b(1)=intercept, b(2)=slope
predheight=10.^(X*b); %the heights predicted by the linear regresison
semilogy(avgspeed,height,'rx',avgspeed,predheight,'r-');
xlabel('Avg.Speed'); ylabel('Height');
And the output is below:
If you need to report the equation for the predicted height, it is:
predheight=10^(b1 + b2*avgspeed)
where b=[b1;b2] is calculated in the code.
  1 个评论
Shinichiro Shimata
Thank you so much!
I am still having a hard time displaying equation of the trendline. Could you help me further?
Thanks,

请先登录,再进行评论。


Shinichiro Shimata
Man, thank you again!!

标签

Community Treasure Hunt

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

Start Hunting!

Translated by