Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was wondering if it is possible to create a function f(x) in matlab based on y values.

Example X=[0,1,2,3,4,5,6,7,8,9] Y=[5,6,7,8,9,10,11,12,13,14]

In this trivial example I wish to get f(x)=x+5

If my memory serves me well, I remember that maple provide such capability. Wonder if this can be done in matlab?


share|cite|improve this question
You'll want to look into polyfit() and polyval(). – J. M. Oct 6 '11 at 14:17
(If you're just trying to find a fit for some data, not necessarily in Matlab, try ) – endolith Oct 7 '11 at 14:19
up vote 2 down vote accepted

polyfit(X,Y,n) will give you the coefficients of n'th degree polynomial that fits best to your data (in SSE sense). For your example:

 >> X=[0,1,2,3,4,5,6,7,8,9];
 >> Y=[5,6,7,8,9,10,11,12,13,14];
 >> polyfit(X,Y,1)

ans =

    1.0000    5.0000
share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.