code: def measure(function, xs, ys, popt, weights):
      
      m = 0
      n = 0
      for x in xs:
          try:
              if len(popt) == 2:
                  m += (ys[n] - function(x, popt[0], popt[1]))**2 * weights[n]
              elif len(popt) == 3:
                  m += (ys[n] - function(x, popt[0], popt[1], popt[2]))**2 * weights[n]
              else:
                  raise NotImplementedError
              n += 1
          except IndexError:
              raise RuntimeError('y does not exist for x = ', x, ' this should not happen')
  
      return m
docs: measure the quality of a fit