Plotting Functions in Gnuplot
Gnuplot is a portable, free, command-driven, interactive function, and data plotting program for Linux, OS/2, MS Windows, OSX, VMS, and many other platforms. Although copyrighted, the source code is freely available. It was initially developed to enable researchers and students to interactively visualize mathematical functions and data, but it has since expanded to cover a wide range of non-interactive purposes, including web scripting. Third-party programs like Octave also use it as a plotting engine. Numerous plot types are supported by Gnuplot in both 2D and 3D. It can draw utilizing a variety of associated text as well as lines, points, boxes, contours, vector fields, and surfaces. Additionally, it supports a number of specialty plot kinds. Gnuplot provides output in a variety of file formats like eps, emf, fig, jpeg, LaTeX, pdf, png, postscript, etc.
Installation:
- Download Gnuplot software from here.
- Double-click the downloaded executable file.
- Follow the instructions on the installation wizard to install Gnuplot.
- After successful installation, start Gnuplot and a screen like this will appear.
Plotting One Function
1. Without setting xrange and yrange: Plotting function in gnuplot is easy. In gnuplot, the exponentiation ^ uses ** so to plot a simple function like
f(x) = exp(-x^4 / 4)
In Gnuplot, the function becomes:
f(x) = exp(-x**4 / 4)
Type the below command:
plot exp(-x**4 / 4)
Output:
2. Setting xrange and yrange: One can opt to specify first the xrange and then the yrange in the form [minimum:maximum] before the function. One can also leave yrange or both. Below is the revised version of the above example:
plot [-4:4] exp(-x**4 / 4)
Output:
Plotting More than One Function
To plot more than one function, list all the functions separated by comma. Below is the example to implement the concept:
plot [-4:4] exp(-x**2 / 2), x**2 / 16
Output:
Defining Function
It is possible to define the function once so that there is no need to retype it each time. Below is the example to implement the concept:
f(x) = exp(-x**4 / 4)
plot [t=-4:4] f(t), t**2 / 16
Output:
Plotting Trigonometric Function
Below is the example to plot a sine and cosine function:
a = 0.9
f(x) = a * sin(x)
g(x) = a * cos(x)
# Plot
plot f(x) title ‘sin(x)’ with lines linestyle 1, \
g(x) notitle with lines linestyle 2
Here,
f(x) and g(x) is the function.
Statements starting with # are the comments.
Output:
Last Updated :
28 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...