# MiscEl: Curve fit

 Back to MiscEl main page Write a message to author This page does a curve fit on data. The resulting curve fit can be extracted either as a formula or as a table.
The formulas can be adapted to any high level computer language and be used for conversion of real world data to useable units. The tables can be used either in high level computer languages or in assembler language for fast conversion.

The curve fit works in multiple steps:
• More data points can be added with a spline algorithm.
• A curve fit is done with the selected algorithm.
• A table (with any number of entries) can be generated from the fit.

### Algorithm supported

The fit can be done with different algorithms, not all algorithm are suitable for all data and sometimes a suitable algorithm might fail to fit. A failed fit can maybe be fixed by adding your own starting values.
Fit format:
• Spline: The standard spline algorithm, it will always match all points and use soft curves between points
• Poly fit 1: Breaks the curve up into segments of straight lines, it will always match all points
• Poly fit 2: Use a 2 degree polynomial to make curved lines between the points, it will always match all points
• Poly fit 3: Use a 3 degree polynomial to make curved lines between the points, it will always match all points
• Poly fit 4: Use a 4 degree polynomial to make curved lines between the points, it will always match all points
• Straight line: Use all point and find the best straight line
• Poly line fit 2: Use all point and find the best 2. degree polynomial
• Poly line fit 3: Use all point and find the best 3. degree polynomial
• Poly line fit 4: Use all point and find the best 4. degree polynomial
• Poly line fit 5: Use all point and find the best 5. degree polynomial
• Poly line fit 6: Use all point and find the best 6. degree polynomial
• Reciprocal straight line: As above, but use 1/fit
• Reciprocal poly line fit 2: As above, but use 1/fit
• Reciprocal poly line fit 3: As above, but use 1/fit
• Reciprocal poly line fit 4: As above, but use 1/fit
• Reciprocal poly line fit 5: As above, but use 1/fit
• Reciprocal poly line fit 6: As above, but use 1/fit
• a×exp(b×x): Use an exponent function for fitting
• a×exp(b×x)+c: Use an exponent function and a offset for fitting
• Power fit: Use a power function: a×x^b+c
• User fit a: Write your own formula with one parameter
• User fit a,b: Write your own formula with 2. parameters
• User fit a,b,c: Write your own formula with 3. parameters
• User fit a,b,c,d: Write your own formula with 4. parameters
• User fit a,b,c,d,e: Write your own formula with 5. parameters
• User fit a,b,c,d,e,f: Write your own formula with 6. parameters
• User fit a,b,c,d,e,f,g: Write your own formula with 7. parameters
The higher degree fits will nearly always match the data better, BUT watch out for uncontrolled behaviour.
See Calculator for syntax and available functions when writing your own formula

### Input values

The input values can be entered in different formats.
The X value must be listed in increasing values.
When the fitting is done the f(x) or y is the value that can be calculated form the x or time value.
Data format:
• x , f(x): Standard input, X is first followed by Y (or f(x))*
• f(x), x: Swapped input*
• x <tab> f(x): Standard input, X is first followed by Y (or f(x)), delimeter is a tab character*
• f(x) <tab> x: Swapped input, delimeter is a tab character*
• hh:mm:ss.sss , y (sec): The first value is a time, all fitting is done with the time converted to seconds
• hh:mm:ss.sss , y (min): The first value is a time, all fitting is done with the time converted to minutes
• hh:mm:ss.sss , y (hour): The first value is a time, all fitting is done with the time converted to hours
• hh:mm:ss.sss , y (days): The first value is a time, all fitting is done with the time converted to days
• hh:mm:ss.sss , thermo-sensor (sec): Fit a temperature, all fitting is done with the time converted to seconds
• hh:mm:ss.sss , thermo-sensor (min): Fit a temperature, all fitting is done with the time converted to minutes
• hh:mm:ss.sss , thermo-sensor (hour): Fit a temperature, all fitting is done with the time converted to hours
• hh:mm:ss.sss , thermo-sensor (days): Fit a temperature, all fitting is done with the time converted to days
• x , thermo-sensor: Fit a temperature with any scale
• 2 × Temp, thermo-sensor-inputvalue: SPECIAL, see below
*A space character can also sometimes be used as delimeter.
The "thermo-sensor" settings uses the page Thermo sensors to convert the supplied value to a temperature. The correct sensor must be selected on this page.
Any line starting with ; or / is ignored.
 Fit start Restrict the used input data and the table output, only X values at or above this value will be used Fit end Restrict the used input data and the table output, only X values at or below this value will be used Fit steps How many steps to generate in the table Add spline points Extend the input data with lots of extra points, these are calculated using the spline algorithm. This will sometimes improve a fit and prevent uncontrolled behaviour. User fit Your own algorithm, the coefficents are named a,b,c... and input value is x Initial guess Starting function for a fit algorithms, use the arrow to fill in the currently calculated coefficents. The checkbox can be used to lock a value, i.e. it will not be changed when doing a fit. Save format Save the result, either as a table or as a formula (Not all fits supports formulas), the save is always to the clipboard Update Force a new fit calculation

### Output values

 Chi˛ A measure of the fit quality, a low value is best Fit The fit algorithm used a,b,c,... The coefficents for the fit algorithm
Save format:
• X,Y: Floating point values for both
• Y: Floating point value for Y
• XY (int8): Scaled into the range -127..127 for both x and y
• Y (int8): Scaled into the range -127..127 for y
• Y (uint8): Scaled into the range 0..255 for y, this setting can only be used if all y values are positive
• XY (uint8, ofs): Scaled into the range 0..255 for both x and y, any starting offset is moved outside the table
• Y (uint8, ofs): Scaled into the range 0..255 for y, any starting offset is moved outside the table
• XY (int16): Scaled into the range -32767..32767 for both x and y
• Y (int16): Scaled into the range -32767..32767 for y
• XY (uint16, ofs): Scaled into the range 0..65535 for both x and y, any starting offset is moved outside the table
• Y (uint16, ofs): Scaled into the range 0..65535 for y, any starting offset is moved outside the table
• XY (int24): Scaled into the range -8388607..8388607 for both x and y
• Y (int24): Scaled into the range -8388607..8388607 for y
• XY (uint24, ofs): Scaled into the range 0..16777215 for both x and y, any starting offset is moved outside the table
• Y (uint24, ofs): Scaled into the range 0..16777215 for y, any starting offset is moved outside the table
• XY (int32): Scaled into the range -2147483647..2147483647 for both x and y
• Y (int32): Scaled into the range -2147483647..2147483647 for y
• XY (uint32, ofs): Scaled into the range 0..4294967295 for both x and y, any starting offset is moved outside the table
• Y (uint32, ofs): Scaled into the range 0..4294967295 for y, any starting offset is moved outside the table
• Y (no scale): no autoscale is used, the data must fit into the integer range
• Y (no scale, ofs): no autoscale is used, the data must fit into the integer range, any starting offset is moved outside the table
• Formula: The generated formulas are saved in C syntax
When using a table, it is always possible to do a simple interpolation between entries, to get better resolution. The precision will depend on the type of data and the interpolation algorithm.
The most usefull tables are the ones with only the Y value, scale X to the table size and use the scaled X as index into the table.
Using ofs settings can increase the resolution in a smaller table (i.e. using int8 instead of int16).

### 2 × Temp, thermo-sensor-inputvalue

This is not really a input data format, but a special function to generate tables for use with thermo sensors.
Four input values are required:
On first line: Min temperature and corresponding input value
On second line: Max temperature and corresponding input value

The "input value" will typical be the digital output from an ADC and the "temperature" will be the corrosponding temperature.
The operation performed will be as follow:
• Get output from thermo sensor at the specified min and max temperature
• Assume linear correlation between sensor value and thermo-sensor-inputvalue
• Generate a table of temperature v.s. thermo-sensor-inputvalue
• Do a curve fit on table
The currently selected sensor on the Thermo sensors page will be used.
CAVEAT It is very important that the ADC measures the property that the Thermo sensors page uses as property for the sensor. I.e. if the sensors has resistance output, the ADC must measure resistance, not the voltage from a voltage divider! If your want to measure resistance, use a constant current source.

### Hints

• The data input is calculating
• The page can automatic generate linearizing tables for thermo sensors. For other sensors your have to make your own table with some "real value"/"input value" pairs, then the page can also generate tables and formulas for them.

### Calculator functions

The Calculator can do spline fits and evalute polynoms:
``` InterpolateSpline(x,x1,y1,x2,y2,x3,y3,....) InterpolateSpline(x,Array) EvalPoly(v,xn,xn-1,....,x,c) EvalPoly(v,array) ```

### Other pages

• Integer math can help optimize calculations with integers
• MPU Support list where MiscEl can help when working with microprocessors