Matlab: Data from Best Fit Lines into Legend

In summary, the conversation discusses plotting data in MATLAB and using the polyfit function to calculate the best fit lines. The question is about how to display the polyfit equation in the legend of the graph. The suggested solution is to use the num2str command on the polyfit values.
  • #1
SeannyBoi71
84
0
Hey all, I am plotting some data using MATLAB and have a question.

Basically, this is what I'm trying to do. I am calling a bunch of data from Excel, plotting this data in MATLAB (it gives me two lines), and then calculating the polyfit values for each line so I can find the equation y = mx +b for each one. I have no problem Doing all of this, but what I want to do next is display the polyfit data in the legend of the graph. I'm confused if this is possible with the legend function.

10aYi.jpg


So in other words where it says 'Best Fit of Reflection 1' and 'Best Fit of Reflection 2' I want to somehow place the data calculated by polyfit in the form y = mx +b. Any suggestions? Thanks in advance.
 
Physics news on Phys.org
  • #2
This is simple in Excel, where you say you started. Chart the data, then pulldown the chart menu, select "add trendline" under options you can select to show the equations.
 
  • #3
I was aware of this method being the easy way out :tongue: I am pretty proficient with Excel and have only recently started using Matlab, so I was trying to familiarize myself a little bit more with graphing this way.
 
  • #4
Just use the num2str command on the values that polyfit returns
 
  • #5
Ok this is what I was looking for, thank you.
 

1. How do I add a best fit line to my Matlab plot?

To add a best fit line to your Matlab plot, you can use the 'polyfit' and 'polyval' functions. First, use 'polyfit' to determine the coefficients of the best fit line. Then, use 'polyval' to generate the y-values for the line using the coefficients and the x-values of your data. Finally, plot the best fit line using the 'plot' function.

2. Can I customize the appearance of the best fit line in my plot?

Yes, you can customize the appearance of the best fit line in your plot. You can use the 'plot' function to specify the line color, style, and thickness. Additionally, you can use the 'legend' function to add a label for the best fit line in the plot's legend.

3. How do I include the equation of the best fit line in the plot's legend?

To include the equation of the best fit line in the plot's legend, you can use the 'sprintf' function to create a string that represents the equation. Then, use the 'legend' function to add the string to the legend along with a label for the best fit line.

4. Can I add multiple best fit lines to the same plot?

Yes, you can add multiple best fit lines to the same plot. You can use the 'hold on' and 'hold off' commands to hold the current plot and add new lines to it. Simply repeat the steps for adding a best fit line for each set of data, and use the 'legend' function to label each line in the plot's legend.

5. How do I remove a best fit line from my plot?

To remove a best fit line from your plot, you can use the 'delete' function and specify the handle of the line you want to remove. The handle can be obtained by using the 'plot' function when adding the best fit line. Alternatively, you can also use the 'cla' function to clear the current axes and remove all lines from the plot.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
788
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
131
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
10
Views
967
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
Back
Top