MATLAB Answers

Regression

9 views (last 30 days)
How can we display the regression equation (obtained by camand "fit") in the plot?

Accepted Answer

the cyclist
the cyclist on 9 Feb 2012
Here is an example:
x = (1:20)';
y = 2 + 3*x + 7*rand(20,1);
model = fit(x,y,'poly1')
figure
plot(model,x,y)
The plot command here is an "overloaded" method for cfit objects (the output of the fit command). The documentation is here: http://www.mathworks.com/help/toolbox/curvefit/plot.html
  1 Comment
Farz Tak
Farz Tak on 14 Feb 2012
Thanks a lot, That was helpful

Sign in to comment.

More Answers (1)

Richard Willey
Richard Willey on 9 Feb 2012
Try the following
X = linspace(1, 2*pi, 50);
X = X';
Y = sin(X) + randn(50,1);
foo = fit(X,Y, 'sin1');
figure1 = figure;
scatter(X,Y)
hold on
plot(foo)
% Create textbox
annotation(figure1,'textbox',...
[0.147443514644351 0.802739726027397 0.305903765690377 0.0931506849315069],...
'String',{[formula(foo)]},...
'FitBoxToText','off',...
'BackgroundColor',[1 1 1]);
  1 Comment
Farz Tak
Farz Tak on 14 Feb 2012
This really helps me and even save me from using text command every time. Thanks. That was exactly what I wanted to know about this question.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!