\(\renewcommand\AA{\unicode{x212B}}\)

Exercise 6

The aim of this exercise is to implement a function to fit the output data from Exercise 4. For simplicity a solution file, 11001_deltaE.nxs, is provided with the training data.

The peak can be fairly well approximated using a Lorentz function:

\[\frac{A}{\pi}(\frac{\frac{\Gamma}{2}}{(x-c)^2 + (\frac{\Gamma}{2})^2})\]

where A is the amplitude, \Gamma is the full width at half maximum and c is the peak centre. We will first define this as a simple 1D function and then improve it to use the peak function capabilities.

Simple 1D

  • Define a new 1D function called Lorentz.
  • It should have 3 parameters corresponding to the parameters described above.
  • Write the function1D method that evaluates the required values from the input x data using the definition as above. (Hint: you can use the python math module for pi).

Test this implementation:

  1. Load the data file.
  2. Plot the spectrum.
  3. Use the fit browser (using the fit function tool button (looks like a peak with a vertical red line on top)).
  4. Right click on plot and select “Add other function…”.
  • You’ll want to see how the fit progresses so set the log level in the Messages Box to information by right clicking in the window and selecting Log Level->Information. This will display additional information as the fit proceeds.
  • You may need to adjust the parameter initial values in the Fit Function window

Analytical Derivative

Extend the above 1D function and add an analytical derivative by adding a functionDeriv1D method. The derivatives w.r.t to each of the parameters are as follows:

\[ \begin{align}\begin{aligned}A \longrightarrow \frac{2}{\pi}\frac{\Gamma}{\Gamma^2 + 4(x - c)^2}\\c \longrightarrow \frac{A}{\pi}\frac{\Gamma(x - c)}{[\{\frac{\Gamma}{2}\}^2 + (x - c)^2]^2}\\\Gamma \longrightarrow - \frac{2A}{\pi}\frac{\Gamma^2 - 4(x - c)^2}{[\Gamma^2 + 4(x - c)^2]^2}\end{aligned}\end{align} \]

Re-run the fit using the above steps.

Peak Function

  • Make a copy of the Lorentz function and rename it to LorentzPeak.
  • Make this class an IPeakFunction instead of IFunction1D and change the methods from function1D to functionLocal and functionDeriv1D to functionDerivLocal.
  • Add the required methods for Mantid to interact with this as a peak function.
  • Retest using the steps above with the exception that when you right click on the plot choose the “Add peak…” menu rather than “Add other function…”.
  • You should now have interactivity in the GUI where you can set the initial values using the tools and get a quicker fit.

Once finished check your answer with the provided Exercise 6 Solutions