Having invoked Emacs (well, you should have never aborted it in the
first place) spawn a separate frame by typing C-x 5 2, or pulling down
the Files menu and selecting Make New Frame. In the new
frame invoke the Emacs Calculator by typing
M-# b M-# c.
Now enter our data from Figure 2.5 on page
[[1 2 3 4 5 6 7 8 9 10] \ [2.2p0.1 3.3p0.2 3.7p0.2 3.8p0.1 \ 4.6p0.2 4.9p0.1 5.3p0.1 6.2p0.2 6.4p0.1 7.1p0.2]]Do not type the backslashes. Type everything on one line. Do not type a return key at any stage either. Observe that as you type the data in, Emacs Calc reformats it in its own way, and places it on the stack. In particular sequences such as
2.2p0.1are automatically converted to
2.2 +/- 0.1When you have typed everything in, Calc should display the following in its main Calc window:
1: [ [ 1, 2, 3, 4, $ [ 2.2 +/- 0.1, 3.3 +/- 0.2, 3.7 +/- 0.2, 3.8 +/- 0.$ .You can scroll this listing left and right by pressing
>keys. At this stage there is just one data item on the stack. It is a very peculiar matrix whose top row contains the abscissa (xi) and the bottom row the ordinate with uncertainties ( ).
You can now invoke a predefined xfit function on that data by typing
IaF1[Ret]and, lo and behold, Emacs Calc replaces the data matrix with a polymorphic array that looks as follows:
1: [1.80828182942 +/- 0.0877310945595 + (0.515389369592 +/- 0.0143532455281) x, \ [1.80828182942, 0.515389369592], \ [ [ 7.69674495262e-3, -1.12072517511e-3 ] , , 15.3104140915, 0.0533833610329] [ -1.12072517511e-3, 2.0601565719e-4 ] ] .The backslashes are mine, to show line continuation. Otherwise the result wouldn't fit on the page. The first element of the matrix is a formula:
1.80828182942 +/- 0.0877310945595 + (0.515389369592 +/- 0.0143532455281) xYou should recognise this result. It tells us that
Calc is awfully clever. It knows everything about linear fits. It knows a lot about polynomial and multilinear fits too. It can even fit nonlinear models, such as exponentials, logarithms, power law, quadratic and gaussian.
Calc can produce Gnuplot graphics too. For example to plot the data itself enter two vectors xi and yi as follows:
[1 2 3 4 5 6 7 8 9 10][2.2 3.3 3.7 3.8 4.6 4.9 5.3 6.2 6.4 7.1]Calc will place them both on the stack, one under the other. Then invoke Calc function calc-graph-fast by typing
gfThe Gnuplot graph should appear on your display.
You can add further curves to this plot. For example in order to add to it the -fit curve, do as follows:
[1 2 3 4 5 6 7 8 9 10] '1.80828182942 + 0.515389369592 xWhen you type the quotation mark Calc will switch to an algebraic input mode. That's when you type in the formula. This should leave two items on the stack the vector
[1..10]and the formula. Now add the plot of that formula with xi replaced by
[1..10]to the graph with the command
gaThis will place you temporarily in the Gnuplot buffer, so that you can edit the resulting Gnuplot command, if you'd like to. Get out of there without typing anything, unless you know what you're doing! Type
C-x band change the buffer back to Calc. Once back in Calc type
gpto finish the plot. You should now see two curves on the display. The measured data and the fitted data. There are no error bars, because Calc doesn't have a precooked function for that. But this plot is informative in its own right. It gives us a hint as to why Q is so low.
Using Calc interactively can be quite a pain, since long formulas may be difficult
to edit and mistakes impossible to correct. Luckily, you can call Calc functions
from your Elisp programs .
The Calc function that hides behind the
key sequence is
xfit. In order to call it from, say, a scratch buffer
of your Emacs session proceed as follows. In the scratch buffer type
(calc-eval "xfit(a + b x, x, [a, b], [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [2.2 +/- 0.1, 3.3 +/- 0.2, 3.7 +/- 0.2, 3.8 +/- 0.1, 4.6 +/- 0.2, 4.9 +/- 0.1, 5.3 +/- 0.1, 6.2 +/- 0.2, 6.4 +/- 0.1, 7.1 +/- 0.2]])")You can edit it and modify to your liking. You can split it, indent it, and beautify in any way possible: new-lines and spaces are ignored by Calc in this context. Once you have finished typing it in, position Emacs cursor just after the last bracket and press C-j. When you do so Emacs/Calc will respond with:
"[1.80828182942 +/- 0.0877310945595 + (0.515389369592 +/- 0.0143532455281) x, \ [1.80828182942, 0.515389369592], \ [[7.69674495262e-3, -1.12072517511e-3], \ [-1.12072517511e-3, 2.0601565719e-4]], \ , 15.3104140915, 0.0533833610329]"The backslashes in the listing above are mine: Emacs types it all on a single line.
What happens here is as follows. xfit is the so called algebraic
function equivalent of the IaF command. It has the following synopsis.
Its first argument is the fit model. When conversing with IaF
we can simply give a model number: 1 for a linear model. But when calling
function xfit explicitly, we must define the model explicitly too.
Here the model is given by
a + bx. The second argument defines the
x, and the third argument gives the parameter
[a, b]. Finally, we pass the data, in the same format as before,
i.e., as a single matrix. Observe that this time, we have to type
p between yi and .
The whole call to
packed into a string, which is then passed as a single parameter to
calc-eval. This is quite like calling
in C. There the single argument to the
system function call is a string that
represents a shell command to be executed by function
the equivalent of that function is
calc-eval, and the equivalent
of shell is Calc.