Using Wolfram Mathematica 10.3 to finish those question

profileoveleberry
m210-s16-class-9.pdf

M 210 Graphics in Mathematica March 22, 2016

We discuss creating graphics in more detail. We will want to discuss enough to create animations built from graphics. We also discuss how to determine the envelopes of some of the families of lines and circles drawn earlier.

Contents

1 Graphics in Mathematica 85

1.1 Drawing in Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

1.2 Envelopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

1.3 Cycloid Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

1 Graphics in Mathematica

1.1 Drawing in Mathematica

Relevant commands:

Mathematica input Result Graphics[primitives,options] represents a two-dimensional graphical image

Graphing Primitives

Arrow[{{x1,y1},{x2,y2}}] arrow from point (x1, y1) to point (x2, y2) Circle[{cx,cy},r] circle centered at (cx, xy) with radius r Disk[{cx,cy},r] filled disk centered at (cx, xy) with radius r Point Takes as argument a single point or a list of points Polygon Takes as argument list of points; the resulting poly-

gon is filled Plot

Line Takes as argument a pair of points or a list of points Rectangle[{xmin,ymin},{xmax,ymax}] rectangle with sides parallel to coordinate axes be-

tween the points (xmin, ymin) and (xmax, ymax) Text[expr,{cx,cy}] text expr centered at point (cx, cy)

Some Graphing Directives

Arrowheads[size] size is a number or one of Tiny, Small, Medium, Large

Background background color specification Dashing line dashing specification EdgeForm edge drawing specification FaceForm face drawing specification Opacity[a] opacity a PointSize[size] size is a number or one of Tiny, Small, Medium,

Large

Thickness[w] line thickness specification

M 210 — March 22, 2016 page 86

Example 1. Draw the regular pentagon shown in the adjacent figure.

Draw the pentagon using the following list of points:

In[1]:= α = 2π/5 In[2]:= Table[{Cos[k α], Sin[k α]}, {k, 0, 4}] In[3]:= Graphics[{Circle[{0,0},1], Point[%]}] In[4]:= p := Table[{Cos[k α + π/10], Sin[k α + π/10]}, {k, 0, 4}]

With the pentagon p defined above, connect every other point:

In[5]:= Graphics[{Circle[{0,0},1],Line[{p[[1]],p[[2]],p[[3]],p[[4]],p[[5]],p[[1]]}]}]

Source: www.spikedmath.com

Example 2. Draw the five pointed star shown in the adjacent figure.

With the pentagon p defined above, connect every other point:

In[1]:= Graphics[{Circle[{0,0},1],Line[{p[[1]],p[[3]],p[[5]],p[[2]],p[[4]],p[[1]]}]}]

We take a particular r to draw an example of a star, though it will not create the above perfect star; we postpone finding the exact value for r that will draw the above perfect star. It is then easy to determine all inner vertices of the star, and define a list of all vertices of the star as follows. The code

M 210 — March 22, 2016 page 87

In[2]:= r = 1/2; In[3]:= q = r Table[{Cos[3π/10 + 2 k π/5], Sin[3π/10 + 2 k π/5]}, {k, 0, 4}] In[4]:= star = {p[[1]],q[[1]],p[[2]],q[[2]],p[[3]],q[[3]],p[[4]],q[[4]],p[[5]],q[[5]],p[[1]]} In[5]:= Graphics[{Thick, Line[star]}]

results in the following imperfect star

In fact, the following creates an animation that shows the stars for varying values of r:

In[6]:= Clear[r] In[7]:= Manipulate[

q = r Table[{Cos[3π/10 + 2 k π/5], Sin[3π/10 + 2 k π/5]}, {k, 0, 4}]; star = {p[[1]],q[[1]],p[[2]],q[[2]],p[[3]],q[[3]],p[[4]],q[[4]],p[[5]],q[[5]],p[[1]]};

Graphics[{Thick, Line[star]}], {r, 0, 1}]

1.2 Envelopes

An envelope of a set of lines (or circles) is a curve tangent to all of the lines (or circles).

Example 3. Find the envelope of the line segments between (cos θ, 0) and (0, sin θ) for all possible angles θ.

Recall that the command Lines is defined on pairs of points (themselves specified as pairs using braces instead of the usual parentheses). So to draw the line connecting the points (cos θ, 0) and (0, sin θ) for a particular value of θ, say θ = 35◦, we can enter the following.

In[1]:= θ = 35◦; Graphics[{

Arrow[{{-1.2, 0}, {1.2, 0}}],

Arrow[{{0, -1.2}, {0, 1.2}}],

Line[{{Cos[θ], 0}, {0, Sin[θ]}}] }]

The command Line extends to lists of pairs of points, and we have seen that Table provides an easy way to generate such lists. So if we want to draw a family of lines parametrized in a regular way, then we can make use of Table, which we can simply put inside the Line command. Suppose we want to draw the lines connecting points (cos θ, 0) and (0, sin θ) for the angles in the first quadrant that are multiples of 10◦, then we can do so as follows.

M 210 — March 22, 2016 page 88

In[2]:= Graphics[{ Arrow[{{-1.2, 0}, {1.2, 0}}],

Arrow[{{0, -1.2}, {0, 1.2}}],

Line[Table[{{Cos[θ], 0}, {0, Sin[θ]}}, {θ, 0, 90◦, 10◦}]] }]

We can visualize what happens with more line segments by creating a demonstration using Manipulate. Instead of degrees, let’s use radians, and draw the lines finer as more are drawn. Also, there is no reason to restrict to the first quadrant: if θ increases in steps of 2π/n, then after n increases we get back to where we started.

In[3]:= Manipulate[Graphics[{ Arrow[{{-1.2, 0}, {1.2, 0}}],

Arrow[{{0, -1.2}, {0, 1.2}}],

{Thickness[0.1/n], Line[Table[{{Cos[θ],0}, {0,Sin[θ]}}, {θ,0,2 π,2 π/n}]]} }], {n, 24, 200, 4}]

Note that the control for the variable n goes from 24 to 200 in increments of 4, and that with increasing n the thickness of the lines decreases so that we get a nicer image. If we want to start n at a different value than its lowest bound, say at 40, we can do so by replacing n in the code by {n,40}. Do we further want a label, for example, “number of lines”, we add that as a third element in this list. Finally, if we want the value of n displayed next to its slider, we add Appearance → "Labeled" after the limit specifications of n.

For example,

In[4]:= Manipulate[Graphics[{ Arrow[{{-1.2, 0}, {1.2, 0}}],

Arrow[{{0, -1.2}, {0, 1.2}}],

{Thickness[0.1/n], Line[Table[{{Cos[θ],0}, {0,Sin[θ]}}, {θ,0,2 π,2 π/n}]]} }], {{n, 40, "number of lines"}, 24, 200, 4, Appearance → "Labeled"}]

The limiting figure as n tends to ∞ is called the envelope of this family of line segments. It is the curve under which a ladder of unit length can still slide along the vertical wall to the floor (or, alternatively, can be carried around the corner, touching two perpendicular walls). We can use calculus to find the shape of the above envelope. Since θ varies, we need to find the maximum of the functions that give the lines:

y = sin θ − (tan θ)x

We can do so by setting the derivative with respect to θ equal to 0, then solving the resulting equation. First remember to clear variable θ (which we earlier gave a specific value).

In[5]:= Clear[θ] In[6]:= D[Sin[θ] - (Tan[θ]) x, θ] In[7]:= Solve[% == 0, x]

The solution is x = sin3 θ. Substitute this into the above equation to obtain y. We can use Mathematica to simplify the expression:

In[8]:= Sin[θ] - (Tan[θ]) x /. → % In[9]:= Simplify[%]

We see that y = cos3 θ. Now check to see that this curve is indeed the envelope.

In[10]:= ParametricPlot[{Sin[t]^3, Cos[t]^3}, {t, 0, 2 π}]

We can actually plot it together with the envelope using Show to combine the graphic corresponding to n = 200, together with the plot above parametric curve:

M 210 — March 22, 2016 page 89

In[11]:= n = 200; Show[

Graphics[{

Arrow[{{-1.2, 0}, {1.2, 0}}],

Arrow[{{0, -1.2}, {0, 1.2}}],

{Thickness[0.1/n], Line[Table[{{Cos[θ],0}, {0,Sin[θ]}}, {θ,0,2 π,2 π/n}]]} }],

ParametricPlot[{Cos[t]^3, Sin[t]^3}, {t,0,2 π}, PlotStyle → {Thick,Red}] ]

The above code creates the following picture (line thickness not translated correctly in the picture in this document):

Finally, we can display the moving line inside the envelope:

In[12]:= Animate[Show[ Graphics[{

Arrow[{{-1.2, 0}, {1.2, 0}}],

Arrow[{{0, -1.2}, {0, 1.2}}]

}],

ParametricPlot[{Cos[t]^3, Sin[t]^3}, {t,0,2 π}, PlotStyle → {Thick, Red}], Graphics[{Thick, Blue, Line[{{Cos[θ], 0}, {0, Sin[θ]}}]}] ], {θ, 0, 2 π}]

Example 4. For positive number c, let Lc be the line with equation y = (2/c) + (1 − 1/c 2)x. Find

the envelope for this family of lines.

Let’s first draw these lines. The code

In[1]:= Show[ Plot[Table[2/c + (1 - 1/c^2) x, {c, .1, 12, .05}], {x, -5, 5},

PlotRange -> Automatic, PlotStyle -> Thickness[0.0015]],

Plot[Table[2/c + (1 - 1/c^2) x, {c, -12, -.1, .05}], {x, -5, 5},

PlotRange -> Automatic, PlotStyle -> Thickness[0.0015]]

]

results in the following picture

M 210 — March 22, 2016 page 90

-4 -2 2 4

-10

-5

5

10

in which the envelope is apparent. Next, find the critical point(s) by differentiating y with respect to parameter c, and solving where this derivative is 0.

In[2]:= ClearAll[c, x]; y := 2/c + (1 - 1/c^2) x In[3]:= D[y, c] In[4]:= Solve[% == 0, x] In[5]:= y /. % In[6]:= Simplify[%]

It is readily seen that x and y satisfy the equation y = x + 1/x. Plot this with above lines:

In[7]:= Show[ Plot[Table[2/c + (1 - 1/c^2) x, {c, .1, 12, .05}], {x, -5, 5},

PlotRange -> Automatic, PlotStyle -> Thickness[0.0015]],

Plot[Table[2/c + (1 - 1/c^2) x, {c, -12, -.1, .05}], {x, -5, 5},

PlotRange -> Automatic, PlotStyle -> Thickness[0.0015]],

Plot[x + 1/x, {x, -5, 5}, PlotStyle -> {Red, Thick}, Exclusions -> {0}]

]

which produces the following picture.

-4 -2 2 4

-10

-5

5

10

M 210 — March 22, 2016 page 91

1.3 Cycloid Curves

Example 5. The cycloid was first studied by Marin Mersenne (1588–1648). The cycloid is defined to be the locus of a fixed point on a circle rolling along a line. In the following figure a circle of positive radius a is rolled along the x-axis. One point on the rolling circle as well as the spoke to the circle’s center is drawn at various instances.

Marin Mersenne (1588–1648)

The following figure displays the circle after it has rolled a distance at.

at

(x, y)

p

q θ

The length of the arc on the circle between the vertical radius and the radius with endpoint (x, y) is equal to at. If angle θ is as indicated in the figure, then the above arc has angle θ + π/2, thus length (θ + π/2)a. It follows that (θ + π/2)a = at, which implies t = θ + π/2. Thus θ = t − π/2, so the indicated lengths are p = a cos(t − π/2) = a sin t, and q = a sin(t − π/2) = −a cost. We have x = at − p = a(t − sin t) and y = a + q = a(1 − cos t). Thus parametric equations for the cycloid are

x = a(t − sin t) and y = a(1 − cos t).

We can now animate the cycloid in Mathematica:

Manipulate[

Graphics[{

{EdgeForm[{Black, Thick}], Yellow, Disk[{t, 1}]},

Line[{{-1, 0}, {7.25, 0}}],

{Thick, Line[{{t, 1}, {t - Sin[t], 1 - Cos[t]}}]},

{PointSize[0.015], Red, Point[{t - Sin[t], 1 - Cos[t]}]}

}], {t, 0, 2 Pi}]

M 210 — March 22, 2016 page 92

We can add the trace of the red point:

Manipulate[

Show[{

Graphics[{

{EdgeForm[{Black, Thick}], Yellow, Disk[{t, 1}]},

Line[{{-1, 0}, {7.25, 0}}],

{Thick, Line[{{t, 1}, {t - Sin[t], 1 - Cos[t]}}]},

{PointSize[0.015], Red, Point[{t - Sin[t], 1 - Cos[t]}]}

}],

ParametricPlot[{u - Sin[u], 1 - Cos[u]}, {u, 0, t}, PlotStyle -> Red]

}],{t, -10^-6, 2 Pi}]

The following example is a variation on the cycloid.

Example 6. Instead of rolling a disk along a straight line, roll it along the inside perimeter of a larger disk. The following figure depicts the initial situation.

x

y

b b b P

Assume the larger circle has radius R and the smaller circle has radius r. If the center of the smaller circle has argument equal to θ (the angle the segment connecting the center to the origin makes with the positive x-axis), then what is the position of point P? The following figure depicts the situation:

x

y

b

b

b B

θ b A

R

ϕ

r C

b

P

M 210 — March 22, 2016 page 93

Note that the length of the arc from A to B along the larger circle is equal to R θ. If angle ϕ is as indicated in the above figure1, then the length of the arc from B to P along the smaller circle is equal to r ϕ. The assumption that the smaller circle rolls inside the larger circle implies that these two arc lengths be equal, so we get the equation r ϕ = R θ, thus ϕ = (R/r)θ.

We are now in a position to find the coordinates of point P . The following figure shows this point on the smaller circle, enlarged to better see it, and with a horizontal axis added, which divides angle ϕ in two parts: θ and ϕ − θ.

b C

b B

r

b

P

θ

ϕ − θ

The above figure shows the coordinates of point P relative to center C of the small circle to be (r cos(ϕ − θ), −r sin(ϕ − θ)).

Center C is distance R−r from the origin (center of the large circle), so its coordinates are ((R−r) cos θ, (R− r) sin θ).

Conclude that point P = (x, y) with

x = (R − r) cos θ + r cos(ϕ − θ) and y = (R − r) sin θ − r sin(ϕ − θ).

Let’s put this to the test in Mathematica using r = R/4. Note that then ϕ = 4θ, so that ϕ − θ = 3θ.

First draw the initial situation:

In[1]:= Graphics[{ Circle[{0, 0}, 1],

Circle[{0.75, 0}, 0.25],

{PointSize[0.012], Red, Point[{1, 0}]}

}]

Next we add the situation for a particular value for θ (once the correct figure shows, we can delete the special value for θ and add code to get the animation).

In[2]:= θ = 35◦; Graphics[{

Circle[{0, 0}, 1],

Circle[{0.75, 0}, 0.25],

{PointSize[0.012], Red, Point[{1, 0}]},

Circle[{0.75 Cos[θ], 0.75 Sin[θ]}, 0.25], {PointSize[0.012], Red,

Point[{0.75 Cos[θ] + 0.25 Cos[3 θ], 0.75 Sin[θ] - 0.25 Sin[3 θ]}]} }]

The figure is as before, so we delete the special value of θ, as well as the initial situation, and create an animation as follows.

In[3]:= Animate[ Graphics[{

Circle[{0, 0}, 1],

Circle[{0.75 Cos[θ], 0.75 Sin[θ]}, 0.25], {PointSize[0.012], Red,

Point[{0.75 Cos[θ] + 0.25 Cos[3 θ], 0.75 Sin[θ] - 0.25 Sin[3 θ]}]} }], {θ, 0, 2 π}]

1The drawing of which required the following argument to calculate the exact location of point P in the figure.

M 210 — March 22, 2016 page 94

Finally, we can add the parametric plot traced by point P . First let’s draw this plot by itself, before including it in the animation using Show to combine graphics. We use a different parameter than θ in anticipation of later inserting this code in the animation:

In[4]:= ParametricPlot[ {0.75 Cos[t] + 0.25 Cos[3 t], 0.75 Sin[t] - 0.25 Sin[3 t]}, {t, 0, 2 π}, PlotStyle → {Thick, Red}]

Now let’s insert this into the animation, drawing the parametric plot up to θ instead of 2π:

In[5]:= Animate[ Show[

Graphics[{

Circle[{0, 0}, 1],

Circle[{0.75 Cos[θ], 0.75 Sin[θ]}, 0.25], {PointSize[0.012], Red,

Point[{0.75 Cos[θ] + 0.25 Cos[3 θ], 0.75 Sin[θ] - 0.25 Sin[3 θ]}]} }],

ParametricPlot[

{0.75 Cos[t] + 0.25 Cos[3 t], 0.75 Sin[t] - 0.25 Sin[3 t]}, {t, 0, 2 π}, PlotStyle → {Thick, Red}]

], {θ, 0, 2 π}]

Example 7. Determine the envelope of the family of lines of TikZercise 3 of Homework 5: the lines connecting points on the unit circle centered at the origin whose polar angles are in the ratio 4 : 1.

Solution. Let’s first draw this family of lines in Mathematica, using the With command that will not make it necessary to clear parameters (given values within this command). The following code will show the unit circle and one of the family of lines:

With[{\[Theta] = 25 \[Degree]},

Graphics[{

Circle[{0, 0}, 1],

Line[{{Cos[\[Theta]], Sin[\[Theta]]}, {Cos[4 \[Theta]], Sin[4 \[Theta]]}}]

}]

]

We draw a family of lines placing a Table command inside Graphics:

Graphics[{

Circle[{0, 0}, 1],

Table[{Thickness[0.001],

Line[{{Cos[\[Theta]], Sin[\[Theta]]}, {Cos[4 \[Theta]],

Sin[4 \[Theta]]}}]}, {\[Theta], 0 \[Degree], 360 \[Degree], 3 \[Degree]}]

}]

The equations of the lines are easily determined: their slopes are

sin(4θ) − sin(θ)

cos(4θ) − cos(θ) ,

so the lines have equations

y = sin(4θ) − sin(θ)

cos(4θ) − cos(θ) (x − cos(θ)) + sin(θ).

We need to optimize these, so for fixed x have Mathematica compute the derivative with respect to θ:

D[(Sin[4 \[Theta]] - Sin[\[Theta]])/(Cos[4 \[Theta]] - Cos[\[Theta]]) (x - Cos[\[Theta]]) +

Sin[\[Theta]], \[Theta]] // Simplify

Find the relation between the critical point (where the above derivative is zero) and x:

Solve[% == 0, x]

M 210 — March 22, 2016 page 95

Next, use replacement to find the corresponding y:

(Sin[4 \[Theta]] - Sin[\[Theta]])/(Cos[4 \[Theta]] - Cos[\[Theta]]) (x - Cos[\[Theta]]) + Sin[\[Theta]]

/. % // Simplify

Put coordinate functions x and y together in ParametricPlot to graph the curve; make use of Show to plot it together with the above graphic displaying the lines.

Homework 8 This assignment consists of only Mathematica problems. Solutions of the problems should be given in a single Mathematica notebook displaying the requested formulas and graphics.

1. Find the exact value of r that creates a perfect 5-pointed star in Example 2.

2. Graph the family of lines connecting the points (a, 0) and (0, b), where a + b = 2, and determine an equation for the envelope of this family of lines2.

3. Graph the family of circles centered at points of the unit circle and all going through the point (1, 0), and determine an equation for the envelope of this family of circles.

4. Determine the envelope of the family of lines of Example 4 of class 6 (March 1): the lines connecting points on the unit circle centered at the origin whose polar angles are in the ratio 3 : 1.

5. Determine the envelope of the family of lines of TikZercise 2 of Homework 5: the lines connecting points on the unit circle centered at the origin whose polar angles are in the ratio 2 : 1.

6. Create an animation of rolling a small disk inside a larger one when the radii are in the ratio 1 : 3.

7. Create an animation of rolling a disk outside a disk of equal radius.

Submit Homework 8 by 3:00pm on Tuesday, March 29, to Moodle.

Please put course, your last name, and homework number in the name of the file you submit (using a file name like M210-HW8-name.nb).

2Note that the lines are parametrized by only one parameter, since the given condition that a + b = 2 expresses b

in terms of a.

  • Graphics in Mathematica
    • Drawing in Mathematica
    • Envelopes
    • Cycloid Curves