Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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

f(0)=data[0]
f(1)=data[1]

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|improve this question

1 Answer 1

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

cfun

Display the residuals

output.residuals
share|improve this answer

Your Answer

 
discard

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.