开发者

Line of best fit scatter plot

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 a开发者_如何学运维llowed line types found. Nothing done'

Any help would be great,

Thanks, Jon.


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);


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜