======= Fitting ======= Starting a fit -------------- Mark the curve you want to fit. To do this, double click the curve on the plot or in the legend, or select it and choose :menuselection:`Edit --> Mark` or press :kbd:`M`. .. image:: images/fit-selectcurves.png :scale: 80 To control which points are used in the fit, use the Range tool and/or masks. Only the visible points on the curve are fit. You can change the dataset or the range of points being fit at any time. Open the fit window .. image:: images/fit-toolbar.png :scale: 80 Adding fit functions -------------------- Click :menuselection:`Fns` to open the Functions dialog, and add one or more terms to your fit. .. image:: images/fitfunctions.png :scale: 80 To remove a term from the fit, press :kbd:`Del` or right click the term's name and select :menuselection:`Delete` from the context menu. To disable/enable a term without deleting it you can collapse/expand the term in the list by clicking on the triangle next to the term name or double clicking the term name. Fit settings ------------ Click :menuselection:`Settings` to open the Fit Settings dialog, and add one or more terms to your fit. **Method** * *Least Squares:* the usual Levenberg-Marquardt least squares method. This minimizes the sum of squares of the ``y`` distance between the data and the fit curve (uses a `Python version of `MPFIT `_). * *Orthogonal distance regression (ODR):* This minimizes the orthogonal distance between the data and the fit curve. ODR is useful if you have data with uncertainty in both the ``x`` and ``y`` values (uses `ODRPACK95 `_). **Weights** * *No Weighting:* Weights for all points :math:`w_i=1` * *Statistical:* Weights are :math:`w_i = 1/y_i` * *Logarithmic:* Log scale fitting, i.e. the distance between the fit curve and data points on a log y scale is minimized * *Instrumental:* Weights are :math:`w_i = 1/\sigma_i^2` where :math:`\sigma` is the error bar of the dataset **Termination Conditions:** The fit will stop for either of the following reasons * the number of iterations exceed the number specified (default is 100) * the relative difference fit parameters in two successive iterations is less than the parameter tolerance * the relative difference in :math:`\chi^2` for two successive iterations is less than the :math:`\chi^2`-tolerance **Fit Results** You can control where to save the fit results when the fit is finished: give a worksheet name for the fit parameters and a notebook name for the fit report. Errors in the fit parameters can optionally also be saved in the worksheet. In the fit results worksheet, a column is created for each fit parameter. Each time you choose to save fit results, a new line is added with the current values of the fit parameters. You can set additional data to be saved in the worksheet by putting a list of comma-separated names in the `Extra Columns` box (for example "temp, pres"). **Fit Curves** Controls how the fit curves are drawn in the plot. The X values can be either uniform (linear or logarithmic, depending on the plot scale) or the same as the X values of the data being fit. Setting initial values ---------------------- You can set the values of the fit parameters by editing the `Value` column in the fit window. For certain fit functions, parameter values can also be changed by dragging the fit curve with the mouse. Choosing `Reset` from the term's context menu will revert all parameters to their default value. Running the fit --------------- Click :menuselection:`Fit` to run the fit. The bottom of the fit window will show the progress of the fit. Saving fit results ------------------ Parameters ~~~~~~~~~~ Click :menuselection:`Params` to save the current parameters to a worksheet (the worksheet name is specified in the settings). Curves ~~~~~~ Click :menuselection:`Curves` to add the fit curves to the plot. The data is stored in a worksheet called ``_curves``. To add only a single term to the graph, choose :menuselection:`Add term to graph` from the term's context menu. Fit report ~~~~~~~~~~ Choose :menuselection:`Fit --> Create report` to save a text summary of the fit to a notebook window. Parameter fixing and limits ---------------------------- You can fix the value of a parameter by right clicking the parameter name and choosing :menuselection:`Lock` from the context menu. Choosing :menuselection:`Limits` lets you set minimum and maximum values; :menuselection:`Reset Limits` will restore the limits to their default values. Fitting multiple datasets ------------------------- It's also possible to fit multiple datasets simultaneously. If you mark multiple datasets, they will appear in a list near the bottom of the fit window. By default all parameters are shared among all datasets. When you select :menuselection:`Split` from a parameter's context menu, its name turns blue indicating that the parameter is no longer shared and can take a different value for each dataset. The parameters shown in the fit window, and the curves on the graph, correspond to the dataset which is currently selected in the list at the bottom of the fit window.