Matlab Displaying the Fit Equation On graph.

  • Context: MATLAB 
  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Fit Graph Matlab
Click For Summary
SUMMARY

The discussion focuses on displaying the fit equation on a graph in MATLAB using linear data. The user has successfully plotted capacitance against inverse distance and performed linear regression using the polyfit function. The coefficients obtained from polyfit are used to create a string representation of the equation, which is then displayed on the graph using the text function. The final solution includes the exact code needed to achieve this functionality.

PREREQUISITES
  • Familiarity with MATLAB programming environment
  • Understanding of linear regression concepts
  • Knowledge of plotting functions in MATLAB
  • Experience with string manipulation in MATLAB
NEXT STEPS
  • Learn how to customize MATLAB plot annotations
  • Explore advanced features of the polyfit function in MATLAB
  • Investigate other regression techniques available in MATLAB
  • Study MATLAB's text function for dynamic labeling
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and researchers who need to visualize regression results and annotate plots effectively.

Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
I have a lineer data and I want to write the equation of it on the graph.
The x-axis is inverse distance (1/m) and I showed as "d"
The y-axis is capacitance (C) and I showed it like "C"
My data name is d_C

Here my codes that I used for now
>> d=d_C(:,1);
>> C=d_C(:,2);
>> plot(d,C,'go');
>> hold on
>> xlabel('Inverse Distance (1/m)','Fontsize',21)
>> ylabel('Capacitance (C)','Fontsize',21)
>> title('Inverse Distance vs Capacitance,Part (A)','Fontsize',23)
>> grid on
>> p = polyfit(d,C,1)

p =

0.0003 0.0414

>> Cfit=polyval(p,d);
>> plot(d,Cfit,'-b');
>> h = legend('Data','Best fit','Location','southeast');
>> set(h,'Fontsize',20);

I just need to write the equation on the graph like probably y=0.0003x+0.0414 (I am not sure about this though).I need codes.I can't do it on manually

Any help would be great
Thanks
 

Attachments

  • Graph Pic.png
    Graph Pic.png
    9.3 KB · Views: 1,856
Last edited:
Physics news on Phys.org
From a MATLAB newsgroup post:

[p]=polyfit(...);
a = p(1)
b = p(2)
polyfit_str = ['y = ' num2str(a) ' *x + ' num2str(b)]
% polyfit_str will be : y = 4*x + 2
text(10,10,polyfit_str);
 
  • Like
Likes   Reactions: Arman777
Thanks a lot.I did it
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
7K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 9 ·
Replies
9
Views
4K