Ander Biguri
Ander Biguri

Reputation: 35525

MATLAB fitting of data to a inverse quadratic equation

I have a bunch of data, and I want a fitting with a function that I want, for example, 1/(ax^2+bx+c). My objective is to get a,b,c values.

Is there any function of MATLAB that helps with this? I have been checking the fit() function, but I didn't reach a conclusion. Which is the best way?

Upvotes: 1

Views: 16127

Answers (3)

Mohsen
Mohsen

Reputation: 1089

I don't know whether this post is useful after 3 months or not. i think cftool may help you check it

easily you can add data and select fitting method ....

Upvotes: 1

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38052

The model you give can be solved using simple methods:

% model function
f = @(a,b,c,x) 1./(a*x.^2+b*x+c);

% noise function 
noise = @(z) 0.005*randn(size(z));

% parameters to find
a = +3;
b = +4;
c = -8;

% exmample data
x = -2:0.01:2;    x = x + noise(x);
y = f(a,b,c, x);  y = y + noise(y);


% create linear system Ax = b, with 
% A = [x²  x  1]
% x = [a; b; c]
% b = 1/y;
A = bsxfun(@power, x.', 2:-1:0);

A\(1./y.')

Result:

ans = 
 3.035753123094593e+00  % (a)
 4.029749103502019e+00  % (b)
-8.038644874704120e+00  % (c)

This is possible because the model you give is a linear one, in which case the backslash operator will give the solution (the 1./y is a bit dangerous though...)

When fitting non-linear models, take a look at lsqcurvefit (optimization toolbox), or you can write your own implementation using fmincon (optimization toolbox), fminsearch or fminunc.

Also, if you happen to have the curve fitting toolbox, type help curvefit and start there.

Upvotes: 5

FakeDIY
FakeDIY

Reputation: 1445

To me this sounds like a least squares problem.

I think lsqcurvefit might be a good place to start:

http://www.mathworks.co.uk/help/optim/ug/lsqcurvefit.html

Upvotes: 1

Related Questions