MATLAB: I want to add a line of best fit to the plot using the polyfit function.

bestfit

%Plot graph 1
figure(2); clf
plot(xL(A),anglex,'ko');

Best Answer

You can do something like this
p = polyfit(xL(A),anglex,1);
anglexFit = polyval(p,xL(A));
plot(xL(A),anglex,'ko',xL(A),anglexFit)
I'm assuming your variable A, is a vector of indices that give the elements of xL that you want plotted and fit.
Also you can enter the command doc polyfit on the command line and it will bring up the documentation for polyfit, which has some nice examples of plotting best fit lines.
Related Question