Janosh
Janosh

Reputation: 4682

Gnuplot loop fitting, stats and plotting

I'm having a hard time getting my gnuplot script to work. Basically, I have six data files and I want to plot them in the same file with a fit for every data set.

It gets a little complicated because I have to reformat the data before plotting. For that I am using stats commands. Here lies the problem. The final plotting command contains a for loop in which the data formatting is done. Each stats command produces statistical data with a prefix Potentiali, where i goes from 1 to 6.

Now my question is, how can I access this running index in the plot loop?

This is my script:

#!/bin/bash

gnuplot << EOF

set terminal epslatex color size 16cm,11cm
set output "strain-energy.tex"

set xrange [-10:10]
set yrange [0:*]


fstr(N) = sprintf('f%d(x) = a%d*x**7 + b%d*x**6 + c%d*x**5 + d%d*x**4 + e%d*x**3 + f%d*x**2 + g%d*x + h%d', N, N, N, N, N, N, N, N, N)
eval(fstr(1))

fitstr(N) = sprintf('set fit quiet; fit [-10:10] f%d(x) ''/path/Shift_%d/potential.dat'' every ::1 using (\$1-Potential%d_pos_min_y):(\$2-Potential%d_min_y) via a%d,b%d,c%d,d%d,e%d,f%d,g%d,h%d', N, N, N, N, N, N, N, N, N, N, N, N)

do for [i=1:6] {
    stats "/path/Shift_".i."/potential.dat" every ::1 using (\$1):(\$2) prefix "Potential".i nooutput
    eval(fstr(i))
    eval(fitstr(i))
}

plot for [i=1:6] "/path/Shift_".i."/potential.dat" every ::1 using (100*((\$1-Potential.i._pos_min_y)/Potential_pos_min_y)):(1000*(\$2-Potential_min_y)) ls i title "\\\footnotesize{C".i."}", f1(x) ls 1, f2(x) ls 2, f3(x) ls 3, f4(x) ls 4, f5(x) ls 5, f6(x) ls 6

set output
EOF

Upvotes: 1

Views: 844

Answers (1)

Miguel
Miguel

Reputation: 7627

In newer gnuplot versions use the do for construction to create a string with all the values you need and then use the word() function to select the one you want. If your variables are a1, a2 and a3 (change to the name in your particular case) then do:

a1=1.1; a2=2.2; a3=3.3 # Values
a="" # a is the string where all the values are stored
do for [i=1:3] {eval "a = sprintf(\"%s %g\", a, a".i.")"} # Print a1, etc. to a
print a # Just check what a looks like
  1.1 2.2 3.3
a(i) = real(word(a,i)) # Create function that depends on i
print a(1) # Check that a(1) indeed gives a1
 1.1

You'll need to adapt the above code to your purposes.

Upvotes: 2

Related Questions