Gnuplot Tutorial 1 – Graph Basics

Gnuplot is a command-driven graphing software. It is free and available in Windows, OS2, DOS and Linux. If you are using Windows, then your Gnuplot window would look like Figure 1.

Figure 1 – The GNU Plot window.

The text gnuplot> at the bottom left of your Gnuplot window is called the Gnulot prompt. It is used for typing command just like in the Windows command prompt.

In this tutorial, we are going to plot the graph of the sine function and modify several options of our output. To plot the graph of the sine function, type plot sin(x) in your GNUPlot prompt (see Figure 2) and then press the ENTER key.

Figure 2 – THE GNUPlot window after plotting the graph of the sine function.

Figure 3 shows the graph of the sine function produced by Gnuplot after pressing the ENTER key. Notice that the x and y axes have are hidden and have no labels.   The x-axis interval is 5 and the y-axis interval is 0.2. We can also notice that the range of the x-axis displayed on the window are from -10 to 10 and the range of they y-axis displayed is from -1 to 1. We will change these settings one-by-one.

Figure 3 – The graph of the sine function.

In this plot we are make our graph look like the one shown in Figure 4. Our modifications are the following:

1.)    Write a title “Graph of the Sine Function”.
2.)    Label the x-axis with “x”
3.)    Label the y-axis with “y”
4.)    Show the x and the y-axis.
5.)    Change the range of the x-axis from -7 to 7.
6.)    Change the range of the y-axis from -2 to 2.
7.)    Set the interval of the x-axis interval to 1
8.)    Set the interval of the y-axis interval to 1.

Figure 4 – Modifed graph of the sine function.

To make the modifications above, just type the commands in the Command column and then press the ENTER key after typing. Remember that after pressing the ENTER key after a command is written, you can always view the changes by typing replot and pressing the ENTER key again.

Command Notes
1.) set title “Graph of the Sine Function” Writes the title Graph of the Sine Function at the top center of the graph. Try typing replot in the GNUPlot prompt and press the ENTER key to see the changes in the graph.
2.) set  x label “x” Labels the x-axis “x”
3.) set ylabel “y” Labels the y-axis “y”
4.) set zeroaxis Shows the x and y axis
5.) set xrange [-7:7] Sets the Cartesian plane x-axis from -7 to 7.
6.) set yrange [-2:2] Sets the Cartesian plane y-axis from -2 to 2.
7.) set xtics (-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7) Set the intervals of the x-axis to 1 and label the intervals. We chose from -7 to 7 because this is the range of our x-axis.
8.) set ytics (-2,-1, ,0,1,2) Set the intervals of the y-axis to 1 and label the intervals. We chose from -2 to 2 because this is the range of our y-axis.

And we are done. To view your graph, type replot and then press the ENTER key. Your graph should look like Figure 4.

GNUPlot can, of course, plot graphs that are a lot more complicated than what we have done above. It is capable of 3-dimensional graphs such as the one shown below.

We are going to discuss them in the future GNUPlot tutorials, so keep posted.

Plotting with Ivan Johansen’s Graph Software

Graph, a graphing software created by Ivan Johansen, was the first graphing software I have learned to use. Although the graphics quality is not that impressive, it has some features that are not available in other graphing software, the most notable of which is the polynomial of best-fit.

Figure 1 – The Graph Window.

The tutorial below teaches the basics of Graph, and most examples are related to elementary and high school mathematics.

The basic capabilities of Graph are enumerated below.

I. Graph Functions

As an example, we will plot the graph of the function f(x) = x2 + 3x from x = -3 to x = 2, the graph of which is shown in Figure 2.

  • To graph a our function, just click the Function menu from the menu bar, then click Insert function, then type the equation of your function. The enumerated steps below are associated with the numbers shown in Figure 2.
  1. Choose Standard from the Function type drop-down list box.  The other options are Parametric and Polar.
  2. Type the equation of our function f(x) = = x^2 + 3x in the Function equation text box. Like other software, Graph uses ^ to denote exponential notation.
  3. Specify the domain of the function which is from -3 to 2.
  4. Choose the type of start and endpoint at the Endpoints drop-down list boxes. We choose circle for our left end point and arrow for our right endpoint.
  5. Change the color, line style, draw type, and width of the graph. Click the OK button when you are done.

Figure 2 – The Function dialog box and the graph of the function f(x) = x2 + 3x.

Exercise: Refer to the steps above in graphing the following functions:

Standard Functions

  1. f(x) = x^3 + 3x – 1
  2. f(x) = sin(x)
  3. f(x) = sqrt(x)
  4. x(t) = cos(t), y(t) = tan(t)
  5. e^(sin(t)) – 2cos(4t) + sin((t – pi/2) /12)^5

Note: a, b and c are standard functions; d is parametric and e is polar. Choose their appropriate function type the Function type box before typing the equations.

II. Graph Inequalities

To get the intersection of the graph y < x^3 + 3 and y > 2x, we first transform the inequality to equation, and choose the shaded portion later. This is the part of Graph that I do not quite like. It’s more like manual drawing rather than graphing.

  1. Click the Function menu from the menu bar, the click Insert Function.
  2. Type x^3 + 1 in Function equation box.  
  3. To graph y > 2x, repeat step a and type y = 2x in the Function equation box.
  4. To shade the graph below x3 + 3, be sure that the equation of the function is selected in the equation window (left pane). Click Function from the menu bar and then click Insert shading… from the list.
  5. Choose Below function icon (see Figure 3). Take note of the other options. Click the OK button.
  6. As an exercise, shade y > 2x.

Figure 3 – The Insert Shading dialog box.

III. Plot Points Series and Determine Line (or Polynomial) of Best Fit

To plot a point series, click the Function menu from the menu bar, click Insert point series. Type the ordered pairs on the Insert point series window as shown below.

Figure 4 – The Insert Points series dialog box.

To insert a line of best fit, click the Function menu from the menu bar, then click Insert trendline….  In the Insert trendline window, choose Linear. Notice that you can also choose polynomial of a chosen order fit.

Figure 5 – The Trendline dialog box.

Change the Line width to 3 and click the OK button. The line of best fit of our point series is shown below. The line of best fit graph is shown in Figure 6.

Figure 6 – The Line of Best Fit of the given data in Figure 4.

IV. Find the area under a curve

Graph is capable of finding the area under a curve or technically, perform definite integration. In the example below, we will find the area under the curve of y = sin(x) from -2 to 3.

  1. To plot y = sin(x), click the Function menu from the menu bar and click Insert function from the drop-down list box.
  2. Type sin(x) in the Function equation box.
  3. To get the area of the curve under -2 through 3, click the Calc menu from the menu bar, then click Area from the list.
  4. A dialog box will appear located at the bottom-left of the Graph window. Type -2 in the From text box and type 3 in the To text box. Notice that the area the curve is displayed on the Area box below the To text box.

V. Generate a table from a graph

Graph is capable of generating table from a graph. If we want to generate table of values of the graph y = sin(x) in (4), be sure that the graph is selected in the left pane of the Graph window, then do the following steps:

  1. Click the Calc menu and then click Table.
  2. Type the minimum value, say -10, in the From text box and the maximum value, say 10, in the To text box.
  3. In the \delta x text box, type the interval, say 2, of your table, the click the Calc button.

Figure 7 – The data generated from graph y = sin(x).

Notice that not only the x and f(x) are displayed but also the value of the first and second derivatives.

Related Posts Plugin for WordPress, Blogger...