# Four ways to plot a circle
# Plotting a circle is quite straightforward, and also unexciting.
# However, since it is so familiar an object, it is probably useful to
# use it to compare four different methods for doing the same thing.
#
# First, let's recall that a circle can be expressed as the set of
# points ((x,y)) which are distance R from the center (a,b). That is,
> sqrt((x-a)^2+(y-b)^2) = R#
# or, since r is always positive, we can square both sides to obtain the
# usual equation
> circleEqn:= (x-a)^2+(y-b)^2=R^2;
2 2 2
circleEqn := (x - a) + (y - b) = R
# 1. As a pair of functions
# To plot the circle as a pair of functions, we need to solve for y. We
# can do this easily in our heads, (at least I hope you can) or ask
# maple:
> circfuncs:= {solve(circleEqn,y)};
#
circfuncs :=
2 2 2 1/2 2 2 2 1/2
{b + (-x + 2 x a - a + R ) , b - (-x + 2 x a - a + R ) }
# Since we need a specific choice for the center and radius for this to
# work, let's substitute in a=2,b=1,R=3:
> mycircfuncs:= subs(a=2,b=1,R=3,circfuncs);
2 1/2 2 1/2
mycircfuncs := {1 + (-x + 4 x + 5) , 1 - (-x + 4 x + 5) }
> plot(mycircfuncs,x=-1..5,scaling=constrained);
# 2. In polar coordinates
# If the circle is centered at the origin, it is, in some sense, most
# naturally expressed in polar coordinates, as r=R. If the center is
# not at the origin, things are a bit messier, but not too bad. Recall
# that polar coordinates represent a point in the plane in terms of its
# distance from the origin (usually represented as r) and the angle
# theta it makes with the positive x-axis (measured counterclockwise).
# Thus, from elementary trigonometry, we have x=r*cos(theta and
# y=r*sin(theta.
#
# Making this change gives us the following:
> subs(x=r*cos(theta),y=r*sin(theta),circleEqn);
2 2 2
(r cos(theta) - a) + (r sin(theta) - b) = R
# which isn't too bad... Since we usually try to represent a polar
# function as r=f(theta), we can solve for r to obtain the following.
> polarcirc:=solve(",r);
2 2
polarcirc := cos(theta) a + sin(theta) b + (cos(theta) a
2 2 2 2 2
+ 2 cos(theta) a sin(theta) b + sin(theta) b - b + R - a )^
1/2 2 2
, cos(theta) a + sin(theta) b - (cos(theta) a
2 2 2 2 2
+ 2 cos(theta) a sin(theta) b + sin(theta) b - b + R - a )^
1/2
# This is really rather dreadful looking, but notice that if a and b are
# both zero, the first simplifies to r=R and the second becomes r=-R.
# On closer examination, you can see that if the circle surrounds the
# origin, (that is a^2+b^2 mypolarcirc:= subs(a=2,b=1,R=3,polarcirc[1]);
mypolarcirc := 2 cos(theta) + sin(theta)
2 2 1/2
+ (4 cos(theta) + 4 cos(theta) sin(theta) + sin(theta) + 4)
# To plot it, we just tell maple that we are using polar coordinates.
# We need to allow theta to range over the whole circle, of course.
> plot(mypolarcirc,theta=0..2*Pi,coords=polar);
# I should point out that maple can also do parametric polar plots,
# where both the radius and angle are functions of a parameter t, for
# example, we can make a "butterfly" with something like
> plot([cos(3*t),sin(4*t),t=0..2*Pi], coords=polar,
> axes=boxed,scaling=constrained);
# This, of course, has nothing to do with plotting a circle, but is fun
# anyway.
# 3. In parametric form
# Representing the circle parametrically is quite simple. Recall that
# for a circle centered at the origin, the pair of equations
# {x=R*cos(theta), y=R*sin(theta)} describe a circle of radius R as
# theta varies from 0 to 2*Pi. If we want to move the center from the
# origin, all we have to do is add the appropriate constant to the x and
# y coordinates.
> plot([2+3*cos(theta),1+3*sin(theta),theta=0..2*Pi],
> scaling=constrained);
# 4. Described implicitly
# Maple can also plot the relation describing the circle implicitly. It
# has to work much harder in this case, and the results aren't always as
# nice. But the call is quite easy, although first we must load in the
# plots library.
> with(plots):
> implicitplot( (x-2)^2 + (y-1)^2= 3^2, x=-10..10, y=-10..10);
# It is worth pointing out a few things here. First, notice that the
# circle looks a bit ragged. Also, note that I specified a rather large
# range for x and y (-10 to 10) but maple chose to only show a much
# smaller range. This is because of the way implicitplot works. The
# range we specify specifies not the desired plotting region, but where
# to search for solutions.
# Maple chops up the region given into a number of subregions (50 by 50,
# unless you say otherwise) and searches for a solution in each of them.
# It then plots whatever it found, assuming it is connected "in a nice
# way". Sometimes it makes mistakes, of course.
# We can greatly improve our picture by specifying a better search area.
> implicitplot( (x-2)^2 + (y-1)^2= 3^2, x=-1..5, y=-2..4);
# Also, we can specify the number of points to search using the "grid"
# option. Rather than the circle, let's use a more complicated example
# to better show what goes wrong.
# A trickier example of implicitplot.
# A deltiod has 3 cusps, making it trickier to get right. The equation
# (at least in Cartesian cordinates) is also nastier.
> implicitplot( (x^2+y^2)^2 + 8*x*(3*y^2-x^2) + 18*(x^2+y^2) = 27,
> x=-10..10,y=-10..10,scaling=constrained,axes=boxed);
# Certainly some refining is in order. Note the strange little island
# down in the lower left corner. Reducing the area of search will help
# a bit.
> implicitplot( (x^2+y^2)^2 + 8*x*(3*y^2-x^2) + 18*(x^2+y^2) = 27,
> x=-1.5..3,y=-2.5..2.5,scaling=constrained,axes=boxed);
# Better, but not perfect. One obvious problem is that the "nose" at
# the right is being cut off, because we aren't testing points on the
# x-axis. This can be remedied by specifying a finer grid, but with an
# odd number of points.
> implicitplot( (x^2+y^2)^2 + 8*x*(3*y^2-x^2) + 18*(x^2+y^2) = 27,
> x=-1.5..3,y=-2.5..2.5,grid=[75,75],scaling=constrained,axes=boxed);
# Much, much better.
#
# 5. By cheating
# Maple has a built-in circle drawing function, in the plottools
# package.
> with(plots):
> with(plottools):
> display(circle([2,1],3));
# That is certainly the easiest of all. But you didn't learn much by
# doing it. Besides, I said I would show 4 ways, and this is the fifth.