# Pgfplots and arithmetic with data files

I have a file (table) with a lot of columns. In the first column I have time and in the nexus three columns that represent vector components. Now I want to plot vector magnitude in function of time. It is possible to do it with a simple command?

Another problem. I have a file containing an angular speed and a work that are related. Now, instead of work I want to plot efficiency. It is possible to divide the work column by a number? And if I have to use a different number for each work point and his differents numbers are stored in a file, it is still possible?

For the first case I have the following code:

\documentclass{standalone}

\useoackage{pfgplots}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[title={}, xlabel={$x$}, ylabel={$z$},%
legend cell align=left]

table[x index=0,y index=1] {mydata.dat};

\legend{}
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}


And the following file:

0 0 0 0
1 0.1 0 0
2 0.2 0.1 0
3 0.3 0.2 0.1


I want to plot the square root of the sum of the square of the last three columns (the columns represent vector components, so I want the magnitude) in function of time (the first column). Is it possible?

Ps: I posted this two problems in the same question because I think they are strictly related.

-
It really would help if you composed a fully compilable MWE that at least sets up the plot and has a few sample data points. While solving problems is fun, setting them up is not. You can use filecontents so that the data points can be packaged in one file that can be cut and pasted to get those trying to help started. –  Peter Grill Oct 7 '12 at 20:07
@PeterGrill Sorry. I don't have the data on this computer. I will try to improve my question! –  R. M. Oct 7 '12 at 20:22
@PeterGrill Edited. I hope it is better. –  R. M. Oct 7 '12 at 20:27
This is not a major problem, but the title is slightly misleading because it's not really calculus you're doing, just arithmetic. I first looked at this question thinking you wanted a way to calculate numerical derivatives in TeX ;-) –  David Z Oct 7 '12 at 20:44

This is indeed possible using the x expr and similar options to \addplot table, described in section 4.2.5 of the PGFplots documentation. These options allow you to manually calculate the values to plot based on the input data, using PGF's math parser. The usage is probably best shown by example:

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}

Your other example can be done the same way if all the numbers are in the same file; you'd just need to replace the mathematical expression with the appropriate one for the new situation. If you have your numbers in different files, then you could write a shell script to pull the numbers from the various files and write them to standard output, and then use \addplot shell to make PGFplots parse the output of the command. But unless the numbers are changing frequently, it's probably less work to just run the shell command manually to create mydata.dat once.
Very nice! Note that instead of merging different files using a shell script, you could also use a create on use column style to pull the data together right within TeX. See for example tex.stackexchange.com/questions/53227/… –  Jake Oct 7 '12 at 20:46