How do I name the trendline in the legend? Also, how can I get the trend line equation on the plot?

9 ビュー (過去 30 日間)
MatLab Help.PNG

回答 (2 件)

Bjorn Gustavsson
Bjorn Gustavsson 2019 年 3 月 19 日
Maybe:
if c(2) > 0
equation_string = sprintf('%4.3f x + %4.3f',c(1),c(2));
elseif c(2) < 0
equation_string = sprintf('%4.3f x - %4.3f',c(1),abs(c(2)));
else
equation_string = sprintf('%4.3f x',c(1));
end
legend('Data',['Best linear fit: ',equation_string])
text(12,43,equation_string)
HTH

Star Strider
Star Strider 2019 年 3 月 19 日
It is not possible to run an image of your code.
Try this:
Voltage = linspace(10.45, -1.4, 8);
GaugePressure = linspace(3353, 0, 8);
c = polyfit(Voltage, GaugePressure, 1);
y_est = polyval(c, Voltage);
figure
plot(Voltage, GaugePressure, '--bs')
hold on
plot(Voltage, y_est, 'r--')
hold off
legend('Data', 'TrendLine', 'Location','best')
Add your title and axis label calls.

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by