Fitting a equation to a set of data

I have a set of data:

``````1.12158 0.42563 0.07
1.12471 0.42112 0.07
1.12784 0.41685 0.07
1.13097 0.41283 0.07
1.13409 0.40907 0.07
1.13722 0.40556 0.07
1.14035 0.40231 0.07
1.14348 0.39933 0.07
1.1466 0.39661 0.07
1.14973 0.39417 0.07
1.15285 0.39201 0.07
1.15598 0.39012 0.07
1.15911 0.38852 0.07
1.16224 0.3872 0.07
1.16536 0.38618 0.07
1.16849 0.38544 0.07
1.17162 0.385 0.07
1.17474 0.38486 0.07
1.17787 0.38543 0.07
1.181 0.38714 0.07
1.18413 0.38994 0.07
1.18725 0.39378 0.07
1.19038 0.39858 0.07
1.19351 0.40426 0.07
1.19664 0.41071 0.07
1.19976 0.41786 0.07
``````

The first column is the x-axis and the second column is the y-axis.

I want to fit this data to the equation:

``````Ax^2 + Bx + c
``````

and find out the values of A, B and c.

What program can I use ?

I would be very glad if you could show me how to do it.

Thanks.

GNUPlot: A CLI solution

Assuming `data.dat` is the file containing data.

``````\$ gnuplot
gnuplot> fit a*x**2 + b*x + c 'data.dat' via a, b, c
(...)
Final set of parameters            Asymptotic Standard Error
=======================            ==========================
a               = 22.2174          +/- 1.09         (4.906%)
b               = -51.7961         +/- 2.53         (4.885%)
c               = 30.5745          +/- 1.468        (4.802%)
(...)
``````

See Fit section in the docs for more options.

You could also directly pipe to GNUPlot:

``````printf '%s\n' 'fit a*x**2 + b*x + c "data.dat" via a, b, c' | gnuplot
``````

XMGrace (also know as Grace): A GUI solution

Assuming `data.dat` is the file containing data.

``````xmgrace data.dat
``````

The XMGrace window pops up with a curve representing data.

On toolbar, select `Data > Transformations > Regression`. Choose `Type of Fit: Quadratic` and `Accept`.

A new curve with the fit will be plotted and a "console" will pop with this:

``````(...)
y = 30.575 - 51.796 * x + 22.217 * x^2
(...)
``````

You can use the GUI further to make data as black dots and fit as red curve.

XMGrace also offers a CLI interface, although some features are absent from it. You can learn more by visiting the docs.

Solution in R via CLI

First in Linux terminal type `R`

Then

``````data.dat<-read.table(textConnection("a b c
1.12158 0.42563 0.07
1.12471 0.42112 0.07
1.12784 0.41685 0.07
1.13097 0.41283 0.07
1.13409 0.40907 0.07
1.13722 0.40556 0.07
1.14035 0.40231 0.07
1.14348 0.39933 0.07
1.1466 0.39661 0.07
1.14973 0.39417 0.07
1.15285 0.39201 0.07
1.15598 0.39012 0.07
1.15911 0.38852 0.07
1.16224 0.3872 0.07
1.16536 0.38618 0.07
1.16849 0.38544 0.07
1.17162 0.385 0.07
1.17474 0.38486 0.07
1.17787 0.38543 0.07
1.181 0.38714 0.07
1.18413 0.38994 0.07
1.18725 0.39378 0.07
1.19038 0.39858 0.07
1.19351 0.40426 0.07
1.19664 0.41071 0.07
``````

Then

``````plot(data.dat\$a,data.dat\$b,col="red",type="b")
``````

For solving use the following

``````fit<-lm(data.dat\$b~poly(data.dat\$a,2,raw=TRUE))
summary(fit)

Call:
lm(formula = data.dat\$b ~ poly(data.dat\$a, 2, raw = TRUE))

Residuals:
Min         1Q     Median         3Q        Max
-0.0041754 -0.0021479  0.0004573  0.0019714  0.0059427

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)                        30.575      1.468   20.83  < 2e-16 ***
poly(data.dat\$a, 2, raw = TRUE)1  -51.796      2.530  -20.47 2.91e-16 ***
poly(data.dat\$a, 2, raw = TRUE)2   22.217      1.090   20.38 3.20e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.002729 on 23 degrees of freedom
Multiple R-squared:  0.9568,    Adjusted R-squared:  0.9531
F-statistic:   255 on 2 and 23 DF,  p-value: < 2.2e-16

``````