dustem_chi2.pro 504 Bytes
FUNCTION DUSTEM_CHI2, y, model, npar, err=err
; returns the chi-square value of fit MODEL to data Y
;
; NPAR (I): nr of parameters in the fit
; ERR  (I): error of each data point. Default is ERR=0.1*Y

  ny = n_elements(y)
  if n_elements(err) EQ 0 OR TOTAL(err) EQ 0 then begin
     err = 0.1*y
     print,'(W) CHI2: error missing, set to 10 %'
  endif
  ndof = ny - npar              ; nr of degrees of freedom
  chi = TOTAL( ((y-model)/err)^2 )
  if ndof GT 0 then chi = chi / ndof

  return, chi
END