# 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 right corner of the page, and 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:
http://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:
http://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:
http://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 only works reliably on Chrome and Firefox. Safari doesn't allow access to the local clipboard, which causes trouble. 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:
| Number of Columns | Assumed Format |
|-------------------| ---------------|
| 2 | x, y |
| 3 | x, y, dy |
| 4 | x, dx, y, dy |
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 (as in 10,000) are not supported.
### 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:wiebold@stolaf.edu).
### 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. The following functions are supported:
- sin
- cos
- tan
- asin
- acos
- atan
- sinh
- cosh
- tanh
- log
- log10
- exp
- sqrt
- sinc
- abs
If you need a function added and it is included in Python's numpy, email [Matt Wiebold](mailto:matt@wiebolds.com).
### 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.
Plotly output will send you to a graph on plotly where you can edit labels, titles, fits, etc. and share the plot with others. It will prompt you to sign in, which you can ignore. There is a known issue with the formatting of the fit data label, it can be edited by clicking "Fork and Edit" at the top right of the plot and moving it manually.
# 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 [Highcharts](http://www.highcharts.com/).
#### jQuery
jQuery is used to manipulate the DOM when loading, plotting and submitting data. Data is submitted using AJAX and stored on the server in a sqlite database.
#### 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.
#### Strapdown.js
This page is written in [Markdown](http://daringfireball.net/projects/markdown/) and generated by [Strapdown.js](http://strapdownjs.com/).