MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

# Non linear fit with four parameters help.

Asked by Adam Parry on 5 Jul 2012

Hi

I am looking to fit some data with a model that has four parameters. I am presently using nlinfit, and have two problems. One is that my model returns imaginary numbers, which realistically I don't want. So is there any way to surpress them. Two is that as far as I can tell the fit is not very good. My fourth parameter doesn't change from the initial value and although I haven't looked into it on matlab, i think the residuals are not good. If any one could help that would be much appreciated. Here is some code

``` clear all;
close all;
close all hidden;```
` A = uiimport;`
```     clear xdata;
clear ydata;
xdata = A.data(:,1);
ydata = A.data(:,6);
Vdata = A.data(:,5);```
```     Vg = -xdata;
Gd = ydata./Vdata;```
`     x0 = [1E-10;1;1;1E6];`
```     options = optimset('Display','iter',...
'TolFun',1E-100,'TolX',1E-30,'MaxIter',1000);```
```    [beta,resid,J,COVB,mse] = nlinfit(Vg,Gd,@myfun,[x0],options) ;
%   [ci se] = nlparci(beta,resid,'covar',COVB);```
```    Gd_new = (((beta(1)*(Vg-beta(2)).^(beta(3)+1)).^(-1))+beta(4)).^(-1);3
plot(Vg,Gd_new,'r',Vg,Gd,'o');```

my function is

` function F = myfun(a,Vg)`
` F = ((((a(1).*((Vg-a(2)).^(a(3)+1))).^(-1))+a(4)).^(-1));`
` end`

I'll add some data on in just a sec

## Products

No products are associated with this question.

Answer by Adam Parry on 5 Jul 2012

Vg Ig Vs Is Vd Id

20 3.76E-10 0 -1.54E-10 -0.5 -5.27E-11

19.2 1.94E-10 0 -7.75E-11 -0.5 -3.01E-11

18.4 1.38E-10 0 -6.02E-11 -0.5 -2.18E-11

17.6 1.04E-10 0 -4.81E-11 -0.5 -1.62E-11

16.8 7.92E-11 0 -3.90E-11 -0.5 -1.46E-11

16 6.38E-11 0 -3.51E-11 -0.5 -1.21E-11

15.2 5.05E-11 0 -3.11E-11 -0.5 -9.92E-12

14.4 3.70E-11 0 -2.30E-11 -0.5 -7.64E-12

13.6 2.80E-11 0 -2.05E-11 -0.5 -7.42E-12

12.8 2.57E-11 0 -1.54E-11 -0.5 -3.41E-12

12 1.52E-11 0 -1.28E-11 -0.5 -3.25E-12

11.2 5.05E-12 0 -7.80E-12 -0.5 -2.42E-12

10.4 5.93E-12 0 -7.68E-12 -0.5 -1.99E-12

9.6 1.50E-12 0 -7.00E-12 -0.5 -1.97E-12

8.8 -6.94E-12 0 -3.68E-12 -0.5 -1.14E-12

8 -7.60E-12 0 -4.99E-12 -0.5 -7.20E-13

7.2 -1.23E-11 0 -2.49E-12 -0.5 -1.34E-12

6.4 -1.35E-11 0 -2.45E-12 -0.5 -2.90E-13

5.6 -1.55E-11 0 -1.86E-12 -0.5 -2.10E-13

4.8 -1.75E-11 0 2.90E-13 -0.5 -7.80E-13

4 -2.41E-11 0 1.64E-12 -0.5 3.30E-13

3.2 -2.19E-11 0 2.63E-12 -0.5 3.40E-13

2.4 -2.51E-11 0 6.60E-13 -0.5 4.81E-12

1.6 -3.24E-11 0 7.24E-12 -0.5 2.26E-12

0.8 -3.67E-11 0 7.82E-12 -0.5 5.37E-12

0 -4.93E-11 0 1.52E-11 -0.5 1.08E-11

-0.8 -5.78E-11 0 2.80E-11 -0.5 1.38E-11

-1.6 -8.40E-11 0 4.99E-11 -0.5 1.52E-11

-2.4 -1.63E-10 0 1.12E-10 -0.5 2.81E-11

-3.2 -2.83E-10 0 2.46E-10 -0.5 -1.47E-11

-4 -5.10E-10 0 6.24E-10 -0.5 -1.32E-10

-4.8 -1.02E-09 0 1.47E-09 -0.5 -5.33E-10

-5.6 -1.29E-09 0 2.82E-09 -0.5 -1.40E-09

-6.4 -2.96E-10 0 3.45E-09 -0.5 -2.75E-09

-7.2 -1.84E-10 0 4.80E-09 -0.5 -4.40E-09

-8 -2.15E-10 0 6.86E-09 -0.5 -6.43E-09

-8.8 -1.16E-10 0 9.14E-09 -0.5 -8.85E-09

-9.6 -1.63E-10 0 1.24E-08 -0.5 -1.17E-08

-10.4 -1.87E-10 0 1.52E-08 -0.5 -1.49E-08

-11.2 -2.20E-10 0 1.87E-08 -0.5 -1.83E-08

-12 -2.59E-10 0 2.25E-08 -0.5 -2.21E-08

-12.8 -3.14E-10 0 2.66E-08 -0.5 -2.62E-08

-13.6 -3.50E-10 0 3.12E-08 -0.5 -3.07E-08

-14.4 -3.98E-10 0 3.61E-08 -0.5 -3.56E-08

-15.2 -4.42E-10 0 4.17E-08 -0.5 -4.11E-08

-16 -4.81E-10 0 4.78E-08 -0.5 -4.71E-08

-16.8 -5.64E-10 0 5.45E-08 -0.5 -5.36E-08

-17.6 -5.99E-10 0 6.15E-08 -0.5 -6.06E-08

-18.4 -6.66E-10 0 6.90E-08 -0.5 -6.79E-08

-19.2 -7.33E-10 0 7.65E-08 -0.5 -7.54E-08

-20 -7.92E-10 0 8.43E-08 -0.5 -8.32E-08

-20.8 -8.61E-10 0 9.21E-08 -0.5 -9.08E-08

-21.6 -9.00E-10 0 1.00E-07 -0.5 -9.86E-08

-22.4 -9.56E-10 0 1.08E-07 -0.5 -1.07E-07

-23.2 -1.02E-09 0 1.17E-07 -0.5 -1.15E-07

-24 -1.09E-09 0 1.25E-07 -0.5 -1.24E-07

-24.8 -1.15E-09 0 1.34E-07 -0.5 -1.33E-07

-25.6 -1.22E-09 0 1.43E-07 -0.5 -1.41E-07

-26.4 -1.27E-09 0 1.52E-07 -0.5 -1.50E-07

-27.2 -1.35E-09 0 1.61E-07 -0.5 -1.59E-07

-28 -1.40E-09 0 1.70E-07 -0.5 -1.67E-07

-28.8 -1.45E-09 0 1.78E-07 -0.5 -1.76E-07

-29.6 -1.52E-09 0 1.87E-07 -0.5 -1.85E-07

-30.4 -1.56E-09 0 1.96E-07 -0.5 -1.94E-07

-31.2 -1.61E-09 0 2.05E-07 -0.5 -2.02E-07

-32 -1.67E-09 0 2.14E-07 -0.5 -2.11E-07

-32.8 -1.73E-09 0 2.22E-07 -0.5 -2.20E-07

-33.6 -1.80E-09 0 2.31E-07 -0.5 -2.28E-07

-34.4 -1.86E-09 0 2.39E-07 -0.5 -2.36E-07

-35.2 -1.93E-09 0 2.48E-07 -0.5 -2.45E-07

-36 -1.99E-09 0 2.56E-07 -0.5 -2.53E-07

-36.8 -2.06E-09 0 2.64E-07 -0.5 -2.62E-07

-37.6 -2.13E-09 0 2.73E-07 -0.5 -2.70E-07

-38.4 -2.20E-09 0 2.81E-07 -0.5 -2.78E-07

-39.2 -2.26E-09 0 2.89E-07 -0.5 -2.85E-07

-40 -2.35E-09 0 2.97E-07 -0.5 -2.93E-07

-40.8 -2.43E-09 0 3.05E-07 -0.5 -3.01E-07

-41.6 -2.58E-09 0 3.13E-07 -0.5 -3.09E-07

-42.4 -2.72E-09 0 3.21E-07 -0.5 -3.17E-07

-43.2 -2.72E-09 0 3.29E-07 -0.5 -3.26E-07

-44 -2.81E-09 0 3.37E-07 -0.5 -3.32E-07

-44.8 -2.85E-09 0 3.44E-07 -0.5 -3.41E-07

-45.6 -2.93E-09 0 3.52E-07 -0.5 -3.47E-07

-46.4 -2.95E-09 0 3.60E-07 -0.5 -3.55E-07

-47.2 -3.04E-09 0 3.67E-07 -0.5 -3.62E-07

-48 -3.13E-09 0 3.74E-07 -0.5 -3.69E-07

-48.8 -3.19E-09 0 3.81E-07 -0.5 -3.76E-07

-49.6 -3.26E-09 0 3.88E-07 -0.5 -3.83E-07

-50.4 -3.32E-09 0 3.94E-07 -0.5 -3.89E-07

-51.2 -3.42E-09 0 4.03E-07 -0.5 -3.96E-07

-52 -3.48E-09 0 4.09E-07 -0.5 -4.03E-07

-52.8 -3.54E-09 0 4.15E-07 -0.5 -4.10E-07

-53.6 -3.64E-09 0 4.22E-07 -0.5 -4.16E-07

-54.4 -3.75E-09 0 4.28E-07 -0.5 -4.22E-07

-55.2 -6.32E-09 0 4.34E-07 -0.5 -4.28E-07

-56 -3.94E-09 0 4.41E-07 -0.5 -4.34E-07

-56.8 -4.05E-09 0 4.46E-07 -0.5 -4.40E-07

-57.6 -4.16E-09 0 4.53E-07 -0.5 -4.47E-07

-58.4 -4.26E-09 0 4.58E-07 -0.5 -4.52E-07

-59.2 -4.35E-09 0 4.64E-07 -0.5 -4.58E-07

-60 -4.60E-09 0 4.71E-07 -0.5 -4.64E-07