Asked by Tom
on 6 Mar 2013

I have these points: -

x=[1 1.5 2 2.5 3];

y=[19.74 14.26 12.34 11.45 10.97];

and I know I can do a very rough approximation of a curve of best fit simply by "joining the dots" using: -

plot(x,y)

but is there a way to get MATLAB to join them using a curve of best fit?

I'm not sure exactly how to define 'curve of best fit', but I suppose an example might be if one had a string of x-values (+ & -) and each one had a corresponding y-value that was just x^2, then a curve of best fit for those points would show the get close to showing the curve y=x^2.

I obviously don't know the equation of my curve, which I guess is one of the issues that requires a certain method to be adopted over another.

Answer by Azzi Abdelmalek
on 6 Mar 2013

Edited by Azzi Abdelmalek
on 6 Mar 2013

Accepted Answer

x=[1 1.5 2 2.5 3];

y=[19.74 14.26 12.34 11.45 10.97];

xi=1:0.2:3

method='spline'

yi=interp1(x,y,xi,method)

plot(xi,yi)

Tom
on 6 Mar 2013

Many thanks for this - I think I'll end up using this solution.

Azzi Abdelmalek
on 6 Mar 2013

Sign in to comment.

Answer by Daniel Shub
on 6 Mar 2013

The title of your question says line, bu the body of the question says curve. If you really interested in a straight line, then lsline will do the job.

x=[1 1.5 2 2.5 3];

y=[19.74 14.26 12.34 11.45 10.97];

plot(x,y, '*')

lsline

The source of lsline is available:

type lsline

and you can see it does all the work with polyfit, so it should be possible to create a enhanced version that fits higher order polynomials or your own custom curve.

Tom
on 6 Mar 2013

Thanks Daniel. Sorry yes I did want a curve of best fit. Do you know how to do that?

Daniel Shub
on 6 Mar 2013

Please edit the question to explain how you are defining the curve of best fit.

Tom
on 6 Mar 2013

Okay - done that.

Sign in to comment.

Answer by Shashank Rayaprolu
on 21 Oct 2017

I took the points and formed a curve using spline function (using spline method and interpl command). But now I want to get the equation of the curve generated.

How should I go about that???

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Jan (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/66091-curve-of-best-fit-from-a-few-points#comment_134523

## Tom (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/66091-curve-of-best-fit-from-a-few-points#comment_134529

## amberly hadden (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/66091-curve-of-best-fit-from-a-few-points#comment_220453

Sign in to comment.