MAS116/MAS117 PRESENTATION LAB 4

IMPORTANT! If you are asked to install packages when processing your file, click ‘yes’ then wait. You may need to do this for multiple packages. At points it may look as though nothing is happening but your PDF should appear eventually. Interrupting the process can cause problems, so make sure you wait long enough!

Open TeXworks or TeXmaker or whichever editor you prefer. Find the preamble template on the course webpage in the ‘Extras’ section, and copy and paste this to start this week’s document.

If your computer is unsure what to do with .tex files like the template on the course webpage, you can open it in any text editor (e.g. Notepad).

1. PGFplots

Let’s start by creating some graphs using pgfplots. You need to ensure that \usepackage{pgfplots} is in your preamble; you will need to delete the comment symbol, % from the template. Start a new section called ‘Graphs created with PGFplots’ then enter the following.

    \begin{tikzpicture}
        \begin{axis}[
                title={$y=\sin x$},
                xlabel=$x$, ylabel=$y$]
            \addplot[smooth, domain=-360:360]{sin(x)};
        \end{axis}
    \end{tikzpicture}

If you get errors here, check that you have recreated the text precisely, including commas and the semicolon.

(1)
See what happens to the axes when you add
    axis lines=center,
below the line title={$y = \sin x$},
(2)
Graphs won’t be centered on the page by default. To change this, put the whole tikzpicture environment inside a center environment. Note the US spelling of ‘center’.
(3)
Try changing sin(x) to sin(deg(x)) and the domain to -2*pi:2*pi. What’s happened now? PGFplots’s trigonometric functions work in degrees: the deg(x) function takes a number (in radians) and turns it into the value in degrees; that is, deg(x) = 180x π .

Try a 3D plot, using the following code.

    \begin{tikzpicture}
        \begin{axis}[xlabel=$x$, ylabel=$y$]
            \addplot3[domain=-1.5:1.5, surf]{-exp(-x^2 - y^2)};
        \end{axis}
    \end{tikzpicture}

(4)
What was the main difference in the code here compared to before?
(5)
What does the surf option do? (Try taking it out.)
(6)
What does putting colormap/blackwhite after ylabel=$y$ do? (Note the US spelling of the word “color”.)

There are many more examples of graphs – with source code – on the PGFplots website, http://pgfplots.sourceforge.net/gallery.html. If you need to make a graph, a combination of the simple techniques on this sheet and what you find at the link above should do the trick.

2. Including image files

Now let’s try including an image from external software. Open a new tab in your browser and go to https://www.geogebra.org/geometry. Geogebra is a point-and-click geometry package which can do more than just create pictures for including in documents.

(7)
To start, make sure the axes are showing on the image: go to the settings cog in the top-right, and click on “Show Axes” if necessary.
(8)
Click on the calculator symbol at the top of the bar on the left, then the plus sign, and add an expression. Type x2 + y2 = 9 into the bar and press enter. A circle should appear.
(9)
Find the button to add a new point, and put it at (5,0).
(10)
Find the ‘Tangents’ button, and follow the instructions to plot the two tangent lines to x2 + y2 = 9 which pass through (5,0). Click on the calculator symbol to see the equations of these two lines.
(11)
Use the ‘Move’ button to drag your point around and see the tangents move.

Play around further if you like. Once you have a picture you are happy with, go to the options in the top-left, click Download As, then choose PDF as the file type and save the image as geogebra_image.pdf in the same folder as your LaTeX document.

Return to your LaTeX document. Put \usepackage{graphicx} in your preamble. Start a new section called ‘Including image files’ and put the command \includegraphics{geogebra_image.pdf} underneath. Your picture should appear though it may not fit properly at this stage. Convince yourself the picture stays smooth no matter how far you zoom in.

If you have problems, check that the name of the uploaded file is exactly the same in the includegraphics command as it is in the filename, and make sure the file is saved in the same folder as the .tex file.

(12)
Now add an optional parameter to the \includegraphics command by changing \includegraphics to \includegraphics[scale=0.5]. What has changed?
(13)
Try \includegraphics[width=8cm].
(14)
Try \includegraphics[width=\textwidth].
(15)
Try \includegraphics[width=0.8\textwidth].
(16)
Put the image inside a center environment.

Note that this might have resulted in an image where the labels are unreadable! You should always ensure that labels are legible. In this case you would have to go to geogebra and either alter the font size or else the range of the image that is exported. If you have lots of time in the lab, you should try to see how you can alter the font size in geogebra.

3. Figures and floats

−−001−−001xy( 1 0.5 1 0.5si..n55(2x),cos(3x))

Figure 1: An example figure that can float around.

To enable images to be referenced, they should be placed inside the figure environment; this will allow you to refer to Figure 1, for instance. Try this instead of using center, so that your LaTeX reads

  \begin{figure}[tbh]
      \includegraphics[width=0.8\textwidth]{geogebra_image.pdf}
      \caption{A Geogebra picture}
  \end{figure}

LaTeX will move figure environments around the document depending on the size of the figure and the space available. Environments like this are called ‘floats’ as they can float around the document. If you are looking this in a PDF document, you will see Figure 1 has ‘floated’ to the top of the page. You might find this behaviour annoying to start with, but it is the standard way to do things in professional documents.

The term [tbh] after \begin{figure} is telling LaTeX to put the figure at the top of a page if there’s room, and if not try the the bottom of the page and then here, i.e., at the position it appears in the LaTeX document. Don’t fight this! Let LaTeX put floats where it thinks is sensible.

Now add \label{fig:geogebra} after the \caption{...} command, then write the following sentence underneath the figure environment

  Geogebra creates good diagrams; see Figure~\ref{fig:geogebra}.

Process your file to see the figure being referenced automatically.

The tilde, ~, puts in a space, but does not allow it to be split across lines. You should do this when using \ref as “Figure 1” looks good but “Figure 1” does not.

Homework

Create a document with title ‘MAS116: Homework 4’ (or MAS117) and your name on as author. Your task this week is to look up Lagrange Interpolation and to write a page or so of LaTeX explaining it. In particular, given points (xi,yi) 2 for i = 0,1,2, explain why the function f defined below is a quadratic polynomial whose graph passes through the points (x0,y0), (x1,y1) and (x2,y2):

f(x) := (x x1)(x x2) (x0 x1)(x0 x2)y0 + (x x0)(x x2) (x1 x0)(x1 x2)y1 + (x x0)(x x1) (x2 x0)(x2 x1)y2.

You could look on the internet, or in search the University library’s digital collection. For instance, the theory is covered in the book by Stroud listed below which is available to view online there.

Note that you must write this in your own words, you must not copy directly from any other source.

Things that may help you with your write-up include the following.

Print out and hand in the PDF and LaTeX file of your homework at next week’s lab, as usual. Remember that this is assessed coursework so must be your own work; colluding with others is cheating and may make you subject to disciplinary proceedings.

References.