% [INTRO_OCTAVE_07.tex] % Graphing %%%%%%%%%%%%%%%%%%%% INPUT OF *.MAC FILES %%%%%%%%%%%%%%%%%%% % CONTENTS OF THE THREE *.MAC FILES % [format.mac] shortcuts for horizontal and vertical spacings (\th,\mni), % [fonts.mac] fonts% % [macros.mac] boxes, time stamp, fractions \input fonts.mac \input format.mac \input macros.mac \input pstricks % need for centring \input epsf % need for importing eps5.94 in \topglue 0pt % p. 156 \topskip -10truept % p. 141 % this works for my famille \nopagenumbers \pageno = 17 % \headline = {\centerline {\running \ifnum \pageno > 17 Section 07: Graphing \fi} } % end headline % the foot line appears on the first page of the section \footline = {\centerline {\ssi \folio } } % end footer % \vsize = 8.50 in {\hsize = 5.94 in % FOR 8.5 X 11 %\vsize = 21.6 truecm = 8.50 in %{\hsize = 15.1 truecm = 5.94 in \voffset = 1.0truecm % linux \hoffset = 0.70truecm % linux \topskip = 20 pt \baselineskip = 14pt % default = 12pt %%%%%%%%%%%%%%%%%%%%%%%% SOF %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% {\rm % %%Type . %\phantom{header} \centerline{ \bfsixteen Section 07: Graphing} % title of section % \vskip 12pt\nin % % % %%%%%%%%%%%%% Octave uses Gnuplot to do the graphing. One could learn how to use Gnuplot directly, but it is better to learn Octave and let it do the work. \mni The following example, in which we plot y = sin(x) and y = cos(x) on the interval [0\gl,\gl $\pi$]\gl, shows all the basic steps involved in plotting, labelling, and printing. There are many options, e.g.\ using dashed lines, label fonts and sizes, but one should first learn the basics. You should start with one function, then add a second function, then labels etc. You can play with line widths etc.\ to suit your taste. \mni % % this is the WIDTH of the imported graph, \centerline{\epsfxsize = 5.5 truein\epsfbox{graph1_fontweight-bold_fontsize-40.eps}} % % this is revised with 'fontweight-fontsize \bni {\tt % for programs \obeylines \parindent = 0pt \parskip = -1.0 pt %was-2.5pt THIS LEAVES MORE SPACE \%\qq first clear all preceeding graphs octave:1> {\bf clf} \% = ``clear function'' \% \%\qq tell Octave the range of values of interest \%\qq the sine and cosine go from -1 to 1, but the graph \%\qq will be nicer if we go slightly below and above \% \%\qq draw the {\it Octave axes\/}, which show the values octave:2> {\bf axis(}[0 pi -1.2 1.2]{\bf)} \% \%\qq keep the Octave axes and all succeeding plots octave:3> {\bf hold} \% \vfill\eject \%\qq Draw the {\it real\/} x-axis from (0,0) to (0,pi) \%\qq a\th=\th [0 pi] gives the x coordinates of the x-axis \%\qq b\th=\th [0 0] gives the y coordinates of the x-axis \% octave:4> a = [0 pi]; \% {\it NOT\/} {\rm [0\th{\bf:}\th .001\th{\bf:}\th pi]} octave:5> b = [0 0]; % \%\hskip 4.0truecm{\it FIRST}{\bf ,} {\it SECOND} \%\qq {\it ALWAYS\/}: {\bf plot(}\th {\it x-values\/}{\bf ,} {\it y-values}{\bf )} \%\qq increase the line thickness, by writing {\bf 'linewidth', 3} % yes points; see the final drawing, looks thick on screen octave:6> {\bf plot(}a,\th b\th ,\th 'linewidth',\th 3{\bf)} \% \%\qq now we are ready to plot the two graphs \%\qq give the x-values for the plot; use increments of .005 octave:6> x = [0\th :\th {\bf .005}\th :\th pi];\qq \% could use .001 \% \%\qq first graph y = sin(x); use y1 for the name of the vector octave:7> y1 = sin(x); \% \%\qq {\it we plot the y-values ({\tt y1}) against the x-values ({\tt x})} \%\qq {\it from now on graphs are thicker using {\bf 'linewidth', 5}} octave:8> {\bf plot(}x,\th y1,\th 'linewidth',\th 5{\bf)} \% \sni \% the same font descriptions were used) \%\qq second graph y = cos(x); use y2 for the name of the vector octave:9> y2 = cos(x); octave:10> {\bf plot(}x,\th y2,\th 'linewidth',\th 5{\bf )} \% \%\qq now add a title \%\qq the text is enclosed in apostrophes, {\it not\/} quotation marks \%\qq {\it from now on we use:\/} {\bf 'fontweight',\th "bold", 'fontsize',\th 20} % [20 points, 1cm $\approx$ 28 pts] octave:11> {\bf title('}sin(x)\th and\th cos(x){\bf '},\th'fontweight',\th "bold", \qq\q 'fontsize',\th 20{\bf )}\qq \% if the line is too long, push return \% \%\qq labels for the x and y axes, note ``xlabel'', not ``x-label'' octave:12> {\bf xlabel('}domain = [0\th ,\th pi]{\bf')} octave:13> {\bf ylabel('}range = [-1\th ,\th 1]{\bf ')} \% \%\qq include a text to indicate the point of intersection \%\qq the text is placed at (.9\th ,\th .7) [obtained by trial and error] octave:14> {\bf text(}.9\th ,\th .7, 'sin(pi/4) = cos(pi/4){\bf ')} \% \%\qq save the graph in three formats: \%\qq 1.\en {\it png\/}:\th image, better than jpg \%\qq 2.\en {\it pdf\/}:\th for immediate printing \%\qq 3.\en {\it eps\/}:\th encapsulated postscript;\th for importing into a Latex file % \vfill\eject % \nin \%\qq specify the full name of the output file octave:15> {\bf print -dpng} graph1.png octave:16> {\bf print -dpdf} graph1.pdf octave:17> {\bf print -deps} graph1.eps % \%\qq check that the graphs are there using {\bf ls}\th =\th ``list'' octave:18> ls *.png \qq\q {\it graph1.png\/}\qq \% yes, its there! } % end \tt % for programs % \mni % Above we plotted a continuous function, but the same holds for discrete data. The one difference is that to indicate that we just want to plot the points using a pentagon ("p") of large size 12 we add {\bf "p", "markersize",12} to the plot command. % Octave accepts 'p', markersize [Other options are "o"\en,\en "+"\en,\en "x"\en,\en "*", "h" (hexagon), "s" (square),\vbox to 11.5truept {}"\gl{\bfsixteen \^{}}\gl" (triangle).] \sni To illustrate this we use the birthday probabilities from section 06A: \mni {\tt % for programs \obeylines \parindent = 0pt \parskip = -1.0 pt %was-2.5pt THIS LEAVES MORE SPACE octave:19> indices = [10\th:\th10\th:\th 60]; octave:20> birthday\_\gl prob\th=\th [0.117\th 0.411\th 0.706\th 0.891\th 0.970\th 0.994]; octave:21> plot(indices, birthday\_\gl prob,"p","markersize",12) } % end \tt % for programs % \mni % revised 2014.08.20 % the data and commands are all in % [birthday_data_for_plotting.txt] % this is the WIDTH of the imported graph, \centerline{\epsfxsize = 4.5 truein\epsfbox{ birthday_not_connected_with_axes_linewidth-15_markersize-12_[unknown]-fontweight-bold_fontsize-40.eps }} % trying to make bigger \mni % If in addition to placing the pentagons at the data points we wanted to connect them by a thin dashed line, all we would have to do is {\bf hold} the graph and then do a new plot which does not have the {\bf "p"} and {\bf ,"markersize",12} commands. We can make the line alternate between a dash and a dot by using the command {\bf 'linestyle, '-.'}. [Other options are '-\gl-'\en,\en ':'\en,\en '-' (solid).] Lines and curves can also be coloured. % \mni % {\tt % for programs \obeylines \parindent = 0pt \parskip = -1.0 pt %was-2.5pt THIS LEAVES MORE SPACE octave:22> {\bf hold} octave:23> plot(indices\th,\th birthday\_\gl prob\th,\th {\bf ''linestyle"\th,\th'-.'}) } % \mni % % this is the WIDTH of the imported graph, \centerline{\epsfxsize = 4.5 truein\epsfbox{ birthday_connected_with_axes_linewidth-15_markersize-12_[unknown]-fontweight-bold_fontsize-40.eps }} % \mni % \bni \centerline{\bf Try these} % \mni \item{1.} Plot the graphs of $y =x^2,\en y=\sqrt{x},\en y =x$ on the same axes. Let $x$ vary between -0.5 and 2.5. \sni \item{}Print the line $y = x$ with dash marks using the command: \pni \item{}Plot(x,y, {\bf 'linestyle', '-\gl-'}) [note the apostrophes and the comma between the last two commands.] \sni \item{}Print the other two curves with a thicker line using the command: \pni \item{} plot(x, y, {\bf 'linewidth', 8}) [note the apostrophes and the comma between the last two commands.] \sni \item{} Put the labels ``x-axis'', ``y-axis''. The two functions are inverse functions of one another and are symmetric about the line $y = x$. Give the title ``Inverse functions'' to the graph. Label the points of intersection of the two graphs. \mni \item{2.} Consider an isosceles triangle with angles A, B, A (in degrees) and sides a, b, a. \sni \item{i.} Create a function that determines the ratio ${b\over a}$ as a function of B. [suggestion: use the law of sines; you have to convert from degrees to radians.] \sni \item{ii.} Plot the ratio ${b\over a}$ as a function of B, as B varies from 1$^{\hbox{\rmten o}}$ to \vtop to 6pt{}179$^{\hbox{\rmten o}}$\gl. Check the answers at 1$^{\hbox{\rmten o}}$ and 179$^{\hbox{\rmten o}}$ by making a sketch. Why were 0$^{\hbox{\rmten o}}$ and 180$^{\hbox{\rmten o}}$ not included? \sni \item{iii.} Above we used regular pentagons to mark the points. Use (i) to find the ratio of the diagonal of a regular pentagon to its side. [answer: see {\tt G} in section 08.] % % %%%%%%%%%%%%%%%%%%%%% KEEP THIS ENDING!!! % %%%%%%%%%%%%%%%%%%%%% \vfill\eject %% } % ends \rm \par} % ends \hsize \bye \mni {\tt % for programs \obeylines \parindent = 0pt \parskip = -1.0 pt %was-2.5pt THIS LEAVES MORE SPACE } % end \tt % for programs \mni \mni {\tt % for programs \obeylines \parindent = 0pt \parskip = -1.0 pt %was-2.5pt THIS LEAVES MORE SPACE } % end \tt % for programs \mni