[par,err,gof,stat] = matmin2(gof_func,inpar,freepar,func,y,...) Recode to use Matlab fmincon and fminunc functions CURRENTLY DOESN'T PROVIDE PARAMETER ERRORS gof_func is the goodness-of-fit rule to use - chisq, logl etc. inpar are the initial parameter values to start minimization from. freepar is a logical array indicating if the corresponding parameter par should be varied in the fit. If freepar=[] then all par are taken to be free. func is the name of an N parameter user supplied function to fit which must have the form [y]=func(par,x). y,... are the data required to calc gof. For logl minimum would be n,x - for chisq min would be y,e,x - but y,e,x1,x2 with [y]=func(par,x1,x2) etc is needed for some problems. par are the output parameter values and err the errors on those parameters. gof is the goodness-of-fit at values par stat is the status of the fit eg: [x,n]=hfill(randn(1,1000),100,-3,3) [p,pe]=matmin('chisq',[30,0,1],[],'gauss',n,sqrt(n),x) [p,pe]=matmin('logl',[30,0,1],[],'gauss',n,x)
0001 function [par,err,gof,stat] = matmin2(gof_func,inpar,freepar,func,y,varargin) 0002 % [par,err,gof,stat] = matmin2(gof_func,inpar,freepar,func,y,...) 0003 % 0004 % Recode to use Matlab fmincon and fminunc functions 0005 % CURRENTLY DOESN'T PROVIDE PARAMETER ERRORS 0006 % 0007 % gof_func is the goodness-of-fit rule to use - chisq, logl etc. 0008 % inpar are the initial parameter values to start minimization from. 0009 % freepar is a logical array indicating if the corresponding parameter 0010 % par should be varied in the fit. If freepar=[] then all par are 0011 % taken to be free. 0012 % func is the name of an N parameter user supplied 0013 % function to fit which must have the form [y]=func(par,x). 0014 % y,... are the data required to calc gof. For logl 0015 % minimum would be n,x - for chisq min would be y,e,x - 0016 % but y,e,x1,x2 with [y]=func(par,x1,x2) etc is needed 0017 % for some problems. 0018 % 0019 % par are the output parameter values and err the errors 0020 % on those parameters. 0021 % gof is the goodness-of-fit at values par 0022 % stat is the status of the fit 0023 % 0024 % eg: [x,n]=hfill(randn(1,1000),100,-3,3) 0025 % [p,pe]=matmin('chisq',[30,0,1],[],'gauss',n,sqrt(n),x) 0026 % [p,pe]=matmin('logl',[30,0,1],[],'gauss',n,x) 0027 0028 if(isempty(freepar)) 0029 freepar=ones(size(inpar)); 0030 end 0031 0032 if(size(inpar)~=size(freepar)) 0033 error('freepar must be same size as inpar (or empty)') 0034 end 0035 0036 opt=optimset('Display','off'); 0037 0038 if(sum(freepar)==length(freepar)) 0039 [par,gof,stat] = fminunc(gof_func,inpar,opt,func,y,varargin{:}); 0040 else 0041 lo=-inf*ones(size(inpar)); 0042 up=+inf*ones(size(inpar)); 0043 ind=freepar==0; 0044 lo(ind)=inpar(ind); 0045 up(ind)=inpar(ind); 0046 [par,gof,stat] = fmincon(gof_func,inpar,[],[],[],[],lo,up,[],opt,func,y,varargin{:}); 0047 end 0048 0049 err=zeros(size(par)); 0050 0051 return