# How can I plot a sum function using gnuplot?

I want to plot the following function `sum a*exp(-0.001*(x-b)**2)` where a and b values are listed in the 1st and 2nd columns of an input file respectively, like this

``````a1 b1
a2 b2
a3 b3

.....
an bn
``````

now I am plotting it using the following command

``````plot a1*exp(-0.001*(x-b1)**2) + a2*exp(-0.001*(x-b2)**2) + a1*exp(-0.001*(x-b3)**2) ...etc
``````

what I need is a short command to plot all data without writing all the data one by one.

the expected output should be one continuous line like this • please update your title, it says 'gun' plot, which can be open to interpretation.. – captcha Nov 9 '17 at 21:06
• ok done it's my bad – Mohsen El-Tahawy Nov 9 '17 at 21:14
• @MohsenEl-Tahawy, and how should look your expected plot? just points, joined points? – RomanPerekhrest Nov 9 '17 at 21:25
• ok, see the attached figure – Mohsen El-Tahawy Nov 10 '17 at 0:21

The simplest is probably to use awk to generate the wanted plot command. For example,

``````f(a,b,x)=a*exp(-0.001*(x-b)**2)
sum(x) = `awk <data '{v = v sprintf("f(%s,%s,x)+",\$1,\$2)} END{print v "0"}'`
set xrange [250:650]
plot sum(x)
``````

This uses backticks inside the gnuplot command file to run awk which reads from the data file and generates a string like:

``````f(1,2,x)+f(3,4,x)+...0
``````

where 1,2 3,4 are the first 2 sets of values from the data. The gnuplot user-defined function `sum()` is set to this, and you can plot it. • this does not give what I want the a*exp(-0.001*(X-b)**2) can be plotted in Gnuplot without defining X, I need to plot the command {plot a1*exp(-0.001*(x-b1)**2) + a2*exp(-0.001*(x-b2)**2) + a1*exp(-0.001*(x-b3)**2) ...etc} using short command – Mohsen El-Tahawy Nov 10 '17 at 0:19
• this is exactly what I want – Mohsen El-Tahawy Nov 10 '17 at 11:08

If I understood correctly you are looking for something like:

with gnuplot 5.0:

``````gnuplot> set title "sum columns"
gnuplot> set size square
gnuplot> plot 'inputfile' u 1:(\$1*exp(-0.001*(x-\$2)**2)) w linespoints
``````
• why do you define x? a function <<a1*exp(-0.001*(x-b1)**2)>> doesnot need to define x. – Mohsen El-Tahawy Nov 10 '17 at 0:14
• @MohsenEl-Tahawy, yes, see my update – RomanPerekhrest Nov 10 '17 at 8:31