[par,err,gof,stat] = matmin(gof_func,inpar,freepar,func,y,...) Use Minuit to fit an arbitrary function and return fitted parameters WITH THEIR 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 MATLAB 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 opt 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 the par values given (chi square or -2 times the log joint probability). stat is the status of the fit (see MINUIT manual - 3 is AOK). 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] = matmin(gof_func,inpar,freepar,func,y,varargin) 0002 % [par,err,gof,stat] = matmin(gof_func,inpar,freepar,func,y,...) 0003 % 0004 % Use Minuit to fit an arbitrary function and return 0005 % fitted parameters WITH THEIR 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 MATLAB 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 opt 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 the par values given (chi 0022 % square or -2 times the log joint probability). 0023 % stat is the status of the fit (see MINUIT manual - 3 is AOK). 0024 % 0025 % eg: [x,n]=hfill(randn(1,1000),100,-3,3); 0026 % [p,pe]=matmin('chisq',[30,0,1],[],'gauss',n,sqrt(n),x) 0027 % [p,pe]=matmin('logl',[30,0,1],[],'gauss',n,x) 0028 0029 if(isempty(freepar)) 0030 freepar=ones(size(inpar)); 0031 end 0032 0033 if(size(inpar)~=size(freepar)) 0034 error('freepar must be same size as inpar (or empty)') 0035 end 0036 0037 [par,err,gof,stat] = matminc(gof_func,inpar,freepar,func,y,varargin{:}); 0038 0039 return