Time for action - testing with peaks
1. You can enter the interactive environment by typing
octave
in your shell under GNU/Linux, or by double-clicking the Octave icon in Windows. You should now see the Octave prompt:
octave:1>
2. You have already learned how to exit the interactive environment. Simply give Octave the command
exit
or press Ctrl + D, but we do not want to exit just yet!3. At the prompt, type as follows:
octave:1> surf(peaks)
4. You should now see a three-dimensional surface plot as depicted on the left-hand side figure shown next. If not, your installation has not been successful. Now, put your mouse pointer over the figure window, hold the left mouse button down, and move the pointer. If the plotting program supports it, the figure should now rotate in the direction you move the pointer. If you click on the figure window using mouse button three (or the scroll wheel) you can zoom by moving the pointer side to side or up and down.
5. Let us try a contour plot. Type as follows:
octave:2> contourf(peaks)
6. Does it look like the following figure on the right? If not, it can be because you are using Octave version 3.2.4 and have the package oct2mat loaded. Try typing
octave:3> pkg unload oct2mat
7. Now retype the previous command.
8. Click somewhere on the window of the right-hand side figure with button three. A cross and two numbers appear in the window if you are using gnuplot with Octave. The cross is just the point where you clicked. The two numbers show the x axis and y axis values.
Note
Octave can use different plotting program, for example, gnuplot or its own native plotting program. Therefore, your figures may look a bit different, depending on that program.
What just happened?
The figure to the left shows a graph of a mathematical function, which is a scalar function of two variables y and x given by:
The function value (range) is evaluated in Octave by the command peaks
, which is the nick name for the function f. The graph is then plotted using the Octave function surf
. As a default, Octave calculates the range of f using 50 x and y values in the interval [ 3; 3]. As you might have guessed already, the contourf
Octave function plots the contours of f. Later we will learn how to label the axis, add text to the figures, and much more.
Did you notice the phrase "Octave function" previously? An Octave function is not necessarily a mathematical function as Equation (1.1), but can perform many different types of operations and have many different functionalities. Do not worry about this for now. We will discuss Octave functions later in the book.
Notice that the interpreter keeps track of the number of commands that have been entered and shows this at the prompt.