Matlab Displaying the Fit Equation On graph.

In summary, to write the equation of a linear data on a graph, you can use the polyfit function in MATLAB to find the coefficients and then use the text function to display the equation. This involves using the polyfit function to find the coefficients, creating a string using the coefficients, and then using the text function to display the string on the graph.
  • #1
Arman777
Insights Author
Gold Member
2,168
193
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,749
Last edited:
Physics news on Phys.org
  • #2
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 Arman777
  • #3
Thanks a lot.I did it
 

Related to Matlab Displaying the Fit Equation On graph.

1. How do I display the fit equation on a graph in Matlab?

To display the fit equation on a graph in Matlab, you can use the "text" function. This function allows you to specify the coordinates and text to be displayed on the graph. You will need to first calculate the equation of the fit using the "fit" function, and then use the "sprintf" function to format the equation in a readable format before using it in the "text" function.

2. Can I customize the appearance of the fit equation on the graph?

Yes, you can customize the appearance of the fit equation on the graph by using the "text" function's optional parameters. These include specifying the font size, font style, and color of the text. You can also adjust the position of the text on the graph by specifying the coordinates.

3. Is it possible to display multiple fit equations on the same graph?

Yes, it is possible to display multiple fit equations on the same graph. You can use the "text" function multiple times, each with a different equation and coordinates, to display multiple fit equations on the graph.

4. How can I save the graph with the fit equation displayed?

To save the graph with the fit equation displayed, you can use the "saveas" function in Matlab. This function allows you to specify the file name and format in which you want to save the graph, including the fit equation. You can also use the "print" function to save the graph as an image file.

5. Can I display the fit equation as a legend on the graph?

No, the "text" function does not allow you to display the fit equation as a legend on the graph. However, you can use the "legend" function in Matlab to create a legend that includes the fit equation along with the plot. Alternatively, you can use the "title" function to add a title to the graph that includes the fit equation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
Back
Top