#
# example of using Ifeffit's minimize:
#  fit powder diffraction data
#
#  usage:
#   ifeffit>  load fit_xrd.iff
#   ifeffit>  fit_file xrd.dat

s2pi = sqrt(2*pi)

read_data(file = xrd.dat, group=f, label='q, dat')

  guess (bxn0   = 400, bxn1 = -139, bxn2    = 20)
  guess (g1_amp = 1.0, g1_sig = 0.022, g1_cen  = 1.28)
  guess (g2_amp = 0.8, g2_sig = 0.01, g2_cen  = 2.00)
  guess (g3_amp = 1.6, g3_sig = 0.01, g3_cen  = 2.10)
  guess (g4_amp = 3.0, g4_sig = 0.01, g4_cen  = 2.45)
  guess (g5_amp = 0.4, g5_sig = 0.01, g5_cen  = 2.57)
  guess (g6_amp = 1.6, g6_sig = 0.01, g6_cen  = 2.95)
  guess (g7_amp = 0.8, g7_sig = 0.01, g7_cen  = 3.25)

  guess (l4_amp = 0.02,  l4_wid = 0.01, l4_cen = 2.45)
  guess (l6_amp = 0.02,  l6_wid = 0.01, l6_cen = 2.95)

  f.bkg = bxn0 +  f.q * (bxn1 + f.q * bxn2)

  f.g1  = (abs(g1_amp/(g1_sig * s2pi))) * exp(-((f.q-g1_cen)/g1_sig)^2/2)
  f.g2  = (abs(g2_amp/(g2_sig * s2pi))) * exp(-((f.q-g2_cen)/g2_sig)^2/2)
  f.g3  = (abs(g3_amp/(g3_sig * s2pi))) * exp(-((f.q-g3_cen)/g3_sig)^2/2)
  f.g4  = (abs(g4_amp/(g4_sig * s2pi))) * exp(-((f.q-g4_cen)/g4_sig)^2/2)
  f.g5  = (abs(g5_amp/(g5_sig * s2pi))) * exp(-((f.q-g5_cen)/g5_sig)^2/2)
  f.g6  = (abs(g6_amp/(g6_sig * s2pi))) * exp(-((f.q-g6_cen)/g6_sig)^2/2)
  f.g7  = (abs(g7_amp/(g7_sig * s2pi))) * exp(-((f.q-g7_cen)/g7_sig)^2/2)

  f.l4  = abs(l4_amp) / ( (f.q-l4_cen)^2 + l4_wid^2)
  f.l6  = abs(l6_amp) / ( (f.q-l6_cen)^2 + l6_wid^2)

  f.fit = f.bkg + f.g1 + f.g2 +f.g3 + f.g4 + f.g5 + f.g6 + f.g7 + f.l4 + f.l6
  f.dif = f.dat - f.fit

  plot(f.q,f.dat, new)
  plot(f.q,f.fit, color=black)
  minimize(f.dif, ordinate = f.q,xmax=3.5, xmin=1)
  plot(f.q,f.fit, color=red)
  show @variables


