8

I'm trying to do a scatter plot with a line of best fit in matlab, I can get a scatter plot using either scatter(x1,x2) or scatterplot(x1,x2) but the basic fitting option is shadowed out and lsline returns the error 'No allowed line types found. Nothing done'

Any help would be great,

Thanks, Jon.

Jon
  • 141
  • 1
  • 1
  • 5

2 Answers2

22

lsline is only available in the Statistics Toolbox, do you have the statistics toolbox? A more general solution might be to use polyfit.

You need to use polyfit to fit a line to your data. Suppose you have some data in y and you have corresponding domain values in x, (ie you have data approximating y = f(x) for arbitrary f) then you can fit a linear curve as follows:

p = polyfit(x,y,1);   % p returns 2 coefficients fitting r = a_1 * x + a_2
r = p(1) .* x + p(2); % compute a new vector r that has matching datapoints in x

% now plot both the points in y and the curve fit in r
plot(x, y, 'x');
hold on;
plot(x, r, '-');
hold off;

Note that if you want to fit an arbitrary polynomial to your data you can do so by changing the last parameter of polyfit to be the dimensionality of the curvefit. Suppose we call this dimension d, you'll receive back d+1 coefficients in p, which represent a polynomial conforming to an estimate of f(x):

f(x) = p(1) * x^d + p(2) * x^(d-1) + ... + p(d)*x + p(d+1)

Edit, as noted in a comment you can also use polyval to compute r, its syntax would like like this:

r = polyval(p, x);
Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
  • 6
    you can use **polyval** to help you evaluate the polynomial – Amro Jan 08 '10 at 02:13
  • @Amro I have the same problem to find the best fit line. Are you sure the answer is correct? I am not sure because I think if the best fit line is selected correctly, The number of points above the line must be equal to number of points under the line, Is it right? I am not sure, what's your idea? – Fatime Aug 15 '13 at 13:03
  • @MarkElliot what is your idea about my above comment? – Fatime Aug 15 '13 at 13:05
  • 2
    @Fatime: not necessarily, keep in mind that simple linear regression is extremely sensitive to [outliers](https://en.wikipedia.org/wiki/Outlier). So if you have a rogue data point, it will skew the best fit line in its direction. There other types of regressions that introduce some form of [regularization](https://en.wikipedia.org/wiki/Regularization_%28mathematics%29) to be more robust and prevent [overfitting](https://en.wikipedia.org/wiki/Overfitting). – Amro Aug 15 '13 at 23:43
0

Infs, NaNs, and imaginaryparts of complex numbers are ignored in the data.

Curve Fitting Tool provides a flexible graphical user interfacewhere you can interactively fit curves and surfaces to data and viewplots. You can:

Create, plot, and compare multiple fits

Use linear or nonlinear regression, interpolation,local smoothing regression, or custom equations

View goodness-of-fit statistics, display confidenceintervals and residuals, remove outliers and assess fits with validationdata

Automatically generate code for fitting and plottingsurfaces, or export fits to workspace for further analysis