Fitting a custom equation
A custom equation lets you fit a model suggested by your experiment rather than choosing a built-in form. The harder part is usually deciding what the parameters mean and whether your data can determine them.
For entry syntax, supported functions and equation-assistance controls, see Custom Equations in Help. This guide uses a decaying signal with a background as an example.
Start with a physical interpretation
Suppose a signal approaches a constant background after an initial disturbance:
A is the initial signal above the background, B is the decay time and C is the background. If x is measured in seconds, B must also be in seconds; the exponent must be dimensionless. A and C have the units of y.
In curve.fit, select Custom Function and enter:
A*exp(-x/B)+C
Use three parameters, A through C. Do not include y =. Multiplication must be explicit. A starting value of zero for B is invalid because it appears in a denominator.
Choose starting values from the data
Estimate C from the late-time baseline. Estimate A as the initial value minus that baseline. B is the time for the signal above background to fall to about 37% of its initial size.
For a signal that starts near 12, approaches 2 and has fallen to about 5.7 at x = 3 seconds, plausible guesses are A = 10, B = 3 and C = 2. These are illustrative starting values, not fitted results.
Try more than one reasonable set of guesses. A nonlinear solver can converge to a local solution, struggle with a poor starting point, or find multiple parameter combinations that fit similarly well. Convergence alone does not establish that the physical interpretation is correct.
Check whether the parameters can be separated
If the data cover only the very beginning of the decay, a slow decay with one background may resemble a faster decay with another. Collecting points near the baseline can help distinguish B from C.
An even clearer example is A*B*x: only the product A×B affects the prediction. No amount of precision at the same kind of observations separates A and B without additional information. Use a single slope parameter unless the experiment independently constrains one factor.
Fix a parameter only when you have a reason to treat it as known. Fixing an uncertain quantity does not propagate its uncertainty into the remaining parameters. A small reported uncertainty after fixing a parameter can therefore understate the uncertainty of the full experiment.
Respect the equation's domain
Check every observed x and the range where you plan to evaluate the model. Logarithms require positive arguments; square roots and fractional powers can have restricted real domains; denominators must stay away from zero. A syntactically valid expression can still be numerically undefined.
curve.fit supports pi and π as constants. Trigonometric functions use radians. For example, a sinusoid with period B can use A*sin(2*pi*x/B)+C. See Help for the supported expression language rather than assuming arbitrary Python is accepted.
Built-in versus custom forms
Use the matching built-in model when it describes your experiment and its parameter definitions suit you. Built-ins have model-specific initial guesses and, when enabled, analytic derivatives. Custom equations use numerical derivatives, so equivalent forms need not follow an identical numerical path.
The uncertainty interpretation is the same choice: Absolute propagates entered uncertainty scales; Nominal estimates a common scale using residuals. Neither convention resolves parameters that the data cannot separately determine.