1

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

the expected output should be one continuous line like this

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

2 Answers 2

2

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.

enter image description here

2
  • 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 Commented Nov 10, 2017 at 0:19
  • this is exactly what I want Commented Nov 10, 2017 at 11:08
2

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

The output: enter image description here

2
  • why do you define x? a function <<a1*exp(-0.001*(x-b1)**2)>> doesnot need to define x. Commented Nov 10, 2017 at 0:14
  • @MohsenEl-Tahawy, yes, see my update Commented Nov 10, 2017 at 8:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .