CPSC 327 | Data Structures and Algorithms | Spring 2010 |
Gnuplot is a quick and easy plotting program. You can plot functions (like x+6) or data from files.
Start gnuplot from a shell by typing:
gnuplot
You should then get a gnuplot prompt (gnuplot>) where you type gnuplot commands.
To plot a function, type something like
plot x
You can also try the following:
plot 3*x+6 plot x**2 plot log(x)
This plots the functions 3x+6, x2 (note the use of ** to denote exponentiation), and ln(x) respectively. For base 10 logs, use log10(x). For logs of other bases, you can exploit the fact that loga b = logc b / logc a.
For gaining intuition about big-Theta, it is useful to change the range of x values shown in the plot. Try:
set xrange [0:10] replot
The first command sets the range of x values to be 0 to 10, and the second repeats the last plot command. (The display won't update until you issue a replot or plot command.)
To test if you've gotten a function into the right big-Theta class, you can first plot the lower bound, your function, and the upper bounds. For example, to test if 3n+6 = Theta(n), I might choose n as the lower bound and 4n as the upper bound. Then plot:
plot x, 3*x+6, 4*x
(Note that n is replaced by x.)
Now, set the xrange to something smallish:
set xrange [0:10] replot
Observe that 3x+6 does eventually end up between the two bounds. But let's try a larger range:
set xrange [0:100] replot
Now 3x+6 seems to be pretty solidly between the bounds. Try increasing the range a few more times, though:
set xrange [0:1000] replot set xrange [0:10000] replot
Observe that the shape of the plot changes less and less with each increase, and that 3x+6 stays solidly between the lines. Looks like our big-Theta is correct!
Useful tip: use the up arrow to retrieve previously-typed commands. (You can also edit them before pressing enter.)
Now pick a big-Theta that grows too fast, say n2. Plot x2, 3x+6, and 4x2 (to use the same constant multipliers as we picked before) starting with a small x range and then repeatedly increasing the x range. What happens?
Now pick a big-Theta that grows too slowly, say 1. Plot 1, 3x+6, and 4 (to use the same constant multipliers as we picked before) starting with a small x range and then repeatedly increasing the x range. What happens?
Typing
help
takes you into gnuplot's help system, which will tell you quite a bit about what it can do. Press enter when asked for a help topic to get back to the gnuplot prompt.
Quit gnuplot by typing
quit