# 5.6 Function fitting

The fit command can be used to fit arbitrary functional forms to data points read from files. It can be used to produce best-fit lines1 for datasets, or to determine gradients and other mathematical properties of data by looking at the parameters associated with the best-fitting functional form.

The following simple example fits a straight line to data in a file called data.dat:

```f(x) = a*x+b
fit f() 'data.dat' index 1 using 2:3 via a,b
```

The first line specifies the functional form which is to be used. The coefficients of this function, a and b, which are to be varied during the fitting process, are listed after the keyword via in the fit command. The modifiers index, every, select and using have the same meanings as in the plot command.

When a function of variables is being fit, at least columns (or rows – see Section 3.9.1) of data must be specified after the using modifier. By default, the first columns are used. These correspond to the values of each of the arguments to the function, plus finally the value which the output from the function is aiming to match.

If an additional column is specified, then this is taken to contain the standard error in the value that the output from the function is aiming to match, and can be used to weight the data points which are being used to constrain the fit.

As an example, below we generate a data file containing samples of a square wave using the tabulate command and fit the first three terms of a truncated Fourier series to it:

```set samples 10
set output 'square.dat'
tabulate [-pi:pi] 1-2*heaviside(x)

f(x) = a1*sin(x) + a3*sin(3*x) + a5*sin(5*x)
fit f() 'square.dat' via a1, a3, a5
set xlabel '\$x\$' ; set ylabel '\$y\$'
plot 'square.dat' title 'data' with points pointsize 2, \
f(x) title 'Fitted function' with lines
``` As the fit command works, it displays statistics including the best fit values of each of the fitting parameters, the uncertainties in each of them, and the covariance matrix. These can be useful for analysing the security of the fit achieved, but calculating the uncertainties in the best fit parameters and the covariance matrix can be time consuming, especially when many parameters are being fitted simultaneously. The optional word withouterrors can be included immediately before the filename of the input data file to substantially speed up cases where this information is not required.

By default, the starting values for each of the fitting parameters is . However, if the variables to be used in the fitting process are already set before the fit command is called, these initial values are used instead. For example, the following would use the initial values :

```f(x) = a*x+b
a = 100
b = 50
fit f() 'data.dat' index 1 using 2:3 via a,b
```

If any of the fitting coefficients are not dimensionless – that is, they have physical units such as meters or seconds – then an initial value with the appropriate units must be specified.

A few points are worth noting:

• A series of ranges may be specified after the fit command, using the same syntax as in the plot command, as described in Section 3.14. If ranges are specified then only data points falling within these ranges are used in the fitting process; the ranges refer to each of the variables of the fitted function in order:

```fit [0:10] f() 'data.dat' via a
```
• As with all numerical fitting procedures, the fit command comes with caveats. It uses a generic fitting algorithm, and may not work well with poorly behaved or ill-constrained problems. It works best when all of the values it is attempting to fit are of order unity. For example, in a problem where was of order , the following might fail:

```f(x) = a*x
fit f() 'data.dat' via a
```

However, better results might be achieved if were artificially made of order unity, as in the following script:

```f(x) = 1e10*a*x
fit f() 'data.dat' via a
```
• For those interested in the mathematical details, the workings of the fit command are discussed in more detail in Appendix C.

Footnotes

1. Another way of producing best-fit lines is to use the interpolate command; more details are given in Section 5.7
{"ft_lang_label":"__label__en","ft_lang_prob":0.8225031,"math_prob":0.93820626,"size":2980,"snap":"2019-51-2020-05","text_gpt3_token_len":671,"char_repetition_ratio":0.12802419,"word_repetition_ratio":0.0390625,"special_character_ratio":0.22751679,"punctuation_ratio":0.098497495,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9910112,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,2,null,6,null,5,null,null,null,6,null,2,null,6,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-11T20:45:37Z\",\"WARC-Record-ID\":\"<urn:uuid:0a025d3f-b4e5-476b-8c4b-7d2498b45e98>\",\"Content-Length\":\"12567\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4a490955-c05b-4fe1-b1d6-a3301eae8a96>\",\"WARC-Concurrent-To\":\"<urn:uuid:d2804c8d-6858-496e-8706-22e1b63824f0>\",\"WARC-IP-Address\":\"93.93.130.44\",\"WARC-Target-URI\":\"http://pyxplot.org.uk/current/doc/html/sec-fit_command.html\",\"WARC-Payload-Digest\":\"sha1:QUIEIKSSAX4V3V7DEMYPHQD4GLFSU3GZ\",\"WARC-Block-Digest\":\"sha1:DONMAHPEI7NNA6XMUTCOQE3JWQFPUBC7\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540532624.3_warc_CC-MAIN-20191211184309-20191211212309-00399.warc.gz\"}"}