Yotam
Yotam

Reputation: 10685

Curve fitting with Matlab fails miserably

I'm trying to fit a curve in Matlab using fit in command line. The input data is:

X =

     1
     2
     4
     5
     8
     9
    10
    13

Y =

1.0e-04 *

    0.1994
    0.0733
    0.0255
    0.0169
    0.0077
    0.0051
    0.0042
    0.0027

And the target function is

Y = 1/(kappa*X.^a)

I am using fittype, fitoptions, and fit as follow:

model1 = fittype('1/(kappa*x.^pow)');
opt1 = fitoptions(model1);
opt1.StartPoint = [1e-5 -2];
[fit1,gof1] = fit(X,Y.^-1,model1,opt1)

I get results with rsquare of roughly -450 which are vaguely in the same direction as the measurement.I have attached a figure to demonstrate this. How can I improve Matlab fitting skills?

Edit:

I removed the .^-1 in the fit command. This improved the behavior but it is not entirely correct. If I set model1 to be:

model1 = fittype('1/(kappa*x.^pow)');

The fit is bad. If I set it to be:

model1 = fittype('kappa*x.^pow');

The fit is good (with kappa being a very small number and pow being negative).

I have also normalized Y and I get a reasonable results

Upvotes: 3

Views: 3694

Answers (1)

anon
anon

Reputation:

You should replace

[fit1,gof1] = fit(X,Y.^-1,model1,opt1)

by

[fit1,gof1] = fit(X,Y,model1,opt1)

Also your initial condition for kappa is 1e-5, which would make sense if kappa was in the numerator.

Using the model kappa*x.^pow, with the initial condition [1e-5 -2], you would get the right fit:

X =[1     2     4     5     8     9    10    13]';
Y = 1.0e-04 * [0.1994 0.0733 0.0255 0.0169 0.0077 0.0051 0.0042  0.0027]';

model1 = fittype('kappa*x.^pow');
opt1 = fitoptions(model1);
opt1.StartPoint = [1e-5 -2];
[fit1,gof1] = fit(X,Y,model1,opt1)
plot(fit1, X, Y)

The fitted result is

>> fit1
fit1 = 
   General model:
   fit1(x) = kappa*x.^pow
   Coefficients (with 95% confidence bounds):
     kappa =   2.044e-05  (1.931e-05, 2.158e-05)
     pow =      -1.657  (-1.851, -1.464)

Fitted Curve

Upvotes: 6

Related Questions