{VERSION 5 0 "IBM INTEL NT" "5.0" }
{USTYLETAB {CSTYLE "Maple Input" -1 0 "Courier" 0 1 255 0 0 1 0 1 0 0
1 0 0 0 0 1 }{CSTYLE "2D Math" -1 2 "Times" 0 1 0 0 0 0 0 0 2 0 0 0 0
0 0 1 }{CSTYLE "2D Comment" 2 18 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 }
{CSTYLE "" -1 256 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1
257 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 258 "" 0 1 0 0
0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 259 "" 0 1 0 0 0 0 0 1 0 0 0 0
0 0 0 1 }{CSTYLE "" -1 260 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }
{CSTYLE "" -1 261 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1
262 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 263 "" 0 1 0 0
0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 264 "" 0 1 0 0 0 0 0 1 0 0 0 0
0 0 0 1 }{CSTYLE "" -1 265 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }
{CSTYLE "" -1 266 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 }{CSTYLE "" -1
267 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 268 "" 0 1 0 0
0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 269 "" 0 1 0 0 0 0 0 1 0 0 0 0
0 0 0 1 }{CSTYLE "" -1 270 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }
{CSTYLE "" -1 271 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1
272 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 273 "" 0 1 0 0
0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 274 "" 0 1 0 0 0 0 1 0 0 0 0 0
0 0 0 1 }{CSTYLE "" -1 275 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 }
{CSTYLE "" -1 276 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 }{PSTYLE "Normal
" -1 0 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 1 1 }1
1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Heading 1" -1 3 1 {CSTYLE "" -1
-1 "Times" 1 18 0 0 0 1 2 1 2 2 2 2 1 1 1 1 }1 1 0 0 8 4 1 0 1 0 2 2
0 1 }{PSTYLE "Heading 3" -1 5 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1
1 1 2 2 2 2 1 1 1 1 }1 1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Title" -1
18 1 {CSTYLE "" -1 -1 "Times" 1 18 0 0 0 1 2 1 1 2 2 2 1 1 1 1 }3 1 0
0 12 12 1 0 1 0 2 2 19 1 }{PSTYLE "Normal" -1 256 1 {CSTYLE "" -1 -1 "
Times" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 1 1 }1 1 0 0 0 0 3 36 3 36 2 2 0
1 }{PSTYLE "Normal" -1 257 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2
2 2 2 2 2 1 1 1 1 }3 1 0 0 0 0 1 0 1 0 2 2 0 1 }}
{SECT 0 {PARA 18 "" 0 "" {TEXT -1 20 "Programming in Maple" }}{PARA
257 "" 0 "" {TEXT -1 97 "Roger Kraft\nDepartment of Mathematics, Compu
ter Science, and Statistics\nPurdue University Calumet" }}{PARA 257 "
" 0 "" {TEXT -1 24 "roger@calumet.purdue.edu" }}{PARA 0 "" 0 "" {TEXT
-1 0 "" }}{SECT 0 {PARA 3 "" 0 "" {TEXT -1 23 "5.3. More loop examples
" }}{PARA 0 "" 0 "" {TEXT -1 62 "In this section we work on more invol
ved examples using loops." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "
" }}}{SECT 1 {PARA 5 "" 0 "" {TEXT -1 30 "5.3.1. Example 1: Riemann su
ms" }}{PARA 0 "" 0 "" {TEXT -1 201 "A common use for a for-loop is to \+
compute a sum. In this example we show how to use a loop to compute Ri
emann sums from calculus. But first let us look at a couple of simple \+
sums written as for-loops. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA
0 "" 0 "" {TEXT -1 72 "A well known result about sums of integers is t
hat the sum of the first " }{XPPEDIT 18 0 "n;" "6#%\"nG" }{TEXT -1 22
" positive integers is " }{XPPEDIT 18 0 "n*(n+1);" "6#*&%\"nG\"\"\",&F
$F%F%F%F%" }{TEXT -1 27 "/2. Let us verify this for " }{XPPEDIT 18 0 "
n = 1000;" "6#/%\"nG\"%+5" }{TEXT -1 101 " using a for-loop. We want a
for-loop that will compute the following sum, written in sigma notati
on." }}{PARA 257 "" 0 "" {XPPEDIT 18 0 "Sum(i,i = 1 .. 1000);" "6#-%$S
umG6$%\"iG/F&;\"\"\"\"%+5" }{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1
357 "The basic idea of using a for-loop to compute a sum is that we co
mpute a running total. If you want to add 1+2+3+4+5+6+7, we start with
0, then we add 1 to 0, then we take that result and add 2 to it, then
we take that result and add 3 to it, then we take that result and add
4 to it, etc. In the following for-loop, we will let the running tota
l be called " }{TEXT 0 1 "s" }{TEXT -1 6 " (for " }{TEXT 0 1 "s" }
{TEXT -1 27 "um) and we will initialize " }{TEXT 0 1 "s" }{TEXT -1
252 " with the value 0. Notice how the single command in the body of t
he for-loop adds another integer to the sum for each iterate of the lo
op. Compare this for-loop with the sigma notation above. In what ways \+
are they alike and in what ways do they differ?" }}{EXCHG {PARA 0 "> \+
" 0 "" {MPLTEXT 1 0 7 "s := 0:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 38 "f
or i from 1 to 1000 do s := s + i od:" }}}{PARA 0 "" 0 "" {TEXT -1
121 "Notice that we have a colon at the end of the loop so that we do \+
not see 1000 lines of output. Let us check the value of " }{TEXT 0 1 "
s" }{TEXT -1 10 ", the sum." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0
2 "s;" }}}{PARA 0 "" 0 "" {TEXT -1 43 "Now verify this result against \+
the formula " }{XPPEDIT 18 0 "n(n+1);" "6#-%\"nG6#,&F$\"\"\"F'F'" }
{TEXT -1 3 "/2." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "1000*(100
0+1)/2;" }}}{PARA 0 "" 0 "" {TEXT -1 78 "By the way, Maple can verify \+
the above (symbolic) formula for us by using the " }{TEXT 0 3 "sum" }
{TEXT -1 9 " command." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "i:=
'i': n:='n':" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "sum( i, i=1
..n );" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "factor( % );" }}}
{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT
-1 0 "" }}{PARA 0 "" 0 "" {TEXT 257 8 "Exercise" }{TEXT -1 27 ": Write
a procedure called " }{TEXT 0 8 "add_list" }{TEXT -1 110 " that takes
one input, a list of numbers, and computes the sum of the numbers in \+
the list. Do not use Maple's " }{TEXT 0 3 "add" }{TEXT -1 4 " or " }
{TEXT 0 3 "sum" }{TEXT -1 10 " commands." }}{EXCHG {PARA 0 "> " 0 ""
{MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 ""
{TEXT -1 82 "Here is another example of computing a sum. We know from \+
calculus that the number " }{XPPEDIT 18 0 "exp(1);" "6#-%$expG6#\"\"\"
" }{TEXT -1 92 " is the sum of the reciprocals of all the factorials. \+
So we can compute an approximation of " }{XPPEDIT 18 0 "exp(1);" "6#-%
$expG6#\"\"\"" }{TEXT -1 151 " by summing the reciprocals of the facto
rials from say 0! to 10!. So we want a for-loop that will compute the \+
following sum, written in sigma notation." }}{PARA 256 "" 0 ""
{XPPEDIT 18 0 "Sum(1/n!,n = 0 .. 10);" "6#-%$SumG6$*&\"\"\"F'-%*factor
ialG6#%\"nG!\"\"/F+;\"\"!\"#5" }{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT
-1 51 "Compare the next for-loop with this sigma notation." }}{EXCHG
{PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "e := 0;" }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 41 "for n from 0 to 10 do e := e + 1/(n!) od;" }}}{PARA
0 "" 0 "" {TEXT -1 97 "That is not what we really want. Let us modify \+
the loop so that it computes with decimal numbers." }}{EXCHG {PARA 0 "
> " 0 "" {MPLTEXT 1 0 7 "e := 0;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 43
"for i from 0 to 10 do e := e + 1.0/(i!) od;" }}}{PARA 0 "" 0 ""
{TEXT -1 125 "In this example we put a semi colon at the end of the lo
op so that we could see the answers converge to the correct value of \+
" }{XPPEDIT 18 0 "exp(1);" "6#-%$expG6#\"\"\"" }{TEXT -1 61 ", which i
s given by the next command (to ten decimal places)." }}{EXCHG {PARA
0 "> " 0 "" {MPLTEXT 1 0 16 "evalf( exp(1) );" }}}{PARA 0 "" 0 ""
{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 119 "Now let us turn to Riema
nn sums. The following execution group will compute a (left hand) Riem
ann sum for the function " }{TEXT 0 1 "f" }{TEXT -1 24 " over the inte
rval from " }{TEXT 0 1 "a" }{TEXT -1 4 " to " }{TEXT 0 1 "b" }{TEXT
-1 7 " using " }{TEXT 0 1 "n" }{TEXT -1 66 " rectangles. If you want t
o, you can change any of the values for " }{TEXT 0 1 "f" }{TEXT -1 2 "
, " }{TEXT 0 1 "a" }{TEXT -1 2 ", " }{TEXT 0 1 "b" }{TEXT -1 4 " or "
}{TEXT 0 1 "n" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1
0 38 "f := x -> sin(x); # The function." }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 44 "a := 0; # Left hand endpoint." }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 45 "b := 3/2*Pi; # Right hand
endpoint." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 45 "n := 75; \+
# How many rectangles." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 52 "L||n :=
0: # Set the running total to 0." }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 25 "for i from 0 to n-1 do " }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 61 " x||i := a+i*(b-a)/n; # Compute a partitio
n point." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 64 " L||n := L||n+f(x||i)
*(b-a)/n: # Add area of rectangle to sum." }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 3 "od:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 57 "'L[n]' = %; \+
# Display the result symbolically," }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 40 "'L[n]' = evalf(%%); # and numerically" }}}{PARA 0
"" 0 "" {TEXT -1 117 "You should think of the for-loop in this executi
on group as the Maple version of the following formula from calculus.
" }}{PARA 0 "" 0 "" {TEXT -1 62 " \+
" }{XPPEDIT 18 0 "L[n] = Sum(f(x[i])*Delta*x,
i = 0 .. n-1);" "6#/&%\"LG6#%\"nG-%$SumG6$*(-%\"fG6#&%\"xG6#%\"iG\"\"
\"%&DeltaGF3F0F3/F2;\"\"!,&F'F3F3!\"\"" }}{PARA 0 "" 0 "" {TEXT -1
288 "This sigma-notation represents the sum and the for-loop in the ex
ecution group computes the sum. An interesting feature of actually com
puting the sum, instead of just representing it abstractly, is that th
e computation must be very precise about what it is doing. For example
, notice how " }{XPPEDIT 18 0 "x[i];" "6#&%\"xG6#%\"iG" }{TEXT -1 64 "
in the sigma-notation is a short hand for the Maple expression " }
{TEXT 0 11 "a+i*(b-a)/n" }{TEXT -1 101 ". In calculus courses where th
is sigma-notation is used, students often do not realize just what the
" }{XPPEDIT 18 0 "x[i]" "6#&%\"xG6#%\"iG" }{TEXT -1 134 " represents
. But if you have to write a Maple for-loop to implement the sum, then
you are forced to explicitly state what the symbol " }{XPPEDIT 18 0 "
x[i]" "6#&%\"xG6#%\"iG" }{TEXT -1 58 " means. Maple will not be able t
o figure out for you what " }{XPPEDIT 18 0 "x[i]" "6#&%\"xG6#%\"iG" }
{TEXT -1 60 " represents if you use it in a for-loop without defining \+
it." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 ""
{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 258 8 "Exercise" }{TEXT -1 43 ":
Modify the execution group for computing " }{XPPEDIT 18 0 "L[n];" "6#
&%\"LG6#%\"nG" }{TEXT -1 12 " to compute " }{XPPEDIT 18 0 "R[n];" "6#&
%\"RG6#%\"nG" }{TEXT -1 28 ", right hand sums. Then try " }{XPPEDIT
18 0 "T[n];" "6#&%\"TG6#%\"nG" }{TEXT -1 4 " nd " }{XPPEDIT 18 0 "S[n]
;" "6#&%\"SG6#%\"nG" }{TEXT -1 40 ", the trapezoid sums and Simpson's \+
rule." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA
5 "" 0 "" {TEXT -1 35 "5.3.2. Example 2: Pascal's triangle" }}{PARA 0
"" 0 "" {TEXT -1 144 "Our next example shows how we can develop a proc
edure for printing out Pascal's triangle. The next for-loop prints out
a table of expansions of " }{XPPEDIT 18 0 "(x+y)^n;" "6#),&%\"xG\"\"
\"%\"yGF&%\"nG" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1
0 16 "x:='x': y:='y': " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "for i fro
m 0 to 8 do " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 " expand( (x+y)^i )
;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od;" }}}{PARA 0 "" 0 "" {TEXT
-1 289 "The coefficients in these polynomials have a name (the binomia
l coefficients) and they make up what is called Pascal's triangle. Pas
cal's triangle and the binomial coefficients have a lot of interesting
properties, so let us see if we can extract Pascal's triangle out of \+
the last example." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 ""
{TEXT -1 18 "The Maple command " }{TEXT 0 6 "coeffs" }{TEXT -1 204 " r
eturns the coefficients of a polynomial as an expression sequence. The
coefficients of the above polynomials make up Pascal's triangle. So l
et us see if the next for-loop will give us Pascal's triangle." }}
{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "for i from 0 to 8 do" }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 30 " coeffs( expand( (x+y)^i ) );" }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od;" }}}{PARA 0 "" 0 "" {TEXT -1 35
"That did not work. The output from " }{TEXT 0 6 "coeffs" }{TEXT -1
168 " gives the coefficients of each polynomial as an expression seque
nce, but the coefficients are not in the same order as in the polynomi
al (see the next two commands). " }}{EXCHG {PARA 0 "> " 0 ""
{MPLTEXT 1 0 18 "expand( (x+y)^8 );" }}}{EXCHG {PARA 0 "> " 0 ""
{MPLTEXT 1 0 12 "coeffs( % );" }}}{PARA 0 "" 0 "" {TEXT -1 319 "So we \+
do not yet have Pascal's triangle. Let us try another approach. Since \+
polynomials are data structures, let us use our knowledge of Maple's d
ata structure commands to get the coefficients of each polynomial in t
he order we want them. Since we want the coefficients to be in an expr
ession sequence, we will use the " }{TEXT 0 3 "seq" }{TEXT -1 24 " com
mand. We can use an " }{TEXT 0 2 "op" }{TEXT -1 85 " command to pick o
ff the individual terms of the polynomial, and then we can use the " }
{TEXT 0 6 "coeffs" }{TEXT -1 62 " command to return the coefficient of
just one term at a time." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 18
"expand( (x+y)^8 );" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 38 "seq(
coeffs(op(j, %)), j=1..nops(%) );" }}}{PARA 0 "" 0 "" {TEXT -1 67 "Th
at worked. Here is a for-loop built around the last two commands." }}
{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "for i from 0 to 8 do" }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 21 " expand( (x+y)^i );" }}{PARA 0 ">
" 0 "" {MPLTEXT 1 0 41 " seq( coeffs(op(j, %)), j=1..nops(%) );" }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od;" }}}{PARA 0 "" 0 "" {TEXT -1 59
"Oops, too much output. Put a colon at the end of the final " }{TEXT
0 2 "od" }{TEXT -1 11 " and use a " }{TEXT 0 5 "print" }{TEXT -1 33 " \+
command in the body of the loop." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT
1 0 20 "for i from 0 to 8 do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 21 " \+
expand( (x+y)^i );" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 41 " seq( coeff
s(op(j, %)), j=1..nops(%) );" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 14 " \+
print( % );" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od:" }}}{PARA 0 "" 0
"" {TEXT -1 103 "There we go. Notice that every number in Pascal's tri
angle is the sum of the two numbers just above it." }}{PARA 0 "" 0 ""
{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 188 "Of course, if binomial c
oefficients are important in mathematics, then we should expect Maple \+
to have a command for computing them directly instead of picking them \+
out of the expansion of " }{XPPEDIT 18 0 "(x+y)^n;" "6#),&%\"xG\"\"\"%
\"yGF&%\"nG" }{TEXT -1 15 ". The command " }{TEXT 0 13 "binomial(n,j)
" }{TEXT -1 45 " returns the coefficient of the j'th term of " }
{XPPEDIT 18 0 "(x+y)^n;" "6#),&%\"xG\"\"\"%\"yGF&%\"nG" }{TEXT -1 1 ".
" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 29 "seq( binomial(8,j), j=0.
.8 );" }}}{PARA 0 "" 0 "" {TEXT -1 60 "So we can get Pascal's triangle
with the following for-loop." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1
0 20 "for i from 0 to 8 do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 " seq
( binomial(i,j), j=0..i );" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od;" }
}}{PARA 0 "" 0 "" {TEXT -1 87 "Notice how this loop uses two index var
iables, one for the loop itself and one for the " }{TEXT 0 3 "seq" }
{TEXT -1 14 " command. The " }{TEXT 0 1 "i" }{TEXT -1 59 " index varia
ble is counting the rows of our output and the " }{TEXT 0 1 "j" }
{TEXT -1 51 " index variable is counting the \"columns\". And the " }
{TEXT 0 1 "i" }{TEXT -1 65 " index variable from the \"outer loop\" is
the final value for the " }{TEXT 0 1 "j" }{TEXT -1 27 " index in the \+
\"inner loop\"." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}
{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 182 "Here is \+
a procedure that allows us to conveniently print out as many lines of \+
Pascal's triangle as we want. (Notice the multiple levels of indentat
ion to make this easier to read.) " }}{EXCHG {PARA 0 "> " 0 ""
{MPLTEXT 1 0 20 "pascal := proc(m, n)" }}{PARA 0 "> " 0 "" {MPLTEXT 1
0 19 " local i, j, x, y;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 22 " for \+
i from m to n do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 23 " expand( (x
+y)^i );" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 43 " seq( coeffs(op(j, \+
%)), j=1..nops(%) );" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 16 " print(
% );" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 5 " od;" }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 4 "end;" }}}{PARA 0 "" 0 "" {TEXT -1 89 "Let us try it ou
t. (On my computer screen I can fit up to 18 lines of Pascal's triangl
e.)" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "pascal(0,8);" }}}
{PARA 0 "" 0 "" {TEXT -1 35 "Notice how this procedure uses the " }
{TEXT 0 5 "print" }{TEXT -1 241 " command to print out one line of Pas
cal's triangle for each loop through the for-loop. Normally a procedur
e only has one output, which is the last line executed by the procedur
e (the \"return value\" of the procedure). But this procedure has " }
{TEXT 0 1 "n" }{TEXT -1 306 " lines of output. These extra lines of ou
tput have a name, they are called side effects. Anything else that a \+
procedure does besides returning its \"return value\" is called a side
effect. (You may wonder just what the return value of this procedure \+
is. We will look at that question later in this chapter.)" }}{EXCHG
{PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }
}{PARA 0 "" 0 "" {TEXT 259 8 "Exercise" }{TEXT -1 43 ": Modify the pro
cedure so that it uses the " }{TEXT 0 8 "binomial" }{TEXT -1 24 " comm
and instead of the " }{TEXT 0 6 "expand" }{TEXT -1 5 " and " }{TEXT 0
5 "coeff" }{TEXT -1 10 " commands." }}{EXCHG {PARA 0 "> " 0 ""
{MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 5 "" 0 "" {TEXT -1 37 "5.3.3. Exam
ple 3: Periodic extensions" }}{PARA 0 "" 0 "" {TEXT -1 128 "Our third \+
example will use a while-loop. We show how to take an arbitrary functi
on g defined on an interval between two numbers " }{XPPEDIT 18 0 "a;"
"6#%\"aG" }{TEXT -1 5 " and " }{XPPEDIT 18 0 "b;" "6#%\"bG" }{TEXT -1
2 ", " }{XPPEDIT 18 0 "a < b;" "6#2%\"aG%\"bG" }{TEXT -1 80 ", and pro
duce a function f that is periodic on the whole real line, with period
" }{XPPEDIT 18 0 "p = b-a;" "6#/%\"pG,&%\"bG\"\"\"%\"aG!\"\"" }{TEXT
-1 40 ", and is equal to the original function " }{XPPEDIT 18 0 "g;" "
6#%\"gG" }{TEXT -1 9 " between " }{XPPEDIT 18 0 "a;" "6#%\"aG" }{TEXT
-1 5 " and " }{XPPEDIT 18 0 "b;" "6#%\"bG" }{TEXT -1 34 ". This new fu
nction is called the " }{TEXT 260 18 "periodic extension" }{TEXT -1
26 " of the original function." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1
0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1
190 "Before showing how to use Maple to define the periodic extension,
let us try to describe it in words. We start with what we might think
of as a segment of a function g, defined just between " }{XPPEDIT 18
0 "a;" "6#%\"aG" }{TEXT -1 5 " and " }{XPPEDIT 18 0 "b;" "6#%\"bG" }
{TEXT -1 138 ". The periodic extension f will have copies of this segm
ent repeated over and over again on the real line. There will be one c
opy between " }{XPPEDIT 18 0 "b;" "6#%\"bG" }{TEXT -1 5 " and " }
{XPPEDIT 18 0 "b+p;" "6#,&%\"bG\"\"\"%\"pGF%" }{TEXT -1 8 " (where " }
{XPPEDIT 18 0 "p = b-a;" "6#/%\"pG,&%\"bG\"\"\"%\"aG!\"\"" }{TEXT -1
89 " is the length of the interval that g is originally defined on) an
d another copy between " }{XPPEDIT 18 0 "b+p;" "6#,&%\"bG\"\"\"%\"pGF%
" }{TEXT -1 5 " and " }{XPPEDIT 18 0 "b+2*p;" "6#,&%\"bG\"\"\"*&\"\"#F
%%\"pGF%F%" }{TEXT -1 73 ", etc. How should we define the extension f \+
on the \"first\" interval from " }{XPPEDIT 18 0 "b;" "6#%\"bG" }{TEXT
-1 4 " to " }{XPPEDIT 18 0 "b+p;" "6#,&%\"bG\"\"\"%\"pGF%" }{TEXT -1
75 "? Visually, we would just imagine sliding the graph of g from the \+
interval " }{XPPEDIT 18 0 "[a, b];" "6#7$%\"aG%\"bG" }{TEXT -1 24 " ov
er onto the interval " }{XPPEDIT 18 0 "[b, b+p];" "6#7$%\"bG,&F$\"\"\"
%\"pGF&" }{TEXT -1 58 ". We would express this mathematically by sayin
g that, if " }{XPPEDIT 18 0 "x;" "6#%\"xG" }{TEXT -1 7 " is in " }
{XPPEDIT 18 0 "[b, b+p];" "6#7$%\"bG,&F$\"\"\"%\"pGF&" }{TEXT -1 7 ", \+
then " }{XPPEDIT 18 0 "f(x) = g(x-p);" "6#/-%\"fG6#%\"xG-%\"gG6#,&F'\"
\"\"%\"pG!\"\"" }{TEXT -1 61 " (make sure you understand this step). F
or the interval from " }{XPPEDIT 18 0 "b+p;" "6#,&%\"bG\"\"\"%\"pGF%"
}{TEXT -1 4 " to " }{XPPEDIT 18 0 "b+2*p;" "6#,&%\"bG\"\"\"*&\"\"#F%%
\"pGF%F%" }{TEXT -1 58 ", we would again just imagine sliding the grap
h of g from " }{XPPEDIT 18 0 "[a, b];" "6#7$%\"aG%\"bG" }{TEXT -1 9 " \+
over to " }{XPPEDIT 18 0 "[b+p, b+2*p];" "6#7$,&%\"bG\"\"\"%\"pGF&,&F%
F&*&\"\"#F&F'F&F&" }{TEXT -1 9 ". And if " }{XPPEDIT 18 0 "x;" "6#%\"x
G" }{TEXT -1 7 " is in " }{XPPEDIT 18 0 "[b+p, b+2*p];" "6#7$,&%\"bG\"
\"\"%\"pGF&,&F%F&*&\"\"#F&F'F&F&" }{TEXT -1 20 ", then we would let "
}{XPPEDIT 18 0 "f(x) = g(x-2*p);" "6#/-%\"fG6#%\"xG-%\"gG6#,&F'\"\"\"*
&\"\"#F,%\"pGF,!\"\"" }{TEXT -1 39 ". In general, for any positive int
eger " }{XPPEDIT 18 0 "k;" "6#%\"kG" }{TEXT -1 5 ", if " }{XPPEDIT 18
0 "x;" "6#%\"xG" }{TEXT -1 7 " is in " }{XPPEDIT 18 0 "[b+k*p, b+(k+1)
*p];" "6#7$,&%\"bG\"\"\"*&%\"kGF&%\"pGF&F&,&F%F&*&,&F(F&F&F&F&F)F&F&"
}{TEXT -1 7 ", then " }{XPPEDIT 18 0 "f(x) = g(x-(k+1)*p);" "6#/-%\"fG
6#%\"xG-%\"gG6#,&F'\"\"\"*&,&%\"kGF,F,F,F,%\"pGF,!\"\"" }{TEXT -1 22 "
. In short, to define " }{XPPEDIT 18 0 "f(x);" "6#-%\"fG6#%\"xG" }
{TEXT -1 5 " for " }{XPPEDIT 18 0 "b < x;" "6#2%\"bG%\"xG" }{TEXT -1
29 " we need to keep subtracting " }{XPPEDIT 18 0 "p;" "6#%\"pG" }
{TEXT -1 6 " from " }{XPPEDIT 18 0 "x;" "6#%\"xG" }{TEXT -1 39 " until
we get a number that is between " }{XPPEDIT 18 0 "a;" "6#%\"aG" }
{TEXT -1 5 " and " }{XPPEDIT 18 0 "b;" "6#%\"bG" }{TEXT -1 70 ", and t
hen use that number to evaluate g. This process of subtracting " }
{XPPEDIT 18 0 "p;" "6#%\"pG" }{TEXT -1 8 "'s from " }{XPPEDIT 18 0 "x;
" "6#%\"xG" }{TEXT -1 31 " until we get a number between " }{XPPEDIT
18 0 "a;" "6#%\"aG" }{TEXT -1 5 " and " }{XPPEDIT 18 0 "b;" "6#%\"bG"
}{TEXT -1 79 " is exactly what a while-loop can do for us. Since we do
not know the value of " }{XPPEDIT 18 0 "x;" "6#%\"xG" }{TEXT -1 40 " \+
ahead of time, we do not know how many " }{XPPEDIT 18 0 "p;" "6#%\"pG
" }{TEXT -1 132 "'s we need to subtract, but while-loops are exactly w
hat we should use when we want to iterate something an unknown number \+
of times." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT
261 8 "Exercise" }{TEXT -1 33 ": Figure out how we would define " }
{XPPEDIT 18 0 "f(x);" "6#-%\"fG6#%\"xG" }{TEXT -1 5 " for " }{XPPEDIT
18 0 "x < a;" "6#2%\"xG%\"aG" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0
"" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "
" {TEXT -1 4 "Let " }{TEXT 0 1 "g" }{TEXT -1 29 " be a Maple function \+
and let " }{TEXT 0 1 "a" }{TEXT -1 5 " and " }{TEXT 0 1 "b" }{TEXT -1
21 " be two numbers with " }{TEXT 0 3 "a** " 0 "" {MPLTEXT
1 0 12 "f := proc(x)" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 10 " local y;
" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 9 " y := x;" }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 34 " while y >= b do y := y-(b-a) od;" }}{PARA 0 "> " 0
"" {MPLTEXT 1 0 34 " while y < a do y := y+(b-a) od;" }}{PARA 0 "> \+
" 0 "" {MPLTEXT 1 0 7 " g(y);" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "en
d;" }}}{PARA 0 "" 0 "" {TEXT -1 19 "This definition of " }{TEXT 0 1 "f
" }{TEXT -1 15 " works for any " }{TEXT 0 1 "g" }{TEXT -1 2 ", " }
{TEXT 0 1 "a" }{TEXT -1 5 " and " }{TEXT 0 1 "b" }{TEXT -1 25 ". Let u
s define specific " }{TEXT 0 1 "g" }{TEXT -1 2 ", " }{TEXT 0 1 "a" }
{TEXT -1 6 ", and " }{TEXT 0 1 "b" }{TEXT -1 1 "." }}{EXCHG {PARA 0 ">
" 0 "" {MPLTEXT 1 0 14 "g := x -> x^2;" }}}{EXCHG {PARA 0 "> " 0 ""
{MPLTEXT 1 0 16 "a := -2; b:= 3; " }}}{PARA 0 "" 0 "" {TEXT -1 36 "Now
graph the periodic extension of " }{TEXT 0 1 "g" }{TEXT -1 1 "." }}
{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 64 "plot( f, -7..18, scaling=con
strained, discont=true, color=red );" }}}{PARA 0 "" 0 "" {TEXT -1 44 "
We should note here that if we try to graph " }{TEXT 0 1 "f" }{TEXT
-1 48 " as an expression, then we get an error message." }}{EXCHG
{PARA 0 "> " 0 "" {MPLTEXT 1 0 69 "plot( f(x), x=-7..18, scaling=const
rained, discont=true, color=red );" }}}{PARA 0 "" 0 "" {TEXT -1 48 "Si
milarly, if we try to look at the formula for " }{TEXT 0 1 "f" }{TEXT
-1 49 " as an expression, we get the same error message." }}{EXCHG
{PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "f(x);" }}}{PARA 0 "" 0 "" {TEXT -1
88 "We will find out how to fix this later in this chapter. For now, h
ere is a way to graph " }{TEXT 0 1 "f" }{TEXT -1 40 " as an expression
if you really want to." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 69 "p
lot('f(x)', x=-7..18, scaling=constrained, discont=true, color=red);"
}}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 ""
{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 265 8 "Exercise" }{TEXT -1 16 ":
With the same " }{TEXT 0 1 "g" }{TEXT -1 52 " as in the last example,
try changing the values of " }{TEXT 0 1 "a" }{TEXT -1 5 " and " }
{TEXT 0 1 "b" }{TEXT -1 46 ". How does this change the periodic extens
ion?" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 ""
{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 262 8 "Exercise" }{TEXT -1 60 ":
Try defining periodic extensions for some other functions " }{TEXT 0
1 "g" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}
{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 263 8 "Exercise
" }{TEXT -1 22 ": How are we defining " }{TEXT 0 8 "f(a+k*p)" }{TEXT
-1 17 " for any integer " }{TEXT 0 1 "k" }{TEXT -1 59 "? What other re
asonable choices are there for the value of " }{TEXT 0 8 "f(a+k*p)" }
{TEXT -1 1 "?" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA
0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 28 "Recall that a f
unction f is " }{TEXT 275 4 "even" }{TEXT -1 12 " if for all " }
{XPPEDIT 18 0 "x;" "6#%\"xG" }{TEXT -1 17 " it is true that " }
{XPPEDIT 18 0 "f(-x) = f(x);" "6#/-%\"fG6#,$%\"xG!\"\"-F%6#F(" }{TEXT
-1 67 ". A function is even if its graph is symmetric with respect to \+
the " }{XPPEDIT 18 0 "y;" "6#%\"yG" }{TEXT -1 33 "-axis. Recall that a
function is " }{TEXT 276 3 "odd" }{TEXT -1 12 " if for all " }
{XPPEDIT 18 0 "x;" "6#%\"xG" }{TEXT -1 17 " it is true that " }
{XPPEDIT 18 0 "f(-x) = -f(x);" "6#/-%\"fG6#,$%\"xG!\"\",$-F%6#F(F)" }
{TEXT -1 165 ". A function is odd if its graph is symmetric with respe
ct to the origin, meaning that if we rotate the graph of f by 180 degr
ees, then the graph remains unchanged. " }}{PARA 0 "" 0 "" {TEXT -1 0
"" }}{PARA 0 "" 0 "" {TEXT -1 3 "If " }{XPPEDIT 18 0 "b;" "6#%\"bG" }
{TEXT -1 91 " is a positive number and we define the periodic extensio
n of a function g on the interval " }{XPPEDIT 18 0 "[0, b];" "6#7$\"\"
!%\"bG" }{TEXT -1 177 ", then the periodic extension will be an odd fu
nction for some g, an even function for some other g, and neither even
nor odd for most g. For example, the periodic extension of " }
{XPPEDIT 18 0 "sin(x);" "6#-%$sinG6#%\"xG" }{TEXT -1 17 " on the inter
val " }{XPPEDIT 18 0 "[0, Pi];" "6#7$\"\"!%#PiG" }{TEXT -1 21 " is an \+
even function." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "g := x -> \+
sin(x);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "a:=0; b:=Pi;" }}
}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 23 "plot( f, -3*Pi..3*Pi );" }}
}{PARA 0 "" 0 "" {TEXT -1 30 "And the periodic extension of " }
{XPPEDIT 18 0 "cos(x);" "6#-%$cosG6#%\"xG" }{TEXT -1 17 " on the inter
val " }{XPPEDIT 18 0 "[0, Pi];" "6#7$\"\"!%#PiG" }{TEXT -1 20 " is an \+
odd function." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "g := x -> c
os(x);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "a:=0; b:=Pi;" }}}
{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 48 "plot( f, -2*Pi..2*Pi, discon
t=true, color=red );" }}}{PARA 0 "" 0 "" {TEXT -1 133 "There is a way \+
to define a periodic extension so that it is always even or always odd
, no matter what the function g on the interval " }{XPPEDIT 18 0 "[0, \+
b];" "6#7$\"\"!%\"bG" }{TEXT -1 174 " looks like. The following proced
ure defines the even periodic extension of a function g on the interva
l from 0 to b. The even extension defined by this procedure has period
" }{XPPEDIT 18 0 "2*b;" "6#*&\"\"#\"\"\"%\"bGF%" }{TEXT -1 78 ". (Not
ice the use of an anonymous function in the last line of the procedure
.)" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "f_even := proc(x)" }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 10 " local y;" }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 9 " y := x;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 32 " whil
e y >= b do y := y-2*b od;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 32 " whi
le y < -b do y := y+2*b od;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 42 " ( \+
z -> piecewise(z<0, g(-z), g(z)) )(y);" }}{PARA 0 "> " 0 "" {MPLTEXT
1 0 4 "end;" }}}{PARA 0 "" 0 "" {TEXT -1 36 "The next procedure define
s the odd, " }{XPPEDIT 18 0 "2*b;" "6#*&\"\"#\"\"\"%\"bGF%" }{TEXT -1
62 " periodic extension of a function g on the interval from 0 to " }
{XPPEDIT 18 0 "b;" "6#%\"bG" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "
" {MPLTEXT 1 0 16 "f_odd := proc(x)" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0
10 " local y;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 9 " y := x;" }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 32 " while y >= b do y := y-2*b od;" }
}{PARA 0 "> " 0 "" {MPLTEXT 1 0 32 " while y < -b do y := y+2*b od;"
}}{PARA 0 "> " 0 "" {MPLTEXT 1 0 43 " ( z -> piecewise(z<0, -g(-z), g
(z)) )(y);" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "end;" }}}{PARA 0 "" 0
"" {TEXT -1 79 "Let us try some examples using these two procedures. H
ere are the even and odd " }{XPPEDIT 18 0 "2*Pi;" "6#*&\"\"#\"\"\"%#Pi
GF%" }{TEXT -1 24 " periodic extensions of " }{XPPEDIT 18 0 "sin(x);"
"6#-%$sinG6#%\"xG" }{TEXT -1 17 " on the interval " }{XPPEDIT 18 0 "[0
, Pi];" "6#7$\"\"!%#PiG" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 ""
{MPLTEXT 1 0 9 "g := sin;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8
"b := Pi;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 28 "plot( f_even, \+
-3*Pi..3*Pi );" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 28 "plot( f_o
dd, -3*Pi..3*Pi );" }}}{PARA 0 "" 0 "" {TEXT -1 26 "Here are the even
and odd " }{XPPEDIT 18 0 "2*Pi;" "6#*&\"\"#\"\"\"%#PiGF%" }{TEXT -1
24 " periodic extensions of " }{XPPEDIT 18 0 "cos(x);" "6#-%$cosG6#%\"
xG" }{TEXT -1 17 " on the interval " }{XPPEDIT 18 0 "[0, Pi];" "6#7$\"
\"!%#PiG" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "g
:= cos;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "b := Pi;" }}}
{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 28 "plot( f_even, -3*Pi..3*Pi );
" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 28 "plot( f_odd, -3*Pi..3*
Pi );" }}}{PARA 0 "" 0 "" {TEXT -1 34 "Let us try the identity functio
n, " }{XPPEDIT 18 0 "g(x) = x;" "6#/-%\"gG6#%\"xGF'" }{TEXT -1 17 " on
the interval " }{XPPEDIT 18 0 "[0, 1];" "6#7$\"\"!\"\"\"" }{TEXT -1
1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "g := x -> x;" }}}
{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "b := 1;" }}}{EXCHG {PARA 0 ">
" 0 "" {MPLTEXT 1 0 22 "plot( f_even, -3..3 );" }}}{EXCHG {PARA 0 "> \+
" 0 "" {MPLTEXT 1 0 22 "plot( f_odd, -3..3 );" }}}{EXCHG {PARA 0 "> \+
" 0 "" {MPLTEXT 1 0 17 "plot( f, -3..3 );" }}}{PARA 0 "" 0 "" {TEXT
-1 63 "Let us try a non symmetric piece of a parabola on the interval \+
" }{XPPEDIT 18 0 "[0, 1];" "6#7$\"\"!\"\"\"" }{TEXT -1 1 "." }}{EXCHG
{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "g := x -> (x-1/4)^2;" }}}{EXCHG
{PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "b := 1;" }}}{EXCHG {PARA 0 "> " 0 "
" {MPLTEXT 1 0 22 "plot( f_even, -3..3 );" }}}{EXCHG {PARA 0 "> " 0 "
" {MPLTEXT 1 0 47 "plot( f_odd, -3..3, discont=true, color=red );" }}
}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 42 "plot( f, -3..3, discont=tru
e, color=red );" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}
{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 256 8 "Exercise
" }{TEXT -1 73 ": Try defining even and odd periodic extensions for so
me other functions " }{TEXT 0 1 "g" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "
> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0
"" 0 "" {TEXT 268 8 "Exercise" }{TEXT -1 49 ": Part (a) Under what con
ditions on the function " }{TEXT 0 1 "g" }{TEXT -1 19 " will the funct
ion " }{TEXT 0 5 "f_odd" }{TEXT -1 24 " be continuous at 0 and " }
{TEXT 0 1 "b" }{TEXT -1 40 "? Under what conditions on the function "
}{TEXT 0 1 "g" }{TEXT -1 19 " will the function " }{TEXT 0 6 "f_even"
}{TEXT -1 24 " be continuous at 0 and " }{TEXT 0 1 "b" }{TEXT -1 1 "?
" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 ""
{TEXT -1 47 "Part (b) Under what conditions on the function " }{TEXT
0 1 "g" }{TEXT -1 20 " will the functions " }{TEXT 0 5 "f_odd" }{TEXT
-1 5 " and " }{TEXT 0 1 "f" }{TEXT -1 29 " be the same function (where
" }{TEXT 0 1 "f" }{TEXT -1 8 " is the " }{TEXT 0 1 "b" }{TEXT -1 23 "
periodic extension of " }{TEXT 0 1 "g" }{TEXT -1 27 " on the interval
from 0 to " }{TEXT 0 1 "b" }{TEXT -1 41 "). Under what conditions on \+
the function " }{TEXT 0 1 "g" }{TEXT -1 20 " will the functions " }
{TEXT 0 6 "f_even" }{TEXT -1 5 " and " }{TEXT 0 1 "f" }{TEXT -1 22 " b
e the same function." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}
{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 52 "Here is a
n interesting example that uses a parabola." }}{EXCHG {PARA 0 "> " 0 "
" {MPLTEXT 1 0 20 "g := x -> 4*x*(1-x);" }}}{EXCHG {PARA 0 "> " 0 ""
{MPLTEXT 1 0 7 "b := 1;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "
plot( f_odd, -4..4 );" }}}{PARA 0 "" 0 "" {TEXT -1 54 "Let us compare \+
this periodic function to a sine curve." }}{EXCHG {PARA 0 "> " 0 ""
{MPLTEXT 1 0 39 "plot( [ f_odd, x->sin(Pi*x) ], -2..2 );" }}}{PARA 0 "
" 0 "" {TEXT -1 118 "Notice how amazingly similar the two functions ar
e. The following parametric graph uses the odd periodic extension of \+
" }{TEXT 0 1 "g" }{TEXT -1 27 ". Which is the real circle?" }}{EXCHG
{PARA 0 "> " 0 "" {MPLTEXT 1 0 39 "plot( [ [f_odd, t->f_odd(t-1/2), 0.
.2]," }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 " [cos, sin, 0..2*Pi]
], " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 28 " scaling=constrained )
;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 ""
{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 269 8 "Exercise" }{TEXT -1 44 ":
Given a function g defined on an interval " }{XPPEDIT 18 0 "[0, b];"
"6#7$\"\"!%\"bG" }{TEXT -1 20 ", write a procedure " }{TEXT 0 9 "f_odd
even" }{TEXT -1 16 " that defines a " }{XPPEDIT 18 0 "4*b;" "6#*&\"\"%
\"\"\"%\"bGF%" }{TEXT -1 103 " periodic extension of g that is odd and
such that the horizontal shift of the extension by the amount " }
{XPPEDIT 18 0 "b;" "6#%\"bG" }{TEXT -1 39 " is even. Similarly, write \+
a procedure " }{TEXT 0 9 "f_evenodd" }{TEXT -1 16 " that defines a " }
{XPPEDIT 18 0 "4*b;" "6#*&\"\"%\"\"\"%\"bGF%" }{TEXT -1 104 " periodic
extension of g that is even and such that the horizontal shift of the
extension by the amount " }{XPPEDIT 18 0 "b;" "6#%\"bG" }{TEXT -1 29
" is odd. (Hint: Think of how " }{TEXT 0 3 "sin" }{TEXT -1 38 " is bui
lt up from just one quarter of " }{TEXT 0 3 "sin" }{TEXT -1 24 "'s gra
ph, the part from " }{TEXT 0 1 "0" }{TEXT -1 4 " to " }{TEXT 0 4 "Pi/2
" }{TEXT -1 24 ", and then think of how " }{TEXT 0 3 "cos" }{TEXT -1
38 " is built up from just one quarter of " }{TEXT 0 3 "cos" }{TEXT
-1 10 "'s graph.)" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}
{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 264 8 "Exercise
" }{TEXT -1 64 ": Make up your own periodic function by extending some
function " }{TEXT 0 1 "g" }{TEXT -1 241 ", and then use your periodic
function in place of the trig functions sin and cos in several parame
tric curves and surfaces like cardiods, spirals, lemniscates, roses, s
phere, torus, etc. Try to come up with a really unusual curve or surfa
ce." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 ""
{TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT
1 {PARA 5 "" 0 "" {TEXT -1 32 "5.3.4. Example 4: Drawing graphs" }}
{PARA 0 "" 0 "" {TEXT -1 218 "Our fourth example uses a for-loop and a
list data structure to draw a graph. Each iterate of the for-loop wil
l compute the coordinates of a point and put the point in a list of po
ints that are to be plotted. Then the " }{TEXT 0 4 "plot" }{TEXT -1
184 " command will be used to draw a graph of all the points in the li
st. This is a fairly common way to create a graph in Maple and this is
fundamentally how all graphs are drawn in Maple." }}{PARA 0 "" 0 ""
{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 89 "The following for-loop co
mputes 9 equally spaced sample points in the interval from 0 to " }
{XPPEDIT 18 0 "2*Pi;" "6#*&\"\"#\"\"\"%#PiGF%" }{TEXT -1 68 ". Each it
erate of the loop computes one sample point, evaluates the " }{TEXT 0
3 "sin" }{TEXT -1 94 " function at the sample point, and puts the orde
red pair of the sample point and the value of " }{TEXT 0 3 "sin" }
{TEXT -1 20 " into a list called " }{TEXT 0 4 "data" }{TEXT -1 11 ". T
hen the " }{TEXT 0 4 "plot" }{TEXT -1 35 " command is used to graph th
e list " }{TEXT 0 4 "data" }{TEXT -1 28 ", and we get a graph of the \+
" }{TEXT 0 3 "sin" }{TEXT -1 23 " function. Notice that " }{TEXT 0 4 "
data" }{TEXT -1 31 " starts off as the empty list, " }{TEXT 0 2 "[]" }
{TEXT -1 24 ", and as each new point " }{TEXT 0 10 "[x,sin(x)]" }
{TEXT -1 66 " is computed, it is put into the list using a command of \+
the form " }{TEXT 0 27 "data:=[op(data),[x,sin(x)]]" }{TEXT -1 60 ". N
otice how this is very much like computing a running sum " }{TEXT 0 1
"s" }{TEXT -1 26 ", where we start off with " }{TEXT 0 1 "s" }{TEXT
-1 123 " equal to the \"empty\" sum, 0, and then as each new term in t
he sum is computed it is added to the sum using a command like " }
{TEXT 0 14 "s:=s+something" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "
" {MPLTEXT 1 0 7 "N := 8;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0
10 "b := 2*Pi;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "data := [
];" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "for n from 0 to N do \+
" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 44 " data := [ op(data), [n*b/N, s
in(n*b/N)] ];" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od;" }}}{PARA 0 ""
0 "" {TEXT -1 362 "Notice that the output from the for-loop is in symb
olic form. Since we are going to just plot the contents of the list, w
e do not need exact symbolic results. So let us modify the for-loop so
that it computes with decimal numbers. When working with loops that m
ight create thousands of points, this could help speed things up. (If \+
we want, we could even use the " }{TEXT 0 6 "evalhf" }{TEXT -1 91 " co
mmand and hardware floating points for even more speed.) First, we nee
d to reinitialize " }{TEXT 0 4 "list" }{TEXT -1 28 " to be the empty l
ist again." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "data := [];" }
}{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "for n from 0 to N do" }}{PARA 0 ">
" 0 "" {MPLTEXT 1 0 58 " data := [ op(data), [evalf(n*b/N), sin(eval
f(n*b/N))] ];" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od;" }}}{PARA 0 ""
0 "" {TEXT -1 8 "Now use " }{TEXT 0 4 "plot" }{TEXT -1 19 " to graph t
he list " }{TEXT 0 4 "data" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "
" {MPLTEXT 1 0 13 "plot( data );" }}}{EXCHG {PARA 0 "> " 0 ""
{MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 ""
{TEXT 270 8 "Exercise" }{TEXT -1 63 ": Modify the above example so tha
t it can be used to graph the " }{TEXT 0 3 "sin" }{TEXT -1 33 " functi
on over any interval from " }{TEXT 0 1 "a" }{TEXT -1 4 " to " }{TEXT
0 1 "b" }{TEXT -1 145 ". (You should suppress the output from the for-
loop in your example, so that you do not fill up the worksheet with an
unwieldy amount of output.)" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0
0 "" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 271 8 "E
xercise" }{TEXT -1 118 ": Modify the above example so that it is easy \+
to change the function that is being graphed. Try graphing the functio
n " }{XPPEDIT 18 0 "f(x) = 3*x^2-2*x-1;" "6#/-%\"fG6#%\"xG,(*&\"\"$\"
\"\"*$F'\"\"#F+F+*&F-F+F'F+!\"\"F+F/" }{TEXT -1 19 " over the interval
" }{XPPEDIT 18 0 "[-2, 3];" "6#7$,$\"\"#!\"\"\"\"$" }{TEXT -1 1 "." }
}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT
-1 0 "" }}{PARA 0 "" 0 "" {TEXT 273 8 "Exercise" }{TEXT -1 69 ": Consi
der the following three execution groups. Each one graphs the " }
{TEXT 0 3 "sin" }{TEXT -1 77 " function and each one uses a slight var
iation on the last for-loop. Suppose " }{TEXT 0 1 "N" }{TEXT -1 148 " \+
is a very large number. How do you think these three execution groups \+
would compare, in terms of speed, with the last for-loop and with each
other?" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "N := 8;" }}{PARA
0 "> " 0 "" {MPLTEXT 1 0 10 "b := 2*Pi;" }}{PARA 0 "> " 0 "" {MPLTEXT
1 0 11 "data := []:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "for n from 0
to N do " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 58 " data := [ op(data), \+
[evalf(n*b/N), evalf(sin(n*b/N))] ];" }}{PARA 0 "> " 0 "" {MPLTEXT 1
0 3 "od:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "plot( data );" }}}
{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT
-1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "N := 8;" }}{PARA 0
"> " 0 "" {MPLTEXT 1 0 10 "b := 2*Pi;" }}{PARA 0 "> " 0 "" {MPLTEXT 1
0 11 "data := []:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "n := 'n':" }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "x := evalf( n*b/N );" }}{PARA 0 "> \+
" 0 "" {MPLTEXT 1 0 21 "for n from 0 to N do " }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 36 " data := [ op(data), [x, sin(x)] ];" }}{PARA 0 "> "
0 "" {MPLTEXT 1 0 3 "od:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "plot( d
ata );" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0
"" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "N := 8;" }
}{PARA 0 "> " 0 "" {MPLTEXT 1 0 10 "b := 2*Pi;" }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 11 "data := []:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "n :=
'n':" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 18 "x := evalf( b/N );" }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "for n from 0 to N do " }}{PARA 0 ">
" 0 "" {MPLTEXT 1 0 11 " p := n*x;" }}{PARA 0 "> " 0 "" {MPLTEXT 1
0 36 " data := [ op(data), [p, sin(p)] ];" }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 3 "od:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "plot( data )
;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 ""
{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 136 "The following for-loop c
omputes five equally space points on the circumference of the unit cir
cle and puts these point in a list called " }{TEXT 0 4 "data" }{TEXT
-1 25 ". Notice that once again " }{TEXT 0 4 "data" }{TEXT -1 31 " sta
rts off as the empty list, " }{TEXT 0 2 "[]" }{TEXT -1 24 ", and as ea
ch new point " }{TEXT 0 5 "[x,y]" }{TEXT -1 65 " is computed it is put
into the list using a command of the form " }{TEXT 0 22 "data:=[op(da
ta),[x,y]]" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7
"N := 5;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "data := [];" }}{PARA 0
"> " 0 "" {MPLTEXT 1 0 20 "for n from 0 to N do" }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 69 " data := [ op(data), [cos(evalf(n*2*Pi/N)), sin(eval
f(n*2*Pi/N))] ];" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od;" }}}{PARA 0
"" 0 "" {TEXT -1 93 "Notice how the list of points grew by one point w
ith each iteration of the loop. Now use the " }{TEXT 0 4 "plot" }
{TEXT -1 26 " command to plot the list." }}{EXCHG {PARA 0 "> " 0 ""
{MPLTEXT 1 0 47 "plot( data, style=point, scaling=constrained );" }}}
{PARA 0 "" 0 "" {TEXT -1 43 "Now plot it with lines connecting the dot
s." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 46 "plot( data, style=line
, scaling=constrained );" }}}{PARA 0 "" 0 "" {TEXT -1 38 "In the next \+
for-loop we use the names " }{TEXT 0 1 "x" }{TEXT -1 5 " and " }{TEXT
0 1 "y" }{TEXT -1 344 " to represent the calculation of the coordinate
s of each of the points. Convince yourself that the next execution gro
up does exactly the same calculation as the previous execution group. \+
The reason for rewriting it like this is to try and make the execution
group easier to read. (And this time we are suppressing the output fr
om the for-loop.)" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "N := 5;
" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "data := [];" }}{PARA 0 "> " 0 "
" {MPLTEXT 1 0 41 "n := 'n'; # be sure that n is unassigned" }}{PARA
0 "> " 0 "" {MPLTEXT 1 0 27 "theta := evalf( n*2*Pi/N );" }}{PARA 0 ">
" 0 "" {MPLTEXT 1 0 16 "x := cos(theta);" }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 16 "y := sin(theta);" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0
20 "for n from 0 to N do" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 32 " data
:= [ op(data), [x, y] ];" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od:" }}
}{PARA 0 "" 0 "" {TEXT -1 66 "Plot the points again, to verify that it
was the same calculation." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0
46 "plot( data, style=line, scaling=constrained );" }}}{PARA 0 "" 0 "
" {TEXT -1 64 "Now go back to the last execution group and change the \+
value of " }{TEXT 0 1 "N" }{TEXT -1 148 " from 5 to 7 or 9. The execut
ion group will plot 7 or 9 equally spaced points on the circumference \+
of the unit circle. Try any positive integer for " }{TEXT 0 1 "N" }
{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA
0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 270 "Here is a vari
ation on this example. The next execution group plots 5 equally spaced
points on the circumference of the unit circle, but they are not comp
uted in sequential order around the circle. Try to figure out exactly \+
what this version computes and how it does it." }}{EXCHG {PARA 0 "> "
0 "" {MPLTEXT 1 0 7 "N := 5;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "J :=
2;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "data := []:" }}{PARA 0 "> "
0 "" {MPLTEXT 1 0 9 "n := 'n':" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 18 "t
heta := J*2*Pi/N;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 18 "x := cos(n*the
ta);" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 18 "y := sin(n*theta);" }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "for n from 0 to N do" }}{PARA 0 "> \+
" 0 "" {MPLTEXT 1 0 32 " data := [ op(data), [x, y] ];" }}{PARA 0 ">
" 0 "" {MPLTEXT 1 0 3 "od:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 46 "plot
( data, style=line, scaling=constrained );" }}}{PARA 0 "" 0 "" {TEXT
-1 13 "Try changing " }{TEXT 0 1 "N" }{TEXT -1 10 " to 7 and " }{TEXT
0 1 "J" }{TEXT -1 40 " to 3. Try several different values for " }
{TEXT 0 1 "N" }{TEXT -1 5 " and " }{TEXT 0 1 "J" }{TEXT -1 1 "." }}
{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT
-1 0 "" }}{PARA 0 "" 0 "" {TEXT 272 8 "Exercise" }{TEXT -1 100 ": Conv
ert the last execution group into a procedure that takes two positive \+
integer parameters, the " }{TEXT 0 1 "N" }{TEXT -1 5 " and " }{TEXT 0
1 "J" }{TEXT -1 104 ", and draws the appropriate graph. This will make
it a lot easier to try out different parameter values." }}{EXCHG
{PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }
}{PARA 0 "" 0 "" {TEXT -1 71 "One more variation on this example. This
version has three parameters, " }{TEXT 0 1 "N" }{TEXT -1 2 ", " }
{TEXT 0 1 "J" }{TEXT -1 6 ", and " }{TEXT 0 1 "K" }{TEXT -1 177 ". Try
to figure out exactly what this execution group is doing. (Hint: It d
oes pretty much the same thing as the previous version, but it does it
twice per iterate of the loop.)" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT
1 0 21 "N, J, K := 36, 21, 9;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "da
ta := []:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "n := 'n':" }}{PARA 0 ">
" 0 "" {MPLTEXT 1 0 19 "theta1 := J*2*Pi/N;" }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 19 "theta2 := K*2*Pi/N;" }}{PARA 0 "> " 0 "" {MPLTEXT 1
0 33 "x1, y1 := cos(n*(theta1+theta2))," }}{PARA 0 "> " 0 "" {MPLTEXT
1 0 33 " sin(n*(theta1+theta2));" }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 40 "x2, y2 := cos(n*(theta1+theta2)+theta1)," }}{PARA 0 "
> " 0 "" {MPLTEXT 1 0 40 " sin(n*(theta1+theta2)+theta1);" }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "for n from 0 to N do" }}{PARA 0 "> \+
" 0 "" {MPLTEXT 1 0 44 " data := [ op(data), [x1, y1], [x2, y2] ];"
}}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od:" }}{PARA 0 "> " 0 "" {MPLTEXT
1 0 57 "plot( data, style=line, scaling=constrained, axes=none );" }}}
{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT
-1 40 "Here are some values for the parameters " }{TEXT 0 1 "N" }
{TEXT -1 2 ", " }{TEXT 0 1 "J" }{TEXT -1 5 " and " }{TEXT 0 1 "K" }
{TEXT -1 26 " that produce nice graphs." }}{PARA 0 "" 0 "" {TEXT -1 9
"15, 8, 13" }}{PARA 0 "" 0 "" {TEXT -1 8 "28,19,15" }}{PARA 0 "" 0 ""
{TEXT -1 8 "39,33,27" }}{PARA 0 "" 0 "" {TEXT -1 8 "19,13,11" }}
{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT
-1 0 "" }}{PARA 0 "" 0 "" {TEXT 256 9 "Exercise:" }{TEXT -1 194 " Part
(a) Convert the last execution group into a procedure that has three \+
positive integer parameters and draws the appropriate graph. Call your
procedure with a number of different parameters." }}{EXCHG {PARA 0 ">
" 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 191 "Part (b): C
onvert your procedure from Part(a) into a procedure that takes no inpu
t parameters and generates the three integers it needs randomly. Your \+
procedure should use the Maple function " }{TEXT 0 4 "rand" }{TEXT -1
49 " to generate the random integers (the expression " }{TEXT 0 12 "ra
nd(a..b)()" }{TEXT -1 7 ", with " }{TEXT 274 4 "both" }{TEXT -1 58 " s
ets of parentheses, generates a random integer between " }{TEXT 0 1 "
a" }{TEXT -1 5 " and " }{TEXT 0 1 "b" }{TEXT -1 183 "). Have the proce
dure print out the values of the randomly chosen integers and then dra
w the appropriate graph. Run this procedure many times. You should get
some very elegant graphs." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0
"" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 ""
{MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 5 "" 0 "" {TEXT -1 33 "5.3.5. Exam
ple 5: Butterfly curve" }}{PARA 0 "" 0 "" {TEXT -1 128 "Our fifth exam
ple uses a for-loop and a data structure (a list) to draw a fairly com
plex graph, a version of a butterfly curve. " }}{PARA 0 "" 0 "" {TEXT
-1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 275 "The curve that we draw is not \+
in fact a curve. Instead we will be plotting points, thousands of poin
ts, and not connecting them together with line segments. The points we
will be plotting will be computed by a for-loop and placed in a list \+
(as in the last example). Then the " }{TEXT 0 4 "plot" }{TEXT -1 233 "
command will be used to draw a graph of all the points in the list (w
ithout connecting them together with line segments). Almost all of the
work in this example is in computing the coordinates of all the point
s that we want to plot." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 ""
0 "" {TEXT -1 291 "The loop in this example is fairly computationally \+
intensive. You should save all of your work before executing it, in ca
se it takes too long for it to compute on your computer (do not try th
is on a Pentium, use at least a Pentium II). If your computer is real
ly fast, you can try changing " }{TEXT 0 1 "N" }{TEXT -1 5 ". Try" }
{TEXT 0 9 " N:=21000" }{TEXT -1 5 ", or " }{TEXT 0 8 "N:=41900" }
{TEXT -1 64 " (which should run for a pretty long time). Different val
ues of " }{TEXT 0 1 "N" }{TEXT -1 33 " give different butterfly curves
." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 63 "r := phi -> ( exp(cos(p
hi))-2*cos(4*phi) )*sin(99999999*phi)^4;" }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 55 "N := 11500; # Number of points to comput
e." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 52 "h := evalf(2*Pi/N); # St
ep size between points." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 43 "n := 'n'
; # Just to be safe." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0
58 "x := r(n*h)*sin(n*h); # x-coord of a point on the curve" }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 58 "y := r(n*h)*cos(n*h); # y-coord \+
of a point on the curve" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 52 "data := \+
[]; # Start with an empty list." }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 63 "for n from 1 to N do # This do-loop computes the \+
butterfly." }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 51 " data := [ op(data)
, [evalhf( x ), evalhf( y )] ]" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "od
:" }}}{PARA 0 "" 0 "" {TEXT -1 62 "Now that we have computed our list \+
of points, let us graph it." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0
53 "plot( data, style=point, symbol=point, color=black );" }}}{PARA 0
"" 0 "" {TEXT -1 47 "The original reference for butterfly curves is "
}{TEXT 266 19 "The Butterfly Curve" }{TEXT -1 164 ", by Temple H. Fay,
in The American Mathematical Monthly, Volume 96, Issue 5 (May, 1989),
pages 442-443. The version of the butterfly curve in this example is \+
from " }{TEXT 267 20 "A Study in Step Size" }{TEXT -1 89 ", by Temple \+
H. Fay, in Mathematics Magazine, Volume 70, No. 2, April 1997, pages 1
16-117." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0
"" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}
{SECT 1 {PARA 5 "" 0 "" {TEXT -1 28 "5.3.6. Example 6: Animations" }}
{PARA 0 "" 0 "" {TEXT -1 89 "Our last example is the use of a for-loop
to compute the frames of an animation. Maple's " }{TEXT 0 7 "animate
" }{TEXT -1 276 " command is limited in the kind of animations it can \+
draw. For example, it cannot animate the graphs of equations. There is
another way to create animations in which we use a for-loop to create
and label a sequence of graphs (the frames) and then we use a special
form of the " }{TEXT 0 7 "display" }{TEXT -1 96 " command to turn the
sequence of graphs into an animation. Here is an example of this tech
nique." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1
148 "First, a for-loop is used to create and name, using concatenated \+
names, 51 two-dimensional graphs, each with a parameter slightly chang
ed. Then the " }{TEXT 0 7 "display" }{TEXT -1 291 " command is used to
\"sequence\" the 51 graphs into a short movie. To view the movie, aft
er the first graph is visible, place the cursor on the graph. Some VCR
type buttons will appear at the top of the Maple window. Click on the
\"play\" button. (The for-loop takes a little while to complete.)" }}
{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "x := 'x': y := 'y':" }}
{PARA 0 "> " 0 "" {MPLTEXT 1 0 23 "for i from -20 to 30 do" }}{PARA 0
"> " 0 "" {MPLTEXT 1 0 53 " p||i := plots[implicitplot]( x^3+y^3-5*x*
y = 1-i/8," }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 65 " x=-3..
3, y=-3..3, numpoints=800, tickmarks=[2,2] )" }}{PARA 0 "> " 0 ""
{MPLTEXT 1 0 3 "od:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 48 "plot
s[display]( p||(-20..30), insequence=true );" }}}{EXCHG {PARA 0 "> "
0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG
{PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}}}{MARK "4 2 0 0" 0 }
{VIEWOPTS 1 1 0 1 1 1803 1 1 1 1 }{PAGENUMBERS 1 1 2 33 1 1 }
**