curve.fit

Custom Function

Your own equation in x with up to five parameters, A to E. Use it when the theory of your experiment gives a form that no built-in model has, and fit that form directly instead of linearizing it.

Fit the example dataStart with your own data

Example: NIST’s Misra1a adsorption data, 14 points, opened as a new copy you can edit.

Parameters

First parameter. Use the letters in order: A, then B, and so on.
Second parameter.
Third parameter.
Fourth parameter.
Fifth parameter. Write powers of ten with a lowercase e, such as 1.2e3, so they are not read as E.

Tips

  • Write the equation in Python syntax: * for multiplication, ^ or ** for powers, and parentheses for grouping. Pasted −, × and ÷ are accepted. The equation is checked as you type, and a matching built-in model is suggested when there is one.
  • Custom equations have no automatic starting values: a blank Guess starts at 2. Enter a guess for every parameter, ideally within a factor of a few of the answer. Fix a parameter at a known value, or give it limits, to keep it in a physical range.
  • Trigonometric functions use radians. For an angle in degrees, write radians(x). Logarithms need positive arguments, and sqrt and fractional powers non-negative ones, over every x and the range where you evaluate the model.
  • Custom equations use numerical derivatives, so a built-in model with the same form can converge faster. Saved fits and reports show the equation typeset, and the guide explains how to choose starting values.

Functions and constants

WriteMeaning
sin(x), cos(x), tan(x)Sine, cosine and tangent of an angle in radians
asin(x), acos(x), atan(x)Inverse sine, cosine and tangent, in radians
sinh(x), cosh(x), tanh(x), coth(x)Hyperbolic sine, cosine, tangent and cotangent
asinh(x), acosh(x), atanh(x), acoth(x)Inverse hyperbolic sine, cosine, tangent and cotangent
exp(x)e raised to the power x. Euler’s number itself is exp(1)
log(x), ln(x)Natural logarithm, base e. For base b, write log(x)/log(b)
log10(x)Logarithm base 10
sqrt(x)Square root
abs(x)Absolute value
floor(x), ceil(x)Round down or up to a whole number
sinc(x)Normalized sinc, sin(πx)/(πx). For sin(u)/u, write sinc(u/pi)
erf(x), erfc(x)Error function and complementary error function, 1 − erf(x)
fact(x)Factorial x!, computed as Γ(x + 1), so non-integer x is accepted
degrees(x), radians(x)Convert radians to degrees, or degrees to radians
besselj0(x), besselj1(x)Bessel functions of the first kind, J₀ and J₁, for circular apertures and membranes
pi, πThe constant π = 3.14159…

Examples

A*exp(-x/B)*cos(2*pi*x/C + D) + E
Damped oscillation with an offset, fitting the period C directly
A*exp(-(x-B)^2/(2*C^2)) + D*x + E
Gaussian peak on a sloping background, also built in as Gaussian on Linear Background
A/sqrt(1 + (x/B)^2)
Low-pass filter gain against frequency; B is the corner frequency
A*cos(radians(x) - B)^2 + C
Malus’s law with the analyzer angle in degrees
A*B^x*exp(-B)/fact(x)
Poisson counts with mean B, the same as the built-in Poisson model
A*tanh((x - B)/C) + D
Smooth step between two levels, centered at B with width C
A*(2*besselj1(B*x)/(B*x))^2 + C
Circular-aperture (Airy) diffraction pattern against angle; leave out x = 0, where it is 0/0

Related models

Guide: Fitting a custom equation