MATLAB: Non Linear Fit help

non linear fit

I have been trying to fit some data with a model for some time now and am getting fairly close. In origin pro the fit seems to work ok and I get reasonable parameters back albeit the residuals are a little funny. Unfortunately in Matlab the fit is not so good. Could I send some one my code my function and some data to take a look at?
Oh and also there are imaginary numbers!
clear all;
close all
close all hidden
A = uiimport;
clear xdata;
clear ydata;
xdata = A.data(:,1);
ydata = A.data(:,2);
Vg = xdata;
Id = abs(ydata);
x0 = [8.6E-10;1.7;0.8;5E6];
options = optimset('Display','iter',...
'TolFun',1E-100,'TolX',1E-100,'MaxIter',1000);
[beta,resid,J,COVB,mse] = nlinfit(Vg,Id,@RcFun,[x0],options);
[ci se] = nlparci(real(beta),resid,'covar',COVB);
Id_new = ((((real(beta(1))*((Vg-real(beta(2))).^(real(beta(3))+1))).^(-1))+real(beta(4))).^(-1));
plot(Vg,Id_new,'r',Vg,Id,'o');
hold on;
plot(Vg,resid,'+');
function F = Rcfun(a,Vg)
K = real(a(1));
V = real(a(2));
b = real(a(3));
R = real(a(4));
F = (((K.*(Vg-V).^(b+1)).^(-1))+R).^(-1);
end
Data
A B
0 3.03E-12
1 1.5E-13
2 1.58E-12
3 2.81E-12
4 2.55E-12
5 2.31E-12
6 4.13E-12
7 2.89E-12
8 4.99E-12
9 6.38E-12
10 1.068E-11
11 1.96E-11
12 5.343E-11
13 5.405E-11
14 5.347E-11
15 5.142E-11
16 2.4139E-10
17 7.4428E-10
18 1.5752E-9
19 2.7328E-9
20 4.3347E-9
21 6.5506E-9
22 9.5258E-9
23 1.31356E-8
24 1.72672E-8
25 2.17876E-8
26 2.66302E-8
27 3.18252E-8
28 3.7101E-8
29 4.23594E-8
30 4.78078E-8
31 5.32604E-8
32 5.86136E-8
33 6.39262E-8
34 6.93234E-8
35 7.47466E-8
36 8.01152E-8
37 8.54398E-8
38 9.08214E-8
39 9.62598E-8
40 1.0184E-7
41 1.074E-7
42 1.1322E-7
43 1.1876E-7
44 1.2432E-7
45 1.299E-7
46 1.3534E-7
47 1.4062E-7
48 1.4596E-7
49 1.5096E-7
50 1.558E-7
51 1.6118E-7
52 1.6616E-7
53 1.7064E-7
54 1.7546E-7
55 1.7946E-7
56 1.8402E-7
57 1.8776E-7
58 1.9138E-7
59 1.9584E-7
60 1.9992E-7

Best Answer

Thank you for clarifying ‘RcFun’. After working with your code for a bit, the problem definitely seems to be the ‘(Vg-V)’ term. The only way I am able to get a decent fit without complex parameters is to use ‘lsqcurvefit’ and constraining ‘V’ to not be greater than zero, but then ‘nlparci’ wouldn't calculate confidence intervals on the parameter estimates. [I was able to get a good fit with ‘nlinfit’ by redefining that term to ‘abs(Vg-V)’, but that changed your function.] With ‘lsqcurvefit’, your ‘options’ statement and structure remains the same, although I increased ‘MaxIter’ and ‘MaxFunEvals’ in mine for diagnostic purposes. I also made ‘RcFun’ a single-line anonymous function for convenience:
RcFun = @(a,Vg) 1./(1./(a(1).*(Vg-a(2)).^(a(3)+1)) + a(4));
Lobnd = [];
Upbnd = ones(4,1)*realmax;
Upbnd(2) = 0; % Constrain ‘V’ to not be greater than zero
[beta,resnrm,resid,xitflg,outpt,lmda,J] = lsqcurvefit(RcFun,x0,Vg,Id,Lobnd,Upbnd,options); % NOTE: ‘Answers’ wrapped this line
and got an acceptable fit with these parameters:
K = 35.6744e-012
V = -32.7518e-003
b = 1.1302e+000
R = 145.7789e-003
The norm of the residuals was 5.2916E-015. I'm not certain what you're doing or what data you're modeling, so I can't interpret these or their validity.
In spite of the decent fit, the residuals had a definite oscillating trend.