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'm trying to get a nonlinear fit of a vector in Matlab with no success.

Let's assume that I have a vector called data:

data = [1,30,250,55,22,76]

which can be seen as a function like


and so on.

This is how it looks on WolframAlpha

I want to make a nonlinear regression of this function/array with Matlab but the documentation is not helping me as it says something about DataSet or Matrix and I don't know how to get there starting from my simple array.

So the question is:

how can I make a nonlinear regression, in Matlab, starting from a given vector that can be seen as a function?

EDIT: As an extra, is it possible to have an array containing all the residuals in this form?

residuals[0]=residual of data[0]

EDIT 2: The output should be a function like this WolframAlpha result

share|cite|improve this question
up vote 0 down vote accepted

Create x data values

 x_data=[1 2 3 4 5 6]';

Create y data values

y_data=[1 30 250 55 22 76]';

Create custom fit function

g = fittype( @(a, b, c, d, e, x) a*x.^4+b*x.^3+c*x.^2+d*x+e );

Fit the data

[cfun,gof,output] = fit( x_data, y_data, g );

Display the coefficients


Display the residuals

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.