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

6 views (last 30 days)
MatLab Help.PNG

Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 19 Mar 2019
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 on 19 Mar 2019
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.

Community Treasure Hunt

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

Start Hunting!