Can I get the best fit curve from the plot of many scattered points?
3 views (last 30 days)
Show older comments
Hi guys,
Do you know how can I make a fit curve from the plot of scattered points?. I need a fitted line with equation.
PS. I also attached the plot as I told you above.
Thanks in advance
3 Comments
John D'Errico
on 25 Oct 2016
Edited: John D'Errico
on 25 Oct 2016
A fit to what? What shape do you expect? What model will you pose? Random numbers? Are you asking for something like a random distribution here? I suppose then you might decide to model this process as a (perhaps normal) distribution with a variance that depends on x.
Otherwise, the best fit to that data is, in my judgement, a constant. So just take the mean of your data in y. That is the least squares estimate of a constant function. Zero seems pretty close to me though.
Answers (2)
Yifan Men
on 25 Oct 2016
If you have the x and y data in the workspace, a quick way is to open the Curve Fitting App and set the x and y data. Then you will be able to choose the most common curve shape from the list and see which one best fits your data.
0 Comments
dpb
on 25 Oct 2016
Well, given the followup response, it's pretty simple to estimate the coefficients of a line given any set of data--of course, the results are going be residuals that look essentially the same as the values: but, maybe that's the desired result.
Given you have the data as x, y, then
ix=(x<1); % indices of the under-one x data
b=polyfit(x(ix),y(ix),1); % coefficients of a line for above data
As John notes, if you want to force a zero-slope line, then the least-squares estimate of the slope and intercept is simply
b=[0 mean(y(ix)]);
To evaluate the fit,
yhat=polyval(b,x(ix));
You can, of course, compare the two sets of coefficients from the above two cases and see just how much variation there is between the LS fit overall and the zero-slope case.
1 Comment
dpb
on 26 Oct 2016
Well, you commented "it didn't work for me" in unaccepting an answer which is rather pointless if you don't say what seems to be the problem you see.
It certainly does what you asked for; how well it represents the data was going to be marginal at best as we warned you going in...but, show the results and what you think isn't right...we don't have the data.
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!