Graphing in L A TEX using PGF and TikZ



Similar documents
PROBLEM SET. Practice Problems for Exam #1. Math 1352, Fall Oct. 1, 2004 ANSWERS

Getting to know your TI-83

SAT Subject Test Practice Test II: Math Level II Time 60 minutes, 50 Questions

Pre-Calculus Graphing Calculator Handbook

Lecture 8 : Coordinate Geometry. The coordinate plane The points on a line can be referenced if we choose an origin and a unit of 20

50 TUGboat, Volume 31 (2010), No. 1. Plotting experimental data using pgfplots Joseph Wright

Math 1B, lecture 5: area and volume

Unit 7 Quadratic Relations of the Form y = ax 2 + bx + c

Formulas, Functions and Charts

TWO-DIMENSIONAL TRANSFORMATION

x 2 + y 2 = 1 y 1 = x 2 + 2x y = x 2 + 2x + 1

MA107 Precalculus Algebra Exam 2 Review Solutions

The University of the State of New York REGENTS HIGH SCHOOL EXAMINATION MATHEMATICS B. Thursday, January 29, :15 a.m. to 12:15 p.m.

Graphics with TikZ. 1 Introduction. Andrew Mertz and William Slough

1.2 GRAPHS OF EQUATIONS. Copyright Cengage Learning. All rights reserved.

Quickstart for Desktop Version

Scientific Graphing in Excel 2010

Introductory Handbook for the TI-89 Titanium

6 EXTENDING ALGEBRA. 6.0 Introduction. 6.1 The cubic equation. Objectives

AP Calculus AB 2010 Free-Response Questions Form B

Grade level: secondary Subject: mathematics Time required: 45 to 90 minutes

Volumes of Revolution

Area and Arc Length in Polar Coordinates

Microsoft Excel 2010 Charts and Graphs

Solutions for Review Problems

FRICTION, WORK, AND THE INCLINED PLANE

PRACTICE FINAL. Problem 1. Find the dimensions of the isosceles triangle with largest area that can be inscribed in a circle of radius 10cm.

CIRCLE COORDINATE GEOMETRY

Review of Fundamental Mathematics

1.7 Graphs of Functions

Each function call carries out a single task associated with drawing the graph.

Visualizing Differential Equations Slope Fields. by Lin McMullin

A very minimal introduction to TikZ

MULTIPLE INTEGRALS. h 2 (y) are continuous functions on [c, d] and let f(x, y) be a function defined on R. Then

MARS STUDENT IMAGING PROJECT

4. How many integers between 2004 and 4002 are perfect squares?

Analyzing Piecewise Functions

Excel Basics By Tom Peters & Laura Spielman

Information Literacy Program

Trigonometry Review with the Unit Circle: All the trig. you ll ever need to know in Calculus

Evaluating trigonometric functions

Excel -- Creating Charts

Create Charts in Excel

2Digital tablets or computer scanners can

MatLab Basics. Now, press return to see what Matlab has stored as your variable x. You should see:

Academic Support Center. Using the TI-83/84+ Graphing Calculator PART II

GeoGebra. 10 lessons. Gerrit Stols

9.4. The Scalar Product. Introduction. Prerequisites. Learning Style. Learning Outcomes

The following is an overview of lessons included in the tutorial.

Double Integrals in Polar Coordinates

Additional Topics in Math

Solutions to Homework 10

Graphic Designing with Transformed Functions

Petrel TIPS&TRICKS from SCM

Math 241, Exam 1 Information.

4 More Applications of Definite Integrals: Volumes, arclength and other matters

2.5 Transformations of Functions

Tutorial 2: Using Excel in Data Analysis

F.IF.7b: Graph Root, Piecewise, Step, & Absolute Value Functions

CHAPTER 8, GEOMETRY. 4. A circular cylinder has a circumference of 33 in. Use 22 as the approximate value of π and find the radius of this cylinder.

AP CALCULUS AB 2008 SCORING GUIDELINES

Chapter 6: Constructing and Interpreting Graphic Displays of Behavioral Data

Gerrit Stols

Angles and Quadrants. Angle Relationships and Degree Measurement. Chapter 7: Trigonometry

Student Performance Q&A:

Solutions to old Exam 1 problems

SDC. Schroff Development Corporation PUBLICATIONS. MultiMedia CD by Jack Zecher

Geometry and Measurement

The Center for Teaching, Learning, & Technology

KaleidaGraph Quick Start Guide

The Circumference Function

Solutions to Homework 5

AP Physics 1 Summer Assignment

Plots, Curve-Fitting, and Data Modeling in Microsoft Excel

How To Use The Ti-83+/84+ Calculator For Math 111

MATLAB Workshop 14 - Plotting Data in MATLAB

0 Introduction to Data Analysis Using an Excel Spreadsheet

Experiment 9. The Pendulum

Intro to Excel spreadsheets

Figure 1. An embedded chart on a worksheet.

2nd Semester Geometry Final Exam Review

Solving Simultaneous Equations and Matrices

HSPA 10 CSI Investigation Height and Foot Length: An Exercise in Graphing

Math Tools Cell Phone Plans

Calculus 1st Semester Final Review

6. The given function is only drawn for x > 0. Complete the function for x < 0 with the following conditions:

1. Kyle stacks 30 sheets of paper as shown to the right. Each sheet weighs about 5 g. How can you find the weight of the whole stack?

Fundamental Theorems of Vector Calculus

Graphing Quadratic Functions

G r a d e 1 0 I n t r o d u c t i o n t o A p p l i e d a n d P r e - C a l c u l u s M a t h e m a t i c s ( 2 0 S ) Final Practice Exam

Definition: A vector is a directed line segment that has and. Each vector has an initial point and a terminal point.

The small increase in x is. and the corresponding increase in y is. Therefore

Inv 1 5. Draw 2 different shapes, each with an area of 15 square units and perimeter of 16 units.

Area of Parallelograms (pages )

Chapter 4 Creating Charts and Graphs

SAS/GRAPH 9.2 ODS Graphics Editor. User s Guide

MATLAB Workshop 3 - Vectors in MATLAB

2 Session Two - Complex Numbers and Vectors

Datum > Curve KIM,ME,NIU

Introduction to Microsoft Excel 2007/2010

Transcription:

Graphing in L A TEX using PGF and TikZ Lindsey-Kay Lauderdale Mathew R. Gluck Department of Mathematics, University of Florida PGF and TikZ are packages in L A TEX that allow for programming figures into documents. PGF is an acronym for Portable Graphics Format and TikZ is a recursive acronym for TikZ ist kein Zeichenprogramm. With these packages, we get all the advantages of the L A TEXapproach to typesetting. As with typesetting in L A TEX however, programming graphics in PGF/TikZ has a steep learning curve. Fortunately, there is a 7 page user-friendly PGF/TikZ manual [1] to explain everything to us! The goal of this workshop is to explore some of the basic features of the PGF/TikZ packages in L A TEX while programming some simple graphics. Using the PGF/TikZ packages in a L A TEX document requires loading the PGF package somewhere in the preamble: \usepackage{pgfplots} This package has many libraries. We will use the following code to load the three libraries which will be used in the problems below. \usepgfplotslibrary{polar} \usepgflibrary{shapes.geometric} \usetikzlibrary{calc} The first allows for polar graphs, the second is used to insert basic shapes into a document and the third will help us label nodes in any shapes that we manually draw. Before we start with some examples, let s create a macro with some axes options. Suppose we want all functions graphed in the xy-plane to have the following specifications: (i) The x-axis and the y-axis in the middle of our graph. Code: axis x line=middle, axis y line=middle (ii) The x-axis and the y-axis labeled. Code: xlabel={$x$}, ylabel={$y$} (iii) The scale on the axes to be equal. Code: axis equal Henceforth, this group of specifications will be called my style. They can be seen in any axis environment in which my style option has been specified. To define these specifications and name them my style, place the following code in the preamble of your L A TEX document. 1

\pgfplotsset{my style/.append style={axis x line=middle, axis y line= middle, xlabel={$x$}, ylabel={$y$}, axis equal }} To typeset a graphics in L A TEX, we need to tell L A TEX that we want to typeset graphics. This will be done in the tikzpicture environment. Among the graphics we will be programming are plots of functions. To plot a function in the xy-plane, we will use the axis environment inside the tikzpicture environment. The basic structure of the code used to achieve this is as follows: \begin{axis}[axes options]... Every command inside these environments will end with a semicolon. Now we can start with some examples. These examples will not demonstrate all of the capabilities of PGF/TikZ, but they will introduce us to some of the features that we are most likely to use. Suppose we want to recreate the axis in the following exam question from a calculus course: Problem 1. Consider the region R bounded by y = x 1 and y = x 1. (a) Sketch the region R. (b) Come up with an expression (involving one or more integrals as appropriate) for determining the volume of the solid obtained by rotating R about the line x =. Then, calculate the volume by evaluating this expression. y 1 1 1 1 x Because we are drawing the xy-plane without plotting any functions, we need to specify the minimum and maximum values for both x and y. We also need to specify where the tick marks on the axes should be placed. The code for these specifications will be placed after my style in the axes options. Note that when written after the my style options, any axis options that conflict with axis options defined in my style will take precedence over the conflicted my style options. The following code yields the desired graph: \begin{axis}[my style, xtick={-,-,...,}, ytick={-,-,...,}, xmin=-, xmax=, ymin=-, ymax=]

Next, we ll plot both an xy-axis and a function on the plane determined by the axis. Consider the graph in the following precalculus exam problem. Problem. Use the graph of the function f(x) = x to select the equation of the function whose graph is shown. (a) y = x 1 4 y (b) y = x 1 + (c) y = x 1 + (d) y = x + 1 + 4 x (e) y = x + 1 + Start with the same basic code structure for plotting functions mentioned above. \begin{axis}[my style]... Will make two changes to this code. Namely, we will adjust the placement of the tick marks on the axes and we will plot the desired function. By default, tick marks are placed at the even-integer values on the axes. Here we want one tick mark between each of the even-valued tick marks. We accomplish this by placing the following code in the axis options: minor tick num=1 Our last step is to program the plot of the function into the code above. The structure of the syntax used for this is \addplot[plot options]{function}; In this example, the function to be plotted is y = x 1 +. The domain over which to plot the function is placed in the plot options. The code that produces the desired graph is \begin{axis}[my style, minor tick num=1] \addplot[domain=-:5]{-abs(x-1)+};

In the next example, we explain how to plot a simple piecewise-defined function. Consider the graph of the function in the following precalculus problem. Problem. The graph of f is given below. Use the graph of f to find f(1) f(4). y (a) 10 (b) (c) 0 (d) (e) 10 4 x 4 4 We wish to program the graph of the function { x if < x 1 f(x) = x if 1 < x <. To program this graph, start with a similar code as in Problem. There will be two changes made to the code. First, we must plot two separate lines, one for each of the subdomains (, 1] and (1, ). Second, we place a solid dot and an open dot at the appropriate positions of the graph so that the value of f(1) is apparent from the graph of the function. The code that achieves the plots of the two lines is as follows: \begin{axis}[my style] The above code yields the graph \addplot[domain=-7:1] {-x-}; \addplot[domain=1:7] {x}; 4

y 4 x 4 4 It remains to make the value of f(1) apparent on the graph. command is used. The following code To do this, the addplot \addplot[mark=*] coordinates {(1,-)}; adds a closed circle to the graph at (1, ), while the code \addplot[mark=*,fill=white] coordinates {(1,1)}; adds an open circle to the graph at (1, 1). Combining the three pieces of code above, we have the following code which produces the desired graph. \begin{axis}[my style] \addplot[domain=-7:1] {-x-}; \addplot[domain=1:7] {x}; \addplot[mark=*] coordinates {(1,-)}; \addplot[mark=*,fill=white] coordinates {(1,1)}; In the next example, we plot a more intricate piecewise-defined function. Consider the graph of the function given in the following calculus 1 problem. Problem 4. The graph of a function f is shown below. f(x) exists but f is discontinuous at x = a. lim x a Find all values of a for which 5

10 y (a) (b) 1 (c) 0 (d) (e) 7 5 x 10 5 5 10 5 10 To program this graph, we will start with a similar code as in Problem and make the following changes: (i) Changing the piecewise functions and their respective domains. (ii) Add the following code which plots multiple open/closed circles: only marks This will code allows us to plot multiple circles in the same line of code and ensures that we are only plotting the open/closed circles (not also the lines connecting them). This is a plot option and should be placed in the square brackets after the addplot command. These two changes yield the complete code: \begin{axis} \addplot[domain=-11:-5.1] {1/(x+5)}; \addplot[domain=-4.9:0] {-1/(x+5)}; \addplot[domain=0:] {x^+}; \addplot[domain=:10] {abs(x-7)-7}; \addplot[mark=*,only marks] coordinates {(-,4)(0,-)(,10)}; \addplot[mark=*,fill=white,only marks] coordinates {(-,-.5) (0,-.)(0,)(,-)}; The PNG and TikZ packages also support polar graphs. To graph in the polar coordinate system, we will use the polaraxis environment. Just like in the xy-plane, we define a style which we would like all of our polar graphs to have. For this workshop, we ll label our

angular axis every radians and we ll increase the thickness of the curves in our polar plots from the default curve thickness. This style will be called my style polar and is defined by placing the following code in the preamble of our L A TEX document. \pgfplotsset{my style polar/.append style={xticklabels={,, $\frac{\pi}{}$, $\frac{\pi}{}$, $\frac{\pi}{}$, $\frac{\pi}{}$, $\frac{5\pi}{}$, $\pi$, $\frac{7\pi}{}$, $\frac{4\pi}{}$, $\frac{\pi}{}$, $\frac{5\pi}{}$,$\frac{11\pi}{}$,}, thick }} Moreover, similarly to the cartesian plots constructed above, we have the following basic code structure for creating polar plots which includes the my style polar polar axis options: \begin{polaraxis}[my style polar]... \end{polaraxis} To practice plotting in polar coordinates, consider the graph in the following calculus problem. Problem 5. Find the area of the region enclosed by the polar curve r = 4 sin(θ). 5 0 0.5 1 1.5 7 11 4 5 To plot this graph, start with the basic code listed above for polar plots and simply input the code that plots the graph of the desired curve. In this case, there are a few technicalities to worry about. First, we need to increase the number of samples so the graph of the curve appears smooth. Second, since the curve does not define r as a function of θ, we need to plot r = sin(θ) on the appropriate domain (we must exclude all values of theta for which sin(θ) < 0). To increase the number of samples used in plotting our curve, insert the following code in the addplot options: samples=100 The code that produces the desired graph is as follows: 7

\begin{polaraxis}[my style polar] \addplot[domain=0:90, samples=100]{*sqrt(sin(*x))}; \addplot[domain=180:70, samples=100]{*sqrt(sin(*x))}; \end{polaraxis} In our next example, we discuss a method of organizing polar graphs in an array in a visually appealing way. To accomplish this we use the same tabular environment with a few minor adjustments. Consider the following problem from a multiple-choice calculus exam. Problem. Which of the following graphs best represents the graph of r = cos(θ)? (a) (b) 5 5 0 0.5 1 1.5 0 0.5 1 1.5 7 11 7 11 4 5 4 5 (c) (d) 5 5 0 0.5 1 1.5 0 0.5 1 1.5 7 11 7 11 4 5 4 5 8

To program this problem, note that the code for each multiple choice option is similar to the code in Problem 5. We ll only discuss the arrangement of the multiple-choice options into an array. As mentioned above, this is achieved via the tabular environment. We need to make a few adjustments to the default tabular environment. First, the width of the columns in the tabular environment is determined automatically by L A TEX. Since the entries in our array are graphs, we want to manually specify the width of the columns in our array to be cm. Moreover, we ll place the graph in each array entry at the top of the array cell via the p command. These specifications are reflected in the following code: \begin{tabular}{p{cm} p{cm}} (a) & (b) \\ (c) & (d) \end{tabular} The PGF and TikZ packages also support simple shapes. In the next example we give an example of how to manipulate one of the built-in shapes offered by PGF/TikZ. Note that the shape library must be loaded in the preamble to use the built-in shapes. To load the shape library, simply type \usepgflibrary{shapes.geometric} into the preamble of your document. Consider the cylinder drawn in the following calculus 1 problem. 9

Problem 7. A cylinder is being flattened so that its volume does not change. If the height h of the cylinder is changing at 0. inches per second, find the rate of change of the radius r when r = inches and h = 4 inches. r h To program the code for a cylinder we will use the node command inside the TikZ environment: \node[draw, cylinder] This code produces this cylinder: This undesirable cylinder can be modified by using the node options below. (i) We can rotate the cylinder by specifying the degree of rotation. Code: rotate=90 (ii) We can tilt the cylinder by changing the aspect ratio. Code: shape aspect = 4 (iii) We can specify an minimum height and a minimum width. Code: minimum height=4cm, minimum width=cm These node options should be placed in the square brackets after \node and will produce a much more desirable cylinder: 10

It remains to label the cylinder. There are many different ways of doing this. One way is to use the over 0 locations on the cylinder that PGF has already predefined (see page 44 of [1]). In the code of the cylinder below, these predefined locations are used to label the height h and the radius r of the cylinder. They are also used in the placement of the arrows seen in the picture. The following code produces the desired output: \node [draw, cylinder, shape aspect=4, rotate=90, minimum height=4cm, minimum width=cm] (c) {}; \coordinate(htop) at ($(c.before top)!-1*.1!(c.after top)$); \coordinate(hbot) at ($(c.after bottom)!-1*.1!(c.before bottom)$); \coordinate(hlab) at ($(htop)!.5!(hbot)+(c.north)!.9!(c.center)$); \draw[<->] (hbot)--(htop); \path (hlab) node {$h$}; %Modify height label here \coordinate (center) at ($(c.before top)!0.5!(c.after top)$); \coordinate (rlab) at ($(center)!0.5!(c.after top)$); \coordinate (rtop) at ($(center)!-1*.1!(c.after top)$); \draw[<->] (rtop) -- (c.after top); \path (rlab) node[above] {$r$}; %Modify radius label here In our final example, we ll manually draw a shape. This may be necessary if one wishes to include in their document a shape that is not one of the predefined shapes offered by PGF. It is not necessary to use the predefined L A TEX shapes. In the next example, we will see a box which is drawn using coordinates in R. Problem 8. A shipping crate with a square base is to be constructed so that it has a volume of 50 cubic meters. The material for the top and bottom cost $ per square meter and the material for the sides costs $1 per square meter. What dimensions will minimize the cost of construction? z x y To recreate this graphic, we will use the path and draw commands. The path command will allows us to define the eight vertices of the box and the draw command will connect them for us. The code of the box is as follows: 11

\pgfmathsetmacro{\x}{1} \pgfmathsetmacro{\y}{1} \pgfmathsetmacro{\z}{1.5} \path (0,0,\y) coordinate (A) (\x,0,0) coordinate (C) (0,\z,\y) coordinate (E) (\x,\z,0) coordinate (G) %Modify length of box here %Modify width of box here %Modify height of box here (\x,0,\y) coordinate (B) (0,0,0) coordinate (D) (\x,\z,\y) coordinate (F) (0,\z,0) coordinate (H); \draw (A) -- (B) -- (C) -- (G) -- (F) -- (B) (A) -- (E) -- (F) -- (G) -- (H) -- (E); \draw[gray,dashed] (A) -- (D) -- (C) (D) -- (H); The sides of the box can be labeled using similar techniques to Problem 7. Note that the box can be resized by changing the values for x, y and z in the code above. References [1] Tantau,Till, The TikZ and PGF Packages (Manual for version.10), Institut für Theoretische Informatik, Universität zu Lübeck, 007. 1