Hello, how do I display the equation for a polyfit line on this plot?

if true
% code
end
p = polyfit(x,y,1);
d = polyval(p,x);
plot(x,d,'k--')
where x and y are columns of 9, averaged from 3 original sets of 9 data point columns each.

6 Comments

Are you able to set the number of sigfigs that show for the m and b in mx+b
Cole, what is a "sigfig"? I've never heard of it.
Cole, you can set the number in the format string in sprintf() when you go to create the string. For example, to have 4 digits to the right of the decimal point, use %.4f:
txt = sprintf('y = %.4f * x + %.4f', m, b);
text(30, 0.5, txt, 'Color', 'r', 'FontSize', 15, 'FontWeight', 'Bold', 'HorizontalAlignment', 'left');
One tweak is that you can include + in the format specifier itself to avoid "+ -<number>" while displaying the sign. Look at the linear term in txt1 and txt2.
a = 2;
b = -pi;
c = exp(1);
txt1 = sprintf('y = %.4f*x^2 +%.4f*x +%.4f', a, b, c)
txt1 = 'y = 2.0000*x^2 +-3.1416*x +2.7183'
txt2 = sprintf('y = %.4f*x^2 %+.4f*x %+.4f', a, b, c)
txt2 = 'y = 2.0000*x^2 -3.1416*x +2.7183'
How would you add this equation into the legend?

Sign in to comment.

 Accepted Answer

Use text() to display the equation on the graph:
grid on;
% Place equation in upper left of graph.
xl = xlim;
yl = ylim;
xt = 0.05 * (xl(2)-xl(1)) + xl(1)
yt = 0.90 * (yl(2)-yl(1)) + yl(1)
caption = sprintf('y = %f * x + %f', p(1), p(2));
text(xt, yt, caption, 'FontSize', 16, 'Color', 'r', 'FontWeight', 'bold');

11 Comments

Thank you. May I ask where the 0.05 and 0.90 came from?
I just tweaked the numbers to get the upper left corner of the displayed text to be where I wanted it. Adjust the numbers and you'll see how you can move the text around. You can put it anywhere in the graph you want, if you adjust those numbers.
You can also put the axes at the origin if you do this
ax = gca
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
Let me know how to plot the R square into that equation?
How to get the equation of xt and yt?
There is no equation. They're just the x and y location of the text. Use whatever method you want to place the text wherever you want. I just decided to place it 5% of the way over and 90% of the way up, but that was just arbitrary. You can decide on a different location than I did if you want.
Nice work. Can you help me to add the graph with their linear regression with the same method?
How would you display this txt in the legend instead of on the plot?
Using a "fake" curve fit:
plot(0:10, (20:2:40) + 4*rand(11,1)' - 2, 'o', 0:10, (20:2:40), '--')
p = [2 20]; % made up fit coefficients
caption = sprintf('y = %f * x + %f', p(1), p(2));
legend('Data', caption, 'Location', 'best')
grid on

Sign in to comment.

More Answers (1)

Here is an example of how you can do this.

There are more efficient ways (e.g. using polyval on both the min and max at the same time), but I thought this might be clearer.

rng default
N = 10;
x = randn(N,1);
y = x + 0.3*randn(N,1);
p = polyfit(x,y,1);
x_min = min(x);
x_max = max(x);
d_min = polyval(p,x_min);
d_max = polyval(p,x_max);
figure
hold on
scatter(x,y)
plot([x_min x_max],[d_min d_max],'k--')

3 Comments

I updated my question to show the plot I am referring to. Also, x and y are described above as averages of 3 trials.
Ah, I misunderstood the question. I thought you wanted to graph the equation, and it was not working.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!