Search code examples
matlabintegralbest-fit-curve

Matlab how define an integral function and find best coefficients


I would like to find coefficients to best fit the nonlinear functions, and the nonlinear functions was an integral function. So the first step was to define an function:

function dT = km(x,sT)

fun=@(temp)((-x(1)*x(2)*(temp).^(x(2)-1.0))./ ...  
                ((x(3)^x(2))*((1+(temp./x(3)).^x(2)).^2)));
dT=integral(fun,0,sT);
% x is a array containing three coefficients;
% sT is a array containing integral upper limits;
$ dT is integral values;

And then the lsqcurvefit function was called to find best coefficients:

 x0=[0.0, 0.0, 0.0]; % initial coefficients;
 x1=[300.0 -10.0 0.0]; % lower limit;
 x2=[500.0 0.0 1000.0]; % upper limit;

stimeT=[1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0]; %  XDATA
dmT=[3.0 6.0 9.0 12.0 15.0 18.0 21.0 24.0]; % YDATA

    [x,resnorm]=lsqcurvefit(@km,x0,stimeT,dmT,x1,x2);

But I got these errors:

Error using integral (line 86)
A and B must be floating point scalars.
Error in km (line 6)
dT=integral(fun,0.0,sT);
Error in lsqcurvefit (line 195)
            initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in individual_kernel (line 57)
 [x,resnorm]=lsqcurvefit(@km,x0,stimeT,dmT,x1,x2);
Caused by:
    Failure in initial user-supplied objective function evaluation. LSQCURVEFIT
    cannot continue. 

MATLAB's default data type is double, and double is a type of floating, so I do not know how to modify the code. Any advice will be appreciated!


Solution

  • The integral function requires its second and third arguments to be scalars. If you want to build the array dT of integrals over the limits contained in the array sT (or silkingT—your reported error is not consistent with the provided code), use in the definition of function km:

    dT = arrayfun(@(T) integral(fun,0,T), sT);