Search code examples
printfgnuplot

Gnuplot - how to print a function to a txt file


I would like to print a function to a file in gnuplot. Here is a minimal example:

set print 'test.txt'
a=2
y(x)=sin(x)
set xrange [-5:5]
plot y(x)
print a
print y(x)

All lines do work, besides the last line, which gives an error: undefined variable:x. How is it possible that the plot command works, but not the print command.

How can I print the x- and y-data of the y(x) function to a txt-file?


Solution

  • The command you are looking for is set table.

    a=2
    y(x)=sin(x)
    set xrange [-5:5]
    set table 'test.txt'
    plot y(x) title "a=2"
    unset table
    

    The file text.txt now contains:

    # Curve 0 of 1, 100 points
    # Curve title: "a=2"
    # x y type
    -5  0.958924  i
    -4.89899  0.982641  i
    -4.79798  0.996339  i
    -4.69697  0.999881  i
    -4.59596  0.99323  i
    -4.49495  0.976453  i
    -4.39394  0.949722  i
    -4.29293  0.913309  i
    -4.19192  0.867586  i
    -4.09091  0.813018  i
    ...