Showing posts with label tools. Show all posts
Showing posts with label tools. Show all posts

Wednesday, July 7, 2010

Gnuplot Fitting

If you aren't using gnuplot, you should check it out.  For those that do, I just figured out a fun tidbit on how to print fit errors that I couldn't find on the Intarwebs for the life of me.

The "fit" command in gnuplot works much like the "plot" command; you give it a data file and a "using" specification, and also the function you want to fit with and its parameters like so:

f(x) = a*x + b
a = -0.01
b = 0
fit f(x) 'somefile.txt' using 1:2:3 via a,b

You do need to give a first guess for parameters, as you see above.  The "using" specification here tells it to use the first column for x, second for y and the third for error in y (which you want to include if you can).  I've had plenty of trouble in the past getting a fit to converge, but generally you need to keep playing with your initial parameters and things will work out.  Once the fit is done you get something like this:

After 4 iterations the fit converged.
final sum of squares of residuals : 8.19305
rel. change during last iteration : -6.6513e-10


degrees of freedom    (FIT_NDF)                        : 9
rms of residuals      (FIT_STDFIT) = sqrt(WSSR/ndf)    : 0.954117
variance of residuals (reduced chisquare) = WSSR/ndf   : 0.910339


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


a               = -0.00123217      +/- 0.0002571    (20.87%)
b               = 0.0347759        +/- 0.001622     (4.665%)


correlation matrix of the fit parameters:
               a      b      
a               1.000 
b              -0.872  1.000 

Huzzah!  You get your final fit parameters with error; you get degrees of freedom and stdfit and chisquare.  This is fun and all, but how to I put that in my plot?  First, you can get at some environment variables to use in your gnuplot script, like "FIT_WSSR" and "FIT_STDFIT."  As far as the final parameters and errors, here's the best way I've found to put them on your plot:

set fit errorvariables
fit f(x) 'group_asym_out.txt' index 1 u 1:2:3 via a,b 
set label sprintf("a = %2.6f +- %2.6f",a,a_err) at graph 0.7,0.15
set label sprintf("b = %2.6f +- %2.6f",b,b_err) at graph 0.7,0.1
plot 'somefile.txt' u 1:2:3 w yerr, f(x) title 'f(x)=a*x+b'

Here I've put the tricky part in bold.  We have to set labels on the graph somewhere; mercifully we can use the "graph" coordinates instead of the coordinates of the plot itself, so that top right is graph (1,1) and bottom left is graph (0,0).  Now we access our parameters "a" and "b" using "sprintf" in the label.

To get the errors on these parameters, we need to include "set fit errorvariables" which enables the error variables "_err," for example "a_err" and "b_err."  The graph still needs some stylistic attention, but at this point it looks this:


Sources: Janert's Gnuplot in Action

Wednesday, June 9, 2010

Introduction

Welcome to our Physics topic review blog!  Jonathan, Stache and I created this blog to be a centralized and easy to access place to collect summaries of review materials.  As a Physics student, whether preparing to write a thesis or not, periodic review of our field is crucial.  As we read through papers and textbooks keeping good notes strengthens the understanding and reinforces memory.  We decided a common place to share our review notes would be an excellent resource for others in the group.

Thus, our goal is to provide regular updates on our Physics readings, here shared for our fellow graduate students as a motivation for review and further learning in our field for ourselves and others.  We hope more graduate students will join us and share their review summaries.  No area of Physics will be too simple or too complicated; the simplest of topics is still useful to recall and obtuse posts will challenge and lead to deeper reading.  The comments could be an excellent place for others to correct or ask questions.

For contributors:  
We'd like this blog to provide interesting and easy to digest snippets for those following the RSS feed.  To this end, we'll try to keep these entries short; just a handful of paragraphs should be enough to inform and jog the memory.  Links and cites to your source are crucial to allow these little bite sized entries to lead to deeper reading for those interested.

To keep things well organized, tags or "labels" as they are called in blogger, are key.  Required tags for each post should be your last name, and the general subject area; more specific keys are encouraged at your discretion.

This handy LaTeX equation maker will no doubt be useful to you all in your posts:

http://www.codecogs.com/latex/eqneditor.php

It should be self explanatory, and it is easy enough to get the original LaTeX back out (mouse over).