Matlab code fitting a line to a histogram

In summary, to plot a line of best fit over a histogram in Matlab 2010, you can use the polyfit command and specify the degree of the polynomial you want to fit.
  • #1
matt_crouch
161
1
Im analyzing some data from a previous student I am trying to plot a line of best fit over the histogram and hense find the value of the coefficiants

the files had to be loaded as -ascii so this is the code i have typed so far

x=load('filename.mat','-ascii'); mean(x); hist(x,300)

this then produces the graph how do i add a best fit line to this. I have the curve fit toolbox but don't seem to be able to use this to give me a plausable best fit. I am using Matlab 2010

any help would be appreciated
:)
 
Physics news on Phys.org
  • #2
You can use the polyfit command to fit a line of best fit. You will need to specify the degree of the polynomial you want to fit. For example: coefs = polyfit(x, y, 2); % Fit a second-degree polynomial to the data. yfit = polyval(coefs, x); % Compute y values for the fitted polynomial. plot(x, yfit) % Plot the fitted polynomial. This will give you the coefficients of the polynomial and allow you to plot the line of best fit on top of your histogram.
 

1. How do I fit a line to a histogram using Matlab code?

To fit a line to a histogram using Matlab code, you can use the polyfit function. This function takes in the x and y values of the histogram and returns the coefficients of the best-fit line.

2. Can I customize the line fitting in Matlab?

Yes, you can customize the line fitting in Matlab by specifying the degree of the polynomial fit, the color and style of the line, and other parameters in the polyfit function. You can also use the plot function to add additional customization to the line.

3. How do I plot the histogram and the fitted line together?

To plot the histogram and the fitted line together, you can use the hist function to plot the histogram and then use the hold on command to hold the plot. Next, use the plot function to plot the fitted line on the same graph.

4. Can I use a logarithmic scale for the histogram?

Yes, you can use a logarithmic scale for the histogram by using the log function on the x-axis of the plot. This will transform the data to a logarithmic scale and the fitted line will adjust accordingly.

5. How do I evaluate the goodness of fit for the line?

You can evaluate the goodness of fit for the line by calculating the R-squared value. This value represents the percentage of the variation in the data that is explained by the fitted line. A higher R-squared value indicates a better fit.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
891
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
961
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top