How to display regression equation without use of toolbox?

As mentioned, I don't want to use the toolbox for this. I want a piece of code.
I am am trying to compare data with the help of a fitting curve and I want the regression equation to be displayed in the plot. I know how to do it with the toolbox. And I have worked around this problem with the help of polyfit and then using text() for the plot. This leaves me with a new problem however, on defining the coordinates of where the text should be displayed. I also worked this out with saying: "display text at 80% of maximum value of the x-axis", which works for the most part but not, when my maximum value is 0 for example.
There should be a more elegant solution to this. Is there a function that directly displays the reg. eq. in a plot? And how can I define that I want the equation always to be displayed in the upper right corner for example?
Thanks for helping! Jana

Answers (2)

I suppose someone may have put a function like this into the file exchange, but I have always found it pretty straightforward to do it like this:
x = 0:0.1:10;
y = -x;
figure
plot(x,y,'b.',x,y,'r-')
xlim=get(gca,'XLim');
ylim=get(gca,'YLim');
text(0.8*xlim(1)+0.2*xlim(2),0.3*ylim(1)+0.7*ylim(2),'y = -x')
Notice that the coefficients of xlim(1) and xlim(2) add up to 100%, and similarly for ylim. This is just the generalization of what you've doing, to the case of a non-zeroed axis.
And for a generalization of a polynomial equation:
[P] = polyfit(x,y,order); %x,y,order polynomial
eqn = ['y = ' sprintf('%3.3fx^%1.0f + ',[P ;length(P)-1:-1:0])];
eqn = eqn(1:end-3);
%eqn will be a string representing the equation

Categories

Asked:

on 12 Apr 2011

Community Treasure Hunt

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

Start Hunting!