I am using matlab to minimize a sum of squares (chi-squared) function. My model has a definite integral (from zero to data values).The model has three parameters w.r.t which I need to minimize.
I need to integrate (1+x).^(b-a-2).* exp(-b.*x)
from zero to z(i)
, where a
and b
are parameters.
I make a function handle
modelfun1=@ (y,a,b,c) (978.4./c).int( (1+x).^(b-a-2). exp(-b.*x),0,y)
and then a sum of squares function as
sum1=@(a,b,c,data) sum(((data.ydata-modelfun1(data.xdata,a,b,c)).^2)./data.zdata.^2);
where data.xdata
has all the z
values, data.ydata
are the observed values and data.zdata
are the variances.
when I minimize this function using fminsearch
[tmin,ssmin]=fminsearch(sum1,[-0.1;0.06;70],[],data)
I get the following error
Error using
@(a,b,c,data)sum(((data.ydata-modelfun1(data.xdata,a,b,c)).^2)./data.zdata.^2)
Not enough input arguments.
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
can someone please point out what I am doing wrong. I have tried many more things but this seems to be the most recurring error.
Here is another attempt at the problem
I am trying to minimize a chi-square function (that involves a definite integral in the model) using nlinfit. This is my attempt: I make the model function as follows:
function [ f ] = modelf( p,ul )
syms x
a=p(1);
b=p(2);
c=p(3);
f=(978.4./c).*int((1+x)^(b-a-2)*exp(-b*x),x,0,ul);
end
here 'ul' is the upper limit of the integral.It is substituted from a data matrix z (32X1). The dependent data is in matrix y(32X1).I make another data set having weighted y values as
w = 1/variance;
where variance are the individual errors on data points yw = sqrt(w).*y;
and I also make a weighted function handle as
modelfunw=@(p,z) (sqrt(var))'.*modelf(p,z);
then I call nlinfit as
p0=[-0.1 0.05 70]'; %the initial guess
beta=nlinfit(z,yw,modelfunw,p0)
but I get the following error message
Error using nlinfit (line 120) Error evaluating model function '@(p,z)(sqrt(var))'.*modelf(p,z)'.
Caused by: Error using mupadmex Error in MuPAD command: Illegal argument [checkNumber]
Can someone please point out what I am doing wrong? Or is there a better way to minimize chi-square?