Intro to Scientific Computing

PHYS 27/193

Physics Department
University of the Pacific

Fitting Functions to Data

So, now we can do science, i.e. discover something about the world. We'll start with a trivial example.

Suppose you and a friend are confronted with the following circuit in lab (likely you did this in the 3rd grade, or in PHYS 55).

You have a variable (i.e. at your control) current source, I, in a simple circuit with a resistor, R, whose value you do not know. However, as your friend varies the current, you are able to measure the voltage, V, across the resistor. As you know from your studies of electromagnetism, the voltage should be related to the current by Ohm's Law:

V = IR

Thus, if you plot your measurements of V versus I, you could find the resistance, R, from the slope of the data.

Here's the data. You can also download the file iv.dat to save you typing it in by hand (right-click, Save Link).

# I    V_R
#amps volts

0.00  0.00
0.10  6.77
0.20  33.84
0.30  42.38
0.40  53.07
0.50  69.87
0.60  81.45
0.70  96.91
0.80  103.90
0.90  111.99
1.00  125.11

Before we begin, notice that the data file has comments at the top, which begin with a #. You can put comments anywhere in your data file; everything on the line after a # will be ignored by gnuplot.


Start gnuplot and plot this data.

You can see that the voltage rises sort of linearly with increasing current.

Estimate the slope by eye.
What is your estimate (write it down or remember it for later)?

Let's try to fit a line to the data "by hand".

Define a linear function: B

f(x) = a*x + b
(that's a line, right?)

Since the first data point is (0,0), and since Ohm's law has no constant term, let's set:

b = 0.

Knowing this, we could just as easily defined f(x) = a*x.

To set a, take your in initial guess from above:

gnuplot>a = your estimate from above # this is a number! 

Now, plot the data and your function together.

gnuplot> plot "iv.dat", f(x)

How well does your guess match?

You can refine your guess by typing a new value for a, and doing replot.

Try it.

Do you see how the line changes as you change the slope, a?

What is your best estimate of a?


Least Squares Fit

Gnuplot has a wonderful builtin command to automate this process, in a very powerful and flexible way.
The command is called fit and you use it like this:

gnuplot> fit f(x) "iv.dat" via a
gnuplot> plot "iv.dat", f(x)

The syntax of the fit command is:

fit function "datafile" via var1, va2, var3,...

Here

When you run the fit command, it will spew a fair amount of information as it changes the variables you specified with via. Finally, it will converge on the "Best" values, and print a summary of the results, which looks like this:

...
After 3 iterations the fit converged.
final sum of squares of residuals : 231.793
rel. change during last iteration : -8.79516e-08

degrees of freedom    (FIT_NDF)                        : 10
rms of residuals      (FIT_STDFIT) = sqrt(WSSR/ndf)    : 4.81449
variance of residuals (reduced chisquare) = WSSR/ndf   : 23.1793

Final set of parameters            Asymptotic Standard Error
=======================            ==========================

a               = 130.403          +/- 2.454        (1.882%)


correlation matrix of the fit parameters:

               a
a               1.000
This contains a wealth of statistical information about your data and the level of its randomness, most of which is beyond the scope of this course. However, it's nice to know that it's there as you learn more about statistics in subsequent courses. We'll look at these things a little bit soon.

The important line is:

Final set of parameters            Asymptotic Standard Error
=======================            ==========================

a               = 130.403          +/- 2.454        (1.882%)

This is your result: you have analyzed your data and found the resistance, R, to be about 130 Ohms, plus or minus 2.5 Ohms.

Whoa...


Next: Fitting Data II