sprajagopal
sprajagopal

Reputation: 350

MATLAB curve fit display equation on graph

Is there a way to display the curvefit equation on the graph produced without having to write it down myself manually every time? By GUI or command-line, anything is okay. Any hacks, some way to get around this?

Upvotes: 0

Views: 13441

Answers (1)

Buck Thorn
Buck Thorn

Reputation: 5073

Probably easiest to use the fit utility which is the non-graphical equivalent of using curvefit:

% sample data
x=[1:10]'; 
y = x+randn(10,1)*0.5; 
plot(x,y,'o')

pars=fit(x,y,'poly1');

pars contains the result of the fit, which you can overlay on the plot above with

hold on
plot(pars)

If you want to see the values of individual parameters, you can type pars.p1 or pars.p2 (for this example, there may be other parameters "pn" for other models)

To display on the figure, you can do something simple like

xpos=3;
ypos=9;
text(xpos,ypos,{num2str([pars.p1;pars.p2])})

For more info look into the documentation for curvefit or try help curvefit or help fit.

Upvotes: 2

Related Questions