# Overview
Data is fitted using Python with scipy.ODR, which wraps the [ODRPACK](http://docs.scipy.org/doc/external/odrpack_guide.pdf) module written by Paul Boggs, Richard Byrd, Janet Rogers and Robert Schnabel in the early nineties. A Python [template](/template) is linked on the main page so you can try the algorithm in Python yourself.
### Fitting Details
The Python used here is based on a script from University of Toronto [here](http://www.physics.utoronto.ca/~phy326/python/odr_fit_to_data.py). \
\
Regular nonlinear regression is used when only y errors are specified. For errors in both variables, ODR is used. For linear regression with no uncertainties, R^2 is reported. For nonlinear regression without uncertainties, an average residual is reported. \
\
Uncertainties in fit parameters are not scaled by the square root of the chi squared, as some programs do to compensate for large or small chi values. The chi squared value, when appropriate, is reported and proportionally colored red if it should be investigated. \
\
For ODR, residuals represent the scaling factor that enlarges / shrinks the ellipse created by the given dx and dy such that the ellipse is tangent to the fit line. This scaling factor is what is minimized by the Levenberg-Marquardt algorithm. The uncertainties for these residuals is simply unity. \
\
Confidence bands are calculated using the covariance matrix and the Jacobian calculated from the fit model (numerically calculated for custom equations). Bands shown are one standard deviation. Confidence bands are not calculated for a damped oscillator fit, and are attempted for custom equations (and works in most cases).
### Sessions
This site is based on sessions that hold your data and the info used to fit and plot it. The session id is listed at the bottom of the page. If you need to return to that session or use it on another computer, simply add the session ID to the end of the URL. For instance, to access the session with id `a08sU3fl`, go to: `https://curve.fit/a08sU3fl`. Data is only stored in a session when you press the "Fit!" button. You can also share the PDF link by copying the URL of the PDF, which will be `https://curve.fit/a08sU3fl/pdf` once you have generated a PDF. The link may have a datetime appended to it, which refers to a specific fit run for that session such as `https://curve.fit/a08sU3fl/pdf/20150405123423`.
### Session Control
![Session Buttons](static/img/sessbuttons.png "Session Buttons")
#### Fork
You can Fork the session you're currently using if you want to keep that session, but copy the data into a new session and continue fitting.
#### Reset
Reset the session if you want to start fresh or if you want to retain what you have just fit for later. A new session id will appear and you can still access the old one if you need to using the above method.
#### Previous Versions
By clicking on the Previous button, a popup will appear that lists the previous fits that have been completed with that session ID, if you need to revert back to a previous version.
# Entering Data
### Copy and Paste
Copying and pasting is fully supported, but works best on Chrome and Firefox. Cells are added automatically to the table.
### Importing Files
Comma or tab-delimited files can be imported using the "Import File" button on the bottom left. Based on the number of columns in the file, the following will be assumed:\
\
![Import Formats](static/img/formattable.png "Import Formats")\
\
You can still move columns around by copying and pasting them after you import. Header rows will be ignored (any row that starts with a non-numeric character).
### Data Format
Scientific notation, such as 1.0234e+03 or similar, is supported. Commas are supported per locales (see below).
### Quick Plot
Note that the plot on the top right doesn't plot error bars on the data, even if selected. You can hover over points to inspect data, or click on the point to highlight the relevant row in the table.
# Selecting a Fit Model
### Models
Many equations commonly used are included here, but you can enter your own by selecting Custom Equation. If you have a suggestion for a new permanent equation in the list, email [Matt Wiebold](mailto:matt@wiebolds.com).
### Starting and Fixed Values
![Fixed Parameters](static/img/fixed.png "Fixed Parameters")\
For each model, you can enter parameters that should be fixed by checking the box next to that parameter and entering a value. To suggest starting values for the fitting algorithm, enter a starting value for that parameter, but do not check the box next to it.
### Custom Functions
![Custom Functions](static/img/custom.png "Custom Functions")\
\
Select the Custom Function model, and use the box that appears to type in your model equation. Add or remove fit parameters with the buttons below the equation box. Be sure to use all the parameters you add in your equation (and don't use more than you've added). The function should include the independent variable x as well as your parameters, but "y =" is not necessary. Use Python syntax for equations, including '*' for multiplication, etc. Scientific notation is supported using lowercase 'e', such as 2.3e-4. Note some custom formulas, particularly those with constants hard coded, may not display properly in the PDF output but will be interpreted correctly. If you need a function added and it is included in Python's numpy, email [Matt Wiebold](mailto:matt@wiebolds.com). The following functions are supported:
- sin
- cos
- tan
- asin
- acos
- atan
- sinh
- cosh
- tanh
- log
- log10
- exp
- sqrt
- sinc
- abs
- floor
- ceil
### Uncertainties
![Uncertainties](static/img/xyuncert.png "Uncertainties")\
\
Be sure to select the appropriate buttons to indicate the uncertainties to use in the fit. The values entered in the table will not be used unless you select "Table" for each uncertainty. \
\
To enter expressions, you can enter constant values or multiplicative values as a percentage (1%) or a multiple (2x or 2*, for instance).
# Plotting Options
### Labels
The title, x and y label fields support LaTeX syntax by wrapping the LaTeX in dollar signs. For instance, you could write a label as:
```
Check out this label! $\int_0^\infty x^2 \, \mathrm{d}x$
```
which would appear as\
\
![LaTeX Label](static/img/label.png "LaTeX Label")
### Output Options
![Output Options](static/img/output.png "Output Options")\
\
Use Double PDF to output two side-by-side reports that can be cut and fit nicely into a lab notebook for a pair of lab partners. \
\
The Raw Data option outputs a PNG plot as well as a data table with residuals and model values at the x points entered. There is also a calculator that will give a y and dy for a given x and dx for the fitted model.
### Locales
User locale is determined via the browser, and data is interpreted accordingly. If there are no commas in the data (including the error fields and starting values), the data will use a period as the radix such that '1.000' is 1, no matter the user's locale.\
\
If there are commas in the data, behavior depends on the locale. If en_US or similar, commas will be interpreted as thousands separators and ignored in the data. If the user has a locale with commas as the radix, they will be interpreted as such, so that '1.000' is 1e3. Note this behavior only happens if there are commas in the data. This can lead to drastically different behavior, therefore a note is included in the footer of the PDF if the user is in a locale with comma radices but none are present so the data is intepreted using period radices, or if commas are present the user's locale will be included to note how the commas were interpreted.
# Technical Details
#### Data Table
The table is provided by [Handsontable](http://handsontable.com/), with support for copy and paste, as well as undo and redo.
#### Plot
The quick plot is provided by [Plotly](http://www.plotly.com/).
#### MathJax
MathJax is used to render the fit equations and the parameters.
#### Python
Fitting and plotting are done in Python using matplotlib, numpy and scipy.
#### LaTeX
PDFs are generated using LaTeX.