Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How to display fit equation on plot?

Asked by Christopher on 17 Nov 2013
Latest activity Answered by Image Analyst on 18 Nov 2013

Hello all,

I have written code that creates a power fit to some data. The code is :

p=polyfit(log(pla_strain),log(pla_stress),1);
m=p(1);
b=exp(p(2));
yy=b.*(pla_strain).^m;

I think plot the data with the fit as follows:

loglog(pla_strain,pla_stress,'LineWidth',4)
hold on
plot(pla_strain,yy,'r--','LineWidth',2)
axis([.001  .1  40  100])
hold off

My problem is I am trying to display the equation of the fit on the plot. The equation is of the form y=b*x^m where b and m are the two coefficients from the polyfit. I would like to display this equation in the same location of the plot every time, which would ideally be towards the top center.

Thanks

0 Comments

Christopher

Tags

Products

2 Answers

Answer by G A on 18 Nov 2013
txt1='b*x^m';
yL=get(gca,'YLim'); 
xL=get(gca,'XLim');   
  text((xL(1)+xL(2))/2,yL(2),txt1,...
      'HorizontalAlignment','left',...
      'VerticalAlignment','top',...
      'BackgroundColor',[1 1 1],...
      'FontSize',12);

0 Comments

G A
Answer by Image Analyst on 18 Nov 2013

You're going to have to convert the x and yy back into pla_strain and pla_stress so you can plot them

x = log(pla_strain);
pla_strain_fitted = exp(x);
yy = m .* x + b; % This equals log(pla_stress)
pla_stress_fitted = exp(yy);
loglog(pla_strain_fitted, pla_stress_fitted, 'LineWidth',4)

0 Comments

Image Analyst

Contact us