-
Notifications
You must be signed in to change notification settings - Fork 1
/
trigbook-appendixb.tex
174 lines (156 loc) · 7.99 KB
/
trigbook-appendixb.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
\addchap[Appendix B:$\quad$Graphing with Gnuplot]{Appendix B}
\textsf{\textbf{\Large Graphing with Gnuplot}}\vspace{4mm}
Gnuplot is a free, open-source software package for producing a variety of graphs. Versions are
available for many operating systems. Below is a very brief tutorial on how to use Gnuplot to graph
trigonometric functions.\vspace{4mm}
\par\noindent\textbf{\textsf{INSTALLATION}}
\begin{enumerate}
\item Go to \url{http://www.gnuplot.info/download.html} and follow the links to download the latest
version for your operating system. For Windows, you should download the setup file with a name
such as \path{gp460-win32-setup.exe}, which is
version 4.6.0. All the examples discussed here will assume at least version 4.6.0, though they
should work with earlier 4.x versions.
\item Install the downloaded file. For example, in Windows you would run the setup file you
downloaded in Step 1, which installs Gnuplot in the
\texttt{C:\symbol{92}Program Files\symbol{92}gnuplot} folder by default.
You can accept the defaults during installation, though you
should select the ``Create a desktop icon'' option in the \textbf{Select Additional Tasks}
screen.
\item (Optional) Read the documentation at \url{http://gnuplot.info/documentation.html}.
\end{enumerate}
\par\noindent\textbf{\textsf{RUNNING GNUPLOT}}
\begin{enumerate}
\item In Windows, run \textbf{\path{wgnuplot.exe}} from the \path{bin} folder where you
installed Gnuplot (the default location is
\texttt{C:\symbol{92}Program Files\symbol{92}gnuplot\symbol{92}bin\symbol{92}wgnuplot.exe}),
or double-click the desktop icon if you selected that option during the installation.
In Linux, just type \textbf{\path{gnuplot}} in a terminal window.
\item You should now get a Gnuplot terminal with a \textbf{\path{gnuplot>}} command prompt. In
Windows this will appear in a new window, as shown in the picture on the next page.
In Linux it will appear in the terminal window
where the \textbf{\path{gnuplot}} command was
run. For Windows, if the font is unreadable you can change it by right-clicking on the text part
of the Gnuplot window
and selecting the ``Choose Font..'' option. For example, the font ``Courier'', style ``Regular'',
size ``12'' is usually a
good choice (that choice can be saved for future sessions by right-clicking in the Gnuplot window
again and selecting the option to update wgnuplot.ini).
\item At the \textbf{\path{gnuplot>}} command prompt you can now run graphing commands, which we
will now describe.
\end{enumerate}
\newpage
\begin{figure}[h]
\begin{center}
\includegraphics{wgnuplot.eps}
\end{center}
\end{figure}
\par\noindent\textbf{\textsf{GRAPHING FUNCTIONS}}\vspace{2mm}\\
The usual way to create graphs in Gnuplot is with the \textbf{\path{plot}} command:
\begin{displaymath}
\texttt{plot \emph{<range> <comma-separated list of functions>}}
\end{displaymath}
For a function $y=f(x)$, \texttt{\emph{<range>}} is the range of $x$ values (and optionally the
range of $y$ values) over which to plot. To specify an $x$ range, use an expression of the form
\symbol{91}$a:b$\symbol{93}, for some numbers $a<b$. This will cause the graph to
be plotted for $a\le x\le b$.\vspace{2mm}
To specify an $x$ range and a $y$ range, use an expression of the form
\symbol{91}$a:b$\symbol{93}\symbol{91}$c:d$\symbol{93}, for some numbers $a<b$ and $c<d$. This will
cause the graph to be plotted for $a\le x\le b$ and $c\le y \le d$.\vspace{2mm}
\par\noindent Function definitions use the $x$ variable in combination with mathematical operators,
listed below:\vspace{1mm}
\begin{center}
\begin{tabular}{@{} | c | c | c | c | @{}}
\hline \textbf{Symbol} & \textbf{Operation} & \textbf{Example} & \textbf{Result}\\
\hline $+$ & Addition & $2 + 3$ & $5$\\
\hline $-$ & Subtraction & $3 - 2$ & $1$\\
\hline * & Multiplication & $2$*$3$ & $6$\\
\hline $/$ & Division & $4/2$ & $2$\\
\hline ** & Power & $2$**$3$ & $2^3 = 8$\\
\hline exp($x$) & $e^x$ & exp($2$) & $e^2$\\
\hline log($x$) & $\ln x$ & log($2$) & $\ln 2$\\
\hline sin($x$) & $\sin x$ & sin(pi/$2$) & $1$\\
\hline cos($x$) & $\cos x$ & cos(pi) & $-1$\\
\hline tan($x$) & $\tan x$ & tan(pi/$4$) & $1$\\\hline
\end{tabular}\end{center}
Note that we use the special keyword ``pi'' to denote the value of $\pi$.
\vspace{2mm}
\hrule width \textwidth height 0.5pt\vspace{2mm}
\par\noindent \emph{\textbf{Example B.1.}}
To graph the function $y=\sin\;x$ from $x=0$ to $x=2\pi$, type this at the
\textbf{\path{gnuplot>}} prompt:
\begin{displaymath}
\texttt{plot \symbol{91}0:2*pi\symbol{93} sin(x)}
\end{displaymath}
The result is shown below:
\begin{figure}[h]
\begin{center}
\input{sine.tex}
\end{center}
\end{figure}
Notice that the $x$-axis is labeled with integers. To get the $x$-axis labels with fractions of
$\pi$, you need to modify the \texttt{terminal} setting. In Windows, you would do this:
\begin{displaymath}
\texttt{set terminal windows enhanced}
\end{displaymath}
In Linux you would do this:
\begin{displaymath}
\texttt{set terminal wxt enhanced}
\end{displaymath}
You can then (provided the Symbol font is installed, which it usually is) set the $x$-axis to have
multiples of $\pi/2$ from $0$ to $2\pi$ as labels with this command (all on one line):
\begin{gather*}
\texttt{set xtics ('0' 0,'\{/Symbol p\}/2' pi/2,'\{/Symbol p\}' pi,'3\{/Symbol p\}/2' 3*pi/2,}\\
\texttt{'2\{/Symbol p\}' 2*pi)}
\end{gather*}
In the above example,
to also plot the function $y=\cos\;2x + \sin\;3x$ on the same graph, put a comma after the first
function then append the new function:
\begin{displaymath}
\texttt{plot \symbol{91}0:2*pi\symbol{93} sin(x), cos(2*x) + sin(3*x)}
\end{displaymath}
By default, the $x$-axis is not shown in the graph. To display it, use this command
\emph{before} the \textbf{\path{plot}} command:
\begin{displaymath}
\texttt{set zeroaxis}
\end{displaymath}
Also, to label the axes, use these commands:
\begin{gather*}
\texttt{set xlabel "x"}\\\texttt{set ylabel "y"}
\end{gather*}
The default sample size for plots is $100$ units, which can result in jagged edges if the curve
is complicated. To get a smoother curve, increase the
sample size (to, say, $1000$) like this:
\begin{displaymath}
\texttt{set samples 1000}
\end{displaymath}
Putting all this together, we get the following graph:
\begin{figure}[H]
\begin{center}
\input{tutorial.tex}
\end{center}
\end{figure}\vspace{-4mm}
\lineacross
\vspace{3mm}
\par\noindent\textbf{\textsf{PRINTING AND SAVING}}\vspace{2mm}\\
In Windows, if you are using the \texttt{windows enhanced} terminal then to print a graph from
Gnuplot click on the printer icon in the menubar of the graph's window. If you are using the
default \texttt{wxt} terminal then select \textbf{Print} near the top of the main Gnuplot window
and enter \path{png} in the \emph{Terminal type?} textfield, then hit OK to get the Print Setup
dialog.
In Windows, to save a graph, say, as a PNG file, go to the File menu on the main Gnuplot menubar,
select ``Output Device ...'', and enter \path{png} in the \emph{Terminal type?} textfield, hit OK. Then, in the
File menu again, select the ``Output ...'' option and enter a filename (say, graph.png) in the \emph{Output
filename?} textfield, hit OK. Now run your plot command again and the file will be saved in the
current directory, usually in your \texttt{My Documents} folder (it can also be found by
selecting the ``show Current Directory'' option in the File menu).\\
\par\noindent In Linux, to save the graph as a file called graph.png run the following
commands:\vspace{2mm}
\begin{tabular}{l @{}}
\texttt{set terminal png}\\
\texttt{set output 'graph.png'}
\end{tabular}\vspace{2mm}
\par\noindent and then run your plot command. There are many terminal types (which determine the output format). Run
the command \textbf{\texttt{set terminal}} to see all the possible types. In Linux, the \textbf{postscript} terminal type is
popular, since the print quality is high and there are many PostScript viewers available.
\vspace{2mm}
\par\noindent To quit Gnuplot, type \textbf{\path{quit}} at the \textbf{\path{gnuplot>}} command prompt.