MATLAB Answers


Simple x-y curve fitting from data set.

Asked by Tom
on 24 Feb 2013

I'm struggling to find a simple example of simple curve fitting in MATLAB.

I just want to do a best-fit curve for the following data:

 x = [1 1.5 2 2.5 3 10e10]
y  = [19.74 14.26 12.34 11.45 10.97 9.87];

Please could someone show me how to do it?


Wayne King
on 24 Feb 2013

do you really have an x vector where the final value jumps from 3 to 10^10?

on 24 Feb 2013

Well it's supposed to be infinity but yes.

1 Answer

Answer by the cyclist
on 24 Feb 2013
Edited by the cyclist
on 24 Feb 2013
 Accepted answer

If you type

doc polyfit

in the MATLAB command window, you will see a simple example (and of course the syntax of the command).


I didn't really pay attention to the, uh, unusual shape of your data. polyfit() does simple polynomial fitting, so it's not right for you.

There's not really going to be a fit for a point "at infinity". You should probably add some detail about what you are trying to really accomplish here.


on 24 Feb 2013

Thanks for the response, I'm not familiar with erf so that's why I don't find it so simple and also I can't see how to apply this to my problem.

The polyfit example starts off knowing the function - erf(x) - whereas I'm starting off not knowing the function and just with a set of data points. Plotting my data doesn't give a curve of best fit - just a line graph joining the data points, which isn't what I want. Sorry if I'm missing something obvious.

on 24 Feb 2013

By definition that line is the best fit , as it goes through every point? If you wan't more restrictions you need to decide on what form your fit must have, even if you don't know the "true" underlying function.

the cyclist
on 24 Feb 2013

Tom, that example starts off with erf() simply to create the sample of data which will be fit later.

As Jurgen says, you need to decide what form you want.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!