scripting and programming
TRANSCRIPT
-
7/27/2019 Scripting and Programming
1/415
-
7/27/2019 Scripting and Programming
2/415
-
7/27/2019 Scripting and Programming
3/415
-
7/27/2019 Scripting and Programming
4/415
-
7/27/2019 Scripting and Programming
5/415
-
7/27/2019 Scripting and Programming
6/415
-
7/27/2019 Scripting and Programming
7/415
-
7/27/2019 Scripting and Programming
8/415
-
7/27/2019 Scripting and Programming
9/415
-
7/27/2019 Scripting and Programming
10/415
-
7/27/2019 Scripting and Programming
11/415
-
7/27/2019 Scripting and Programming
12/415
-
7/27/2019 Scripting and Programming
13/415
-
7/27/2019 Scripting and Programming
14/415
-
7/27/2019 Scripting and Programming
15/415
-
7/27/2019 Scripting and Programming
16/415
-
7/27/2019 Scripting and Programming
17/415
-
7/27/2019 Scripting and Programming
18/415
-
7/27/2019 Scripting and Programming
19/415
-
7/27/2019 Scripting and Programming
20/415
-
7/27/2019 Scripting and Programming
21/415
-
7/27/2019 Scripting and Programming
22/415
2 + 3
().
-
7/27/2019 Scripting and Programming
23/415
-
7/27/2019 Scripting and Programming
24/415
-
7/27/2019 Scripting and Programming
25/415
-
7/27/2019 Scripting and Programming
26/415
-
7/27/2019 Scripting and Programming
27/415
-
7/27/2019 Scripting and Programming
28/415
-
7/27/2019 Scripting and Programming
29/415
-
7/27/2019 Scripting and Programming
30/415
-
7/27/2019 Scripting and Programming
31/415
-
7/27/2019 Scripting and Programming
32/415
-
7/27/2019 Scripting and Programming
33/415
-
7/27/2019 Scripting and Programming
34/415
-
7/27/2019 Scripting and Programming
35/415
-
7/27/2019 Scripting and Programming
36/415
System.out.println(k^2 + k + k + 3);
-
7/27/2019 Scripting and Programming
37/415
-
7/27/2019 Scripting and Programming
38/415
-
7/27/2019 Scripting and Programming
39/415
-
7/27/2019 Scripting and Programming
40/415
-
7/27/2019 Scripting and Programming
41/415
arcsinarccosarctan
-
7/27/2019 Scripting and Programming
42/415
-
7/27/2019 Scripting and Programming
43/415
-
7/27/2019 Scripting and Programming
44/415
-
7/27/2019 Scripting and Programming
45/415
e
-
7/27/2019 Scripting and Programming
46/415
-
7/27/2019 Scripting and Programming
47/415
-
7/27/2019 Scripting and Programming
48/415
-
7/27/2019 Scripting and Programming
49/415
-
7/27/2019 Scripting and Programming
50/415
-
7/27/2019 Scripting and Programming
51/415
-
7/27/2019 Scripting and Programming
52/415
-
7/27/2019 Scripting and Programming
53/415
_
-
7/27/2019 Scripting and Programming
54/415
infin
infin.
omega
e
-
7/27/2019 Scripting and Programming
55/415
omega
-
7/27/2019 Scripting and Programming
56/415
-
7/27/2019 Scripting and Programming
57/415
_
resultResult
trigResult
-
7/27/2019 Scripting and Programming
58/415
-
7/27/2019 Scripting and Programming
59/415
x
'x'
(4.13)
-
7/27/2019 Scripting and Programming
60/415
-
7/27/2019 Scripting and Programming
61/415
-
7/27/2019 Scripting and Programming
62/415
-
7/27/2019 Scripting and Programming
63/415
-
7/27/2019 Scripting and Programming
64/415
-
7/27/2019 Scripting and Programming
65/415
-
7/27/2019 Scripting and Programming
66/415
restart
-
7/27/2019 Scripting and Programming
67/415
:=
==
-
7/27/2019 Scripting and Programming
68/415
-
7/27/2019 Scripting and Programming
69/415
restart
unassign
restart
restart
PiI
-
7/27/2019 Scripting and Programming
70/415
-
7/27/2019 Scripting and Programming
71/415
-
7/27/2019 Scripting and Programming
72/415
-
7/27/2019 Scripting and Programming
73/415
-
7/27/2019 Scripting and Programming
74/415
-
7/27/2019 Scripting and Programming
75/415
-
7/27/2019 Scripting and Programming
76/415
-
7/27/2019 Scripting and Programming
77/415
-
7/27/2019 Scripting and Programming
78/415
-
7/27/2019 Scripting and Programming
79/415
-
7/27/2019 Scripting and Programming
80/415
restart
-
7/27/2019 Scripting and Programming
81/415
-
7/27/2019 Scripting and Programming
82/415
-
7/27/2019 Scripting and Programming
83/415
-
7/27/2019 Scripting and Programming
84/415
-
7/27/2019 Scripting and Programming
85/415
-
7/27/2019 Scripting and Programming
86/415
-
7/27/2019 Scripting and Programming
87/415
-
7/27/2019 Scripting and Programming
88/415
-
7/27/2019 Scripting and Programming
89/415
"This is a string."
[1,2, x, 3.5]
-
7/27/2019 Scripting and Programming
90/415
-
7/27/2019 Scripting and Programming
91/415
-
7/27/2019 Scripting and Programming
92/415
-
7/27/2019 Scripting and Programming
93/415
-
7/27/2019 Scripting and Programming
94/415
-
7/27/2019 Scripting and Programming
95/415
-
7/27/2019 Scripting and Programming
96/415
-
7/27/2019 Scripting and Programming
97/415
-
7/27/2019 Scripting and Programming
98/415
pi
Pi. Pi
-
7/27/2019 Scripting and Programming
99/415
-
7/27/2019 Scripting and Programming
100/415
-
7/27/2019 Scripting and Programming
101/415
-
7/27/2019 Scripting and Programming
102/415
-
7/27/2019 Scripting and Programming
103/415
-
7/27/2019 Scripting and Programming
104/415
-
7/27/2019 Scripting and Programming
105/415
-
7/27/2019 Scripting and Programming
106/415
-
7/27/2019 Scripting and Programming
107/415
-
7/27/2019 Scripting and Programming
108/415
-
7/27/2019 Scripting and Programming
109/415
-
7/27/2019 Scripting and Programming
110/415
-
7/27/2019 Scripting and Programming
111/415
-
7/27/2019 Scripting and Programming
112/415
-
7/27/2019 Scripting and Programming
113/415
-
7/27/2019 Scripting and Programming
114/415
-
7/27/2019 Scripting and Programming
115/415
-
7/27/2019 Scripting and Programming
116/415
-
7/27/2019 Scripting and Programming
117/415
-
7/27/2019 Scripting and Programming
118/415
-
7/27/2019 Scripting and Programming
119/415
-
7/27/2019 Scripting and Programming
120/415
-
7/27/2019 Scripting and Programming
121/415
-
7/27/2019 Scripting and Programming
122/415
-
7/27/2019 Scripting and Programming
123/415
-
7/27/2019 Scripting and Programming
124/415
-
7/27/2019 Scripting and Programming
125/415
P := (t) - >
-
7/27/2019 Scripting and Programming
126/415
-
7/27/2019 Scripting and Programming
127/415
-
7/27/2019 Scripting and Programming
128/415
-
7/27/2019 Scripting and Programming
129/415
CurveFitting[LeastSquares]
-
7/27/2019 Scripting and Programming
130/415
-
7/27/2019 Scripting and Programming
131/415
-
7/27/2019 Scripting and Programming
132/415
-
7/27/2019 Scripting and Programming
133/415
units
-
7/27/2019 Scripting and Programming
134/415
-
7/27/2019 Scripting and Programming
135/415
-
7/27/2019 Scripting and Programming
136/415
-
7/27/2019 Scripting and Programming
137/415
-
7/27/2019 Scripting and Programming
138/415
-
7/27/2019 Scripting and Programming
139/415
-
7/27/2019 Scripting and Programming
140/415
-
7/27/2019 Scripting and Programming
141/415
-
7/27/2019 Scripting and Programming
142/415
-
7/27/2019 Scripting and Programming
143/415
-
7/27/2019 Scripting and Programming
144/415
-
7/27/2019 Scripting and Programming
145/415
-
7/27/2019 Scripting and Programming
146/415
-
7/27/2019 Scripting and Programming
147/415
-
7/27/2019 Scripting and Programming
148/415
-
7/27/2019 Scripting and Programming
149/415
-
7/27/2019 Scripting and Programming
150/415
-
7/27/2019 Scripting and Programming
151/415
-
7/27/2019 Scripting and Programming
152/415
-
7/27/2019 Scripting and Programming
153/415
-
7/27/2019 Scripting and Programming
154/415
-
7/27/2019 Scripting and Programming
155/415
-
7/27/2019 Scripting and Programming
156/415
-
7/27/2019 Scripting and Programming
157/415
-
7/27/2019 Scripting and Programming
158/415
-
7/27/2019 Scripting and Programming
159/415
-
7/27/2019 Scripting and Programming
160/415
-
7/27/2019 Scripting and Programming
161/415
-
7/27/2019 Scripting and Programming
162/415
-
7/27/2019 Scripting and Programming
163/415
-
7/27/2019 Scripting and Programming
164/415
-
7/27/2019 Scripting and Programming
165/415
-
7/27/2019 Scripting and Programming
166/415
-
7/27/2019 Scripting and Programming
167/415
-
7/27/2019 Scripting and Programming
168/415
-
7/27/2019 Scripting and Programming
169/415
-
7/27/2019 Scripting and Programming
170/415
-
7/27/2019 Scripting and Programming
171/415
-
7/27/2019 Scripting and Programming
172/415
-
7/27/2019 Scripting and Programming
173/415
-
7/27/2019 Scripting and Programming
174/415
-
7/27/2019 Scripting and Programming
175/415
-
7/27/2019 Scripting and Programming
176/415
-
7/27/2019 Scripting and Programming
177/415
-
7/27/2019 Scripting and Programming
178/415
-
7/27/2019 Scripting and Programming
179/415
-
7/27/2019 Scripting and Programming
180/415
-
7/27/2019 Scripting and Programming
181/415
-
7/27/2019 Scripting and Programming
182/415
-
7/27/2019 Scripting and Programming
183/415
-
7/27/2019 Scripting and Programming
184/415
-
7/27/2019 Scripting and Programming
185/415
-
7/27/2019 Scripting and Programming
186/415
-
7/27/2019 Scripting and Programming
187/415
-
7/27/2019 Scripting and Programming
188/415
-
7/27/2019 Scripting and Programming
189/415
-
7/27/2019 Scripting and Programming
190/415
-
7/27/2019 Scripting and Programming
191/415
-
7/27/2019 Scripting and Programming
192/415
-
7/27/2019 Scripting and Programming
193/415
-
7/27/2019 Scripting and Programming
194/415
-
7/27/2019 Scripting and Programming
195/415
-
7/27/2019 Scripting and Programming
196/415
-
7/27/2019 Scripting and Programming
197/415
-
7/27/2019 Scripting and Programming
198/415
-
7/27/2019 Scripting and Programming
199/415
-
7/27/2019 Scripting and Programming
200/415
-
7/27/2019 Scripting and Programming
201/415
#We reset Maple so previous assignments are ditched.
restart;
#We assign two variables with pressure and #temperature
data.
pData := [134.2, 142.5, 155.0, 159.8, 171.1, 184.2];
tData := [0, 20.1, 39.8, 60.0, 79.9, 100.3];
#Load in the package
with(CurveFitting);
#Develop a linear formula for temperature
#as a function of pressure.
pressureFormula := LeastSquares(tData, pData, t);
#We can also fit a line to pressure as a function
# of temperature.
tempFormula := LeastSquares(pData, tData, p);
-
7/27/2019 Scripting and Programming
202/415
-
7/27/2019 Scripting and Programming
203/415
-
7/27/2019 Scripting and Programming
204/415
# Figure 11.1.1
#initialize variables
i := 1;
val := .3; #evaluation point
term := (val^i)/i!; # a term to compute
print("term is", term); #message
s := term; #s has a copy of the term
tol := 10e-7; #A small value.
-
7/27/2019 Scripting and Programming
205/415
a := sin(3.5 + cos( Pi/2) ;
Error, `;` unexpected
a := sin(3.5) + cos( Pi/2) ;
a := sin(3.5) + cos( Pi/2)
Warning, inserted missing semicolon at
end of statement
a := sin(3.5) + cos( Pi/2)
b := cos(3.5) + sin( Pi/2)
Error, missing operator or `;`
a := sin(3.5) + cos( Pi/2) ;
b := cos(3.5) + sin( Pi/2);
a := sin(3.5 + cos( Pi/2) ;
b := cos(3.5) + sin( Pi/2);
Error, `;` unexpected
a := sin(3 5 + cos( Pi/2)
-
7/27/2019 Scripting and Programming
206/415
a := sin(3.5 + cos( Pi/2)
Warning, premature end of input,
use + to avoid this
message.
a := sin(3.5 + cos( Pi/2)
b := cos(3.5) + sin( Pi/2);
Error, missing operator or `;`
#We reset Maple so previous assignments are lost.
restart;
#We assign two variables with pressure and #temperaturedata.
pData := [134.2, 142.5, 155.0, 159.8, 171.1, 184.2]:
print("Pressure data is:", pData);
tData := [0, 20.1, 39.8, 60.0, 79.9, 100.3]:
print("Temperature data is:", tData);
#Load in the package
-
7/27/2019 Scripting and Programming
207/415
with(CurveFitting):
#Develop a linear formula for temperature
#as a function of pressure.pressureFormula := LeastSquares(tData, pData, t):
print("Fit of temperature to pressure is:",
p=pressureFormula);
#We can also fit a line to pressure as a function
# of temperature.
tempFormula := LeastSquares(pData, tData, p):
print("Fit of pressure to temperature is:",
t=pressureFormula);
#Code region #1
#Reset all assignments in Maple so that they are forgotten
restart;
#Look for packages in the current directory (".") as well as
#built-in Maple library.
libname := ".", libname;
#Load CarSimulator package
with(CarSimulator);
-
7/27/2019 Scripting and Programming
208/415
-
7/27/2019 Scripting and Programming
209/415
#Code region #2
#We don't put a restart here because we want to
#remember what we did in the previous code region.
#To start afresh, rerun all of the regions.
#Initialize car simulator
initialize();
establishBoundaries(20,20): #draw a 20 x 20 sandbox for the car to roam
around in. Coordinates range from [0,0] to [20,20].drawBackground(): #Create a plot of the wall and the target for later
use, but don't display it yet
carMovie(stateTable); #Show initial configuration
-
7/27/2019 Scripting and Programming
210/415
#Code Region #3
run1a := proc()
move(1); #move forward one square
-
7/27/2019 Scripting and Programming
211/415
move(1); #move forward one square
turn(Pi/2); #turn Pi/2 radians (left 90 degrees)
move(1); #move forward one square
move(1); #move again.end proc:
run(run1a);
#Code Region #4
#Play an animation of the results of the simulation.
#After the "run", the stateTable has all the information #needed to
make the animation.
carMovie(stateTable);
-
7/27/2019 Scripting and Programming
212/415
#Establish an arena
restart;
libname := ".", libname;with(CarSimulator):
#Set up the problem. Note that by parameterizing the problem we can use random
#numbers to set up sizes and locations to vary the problem a little.
size := 30: #width and length of the sandbox.
k := 15;
L := 12;
h := 10:
y := 10:
distanceBetweenWalls := 10:
wallHeight := 5: #parameters for location and sizes
halfDistance := floor(distanceBetweenWalls/2):
halfHeight := floor(wallHeight/2):
initialize();
pickAStart := 3:
#Set the starting location by resetting the state table and CURRENTLOCATION
stateTable[0] := [ k+pickAStart,y,0,CARNORMAL]:
CURRENTLOCATION := [ k+pickAStart,y,0,CARNORMAL]:
#draw walls
setBackground([seq('[k-halfDistance, y+i],[k+halfDistance, y+i]',i=-halfHeight..halfHeight)],
BARRIER):
establishBoundaries(size,size): #draw a sandbox for the car to roam around in. Coordinates
drawBackground():
print("Starting configuration is:");
carMovie(stateTable);
-
7/27/2019 Scripting and Programming
213/415
-
7/27/2019 Scripting and Programming
214/415
barrierBounce := proc()
while not(isTouching(0, 'bt')) do
move(1);
end do;
turn(Pi); #turn around
while not(isTouching(Pi, 'bt')) do
move (1);
end do;
end:
run(barrierBounce);print("Simulation result is:");
carMovie(stateTable);
-
7/27/2019 Scripting and Programming
215/415
#Something that solves one kind of arena
lessRobustSoln := proc()
move(1);
turn(Pi);
move(1);
move(1):
move(1):
move(1):
move(1):
move(1);
move(1);
move(1);
end proc:
#We're not resetting everything, but we will initialize with a new arena
#Set up the problem. Note that by parameterizing the problem we can use random
#numbers to set up sizes and locations to vary the problem a little.
size := 40: #width and length of the sandbox.
k := 15;
L := 12;
-
7/27/2019 Scripting and Programming
216/415
L := 12;
h := 10:
y := 10:distanceBetweenWalls := 20:
wallHeight := 5: #parameters for location and sizes
halfDistance := floor(distanceBetweenWalls/2):
halfHeight := floor(wallHeight/2):
initialize();
pickAStart := 7:
#Set the starting location by resetting the state table and CURRENTLOCATION
stateTable[0] := [ k+pickAStart,y,0,CARNORMAL]:
CURRENTLOCATION := [ k+pickAStart,y,0,CARNORMAL]:
#draw walls
setBackground([seq('[k-halfDistance, y+i],[k+halfDistance, y+i]',i=-halfHeight..halfHeight)],
BARRIER):
establishBoundaries(size,size): #draw a sandbox for the car to roam around in. Coordinates
drawBackground():
print("Starting configuration is:");
carMovie(stateTable);
-
7/27/2019 Scripting and Programming
217/415
run(barrierBounce);
print("Simulation result is:");
carMovie(stateTable);
-
7/27/2019 Scripting and Programming
218/415
-
7/27/2019 Scripting and Programming
219/415
-
7/27/2019 Scripting and Programming
220/415
# Figure 11.1.1#initialize variables
i := 1;
val := .3; #evaluation point
term := (val^i)/i!; # a term to compute
print("term is", term); #message
s := term; #s has a copy of the term
tol := 10e-7; #A small value.
#Code region #1
#Reset all assignments in Maple so that they are forgotten
restart;
#Look for packages in the current directory (".") as well as
#built-in Maple library.
libname := ".", libname;
#Load CarSimulator packagewith(CarSimulator);
-
7/27/2019 Scripting and Programming
221/415
#Code region #2
#We don't put a restart here because we want to
#remember what we did in the previous code region.
#To start afresh, rerun all of the regions.
#Initialize car simulator
initialize();
establishBoundaries(20,20): #draw a 20 x 20 sandbox for the car to
roam around in. Coordinates range from [0,0] to [20,20].
drawBackground(): #Create a plot of the wall and the target for
later use, but don't display it yet
carMovie(stateTable); #Show initial configuration
-
7/27/2019 Scripting and Programming
222/415
barrierBounce := proc()
while not(isTouching(0, 'bt')) do
move(1);
end do;
turn(Pi); #turn around
while not(isTouching(Pi, 'bt')) do
move (1);
end do;
end:
run(barrierBounce);
print("Simulation result is:");
carMovie(stateTable);
-
7/27/2019 Scripting and Programming
223/415
-
7/27/2019 Scripting and Programming
224/415
-
7/27/2019 Scripting and Programming
225/415
-
7/27/2019 Scripting and Programming
226/415
-
7/27/2019 Scripting and Programming
227/415
#This function demonstrates the use of local variables.
testFunction := proc()
local i;
print("Line 1: i's value is",i);
i := 1;
print("Line 2: i's value is",i);
end proc;
#This is a "non-local" use of i;
i := 0;
print("Line 0: i's value is",i);
testFunction();
print("Line 3: i's value is",i);
-
7/27/2019 Scripting and Programming
228/415
#Generate test problem (code not shown)
#Turn left at the first 4 way intersection, go to target.
#Reset all assignments in Maple so that they are forgotten
restart;
#Look for packages in the current directory (".") as well as
#built-in Maple library.
libname := ".", libname;
#Load CarSimulator package
-
7/27/2019 Scripting and Programming
229/415
with(CarSimulator):
StringTools[Randomize]();
randomize():
#Pick a starting location for the car
xStart := 5:
yStart := 15:
#Pick typical distance to intersection
reach := 20:
#Pick distance to target intersection
distance := rand(reach-4..reach+4)():
#Distance to target
yTarget := rand(5..10)():
#Initialize car simulator
initialize():
#Size of arena
arenaSize := 40:
#pick two intersection locations
L1, R1:= op(combinat[randperm]([seq('2*i',i=1..floor(distance/2)-1)])[1..2]):
establishBoundaries(arenaSize,arenaSize):
#draw a sandbox for the car to roam around in. Coordinates range from [0,0] to [arenaSize,
arenaSize].
#Set the starting location by resetting the state table and CURRENTLOCATIONstateTable[0] := [ xStart,yStart,0,CARNORMAL]:
CURRENTLOCATION := [xStart,yStart,0,CARNORMAL]:
minLR := min(L1,R1):
maxLR := max(L1,R1):
#draw wall to a first intersection
setBackground([seq('[xStart+i, yStart-1],[xStart+i, yStart+1]',i=0..minLR-1)],
BARRIER):
#draw wall to a second intersection
setBackground([seq('[xStart+i, yStart-1],[xStart+i, yStart+1]',i=minLR+1..maxLR-1)],BARRIER):
#draw wall to a third intersection
setBackground([seq('[xStart+i, yStart-1],[xStart+i, yStart+1]',i=maxLR+1..distance-1)],
BARRIER):
#Draw wall to the end
setBackground([seq('[xStart+i, yStart-1],[xStart+i, yStart+1]',i=distance+1..min(arenaSize-
xStart-2,2*distance))],
BARRIER):
-
7/27/2019 Scripting and Programming
230/415
#Draw wall at intersection
setBackground([seq('[xStart+distance-1, yStart+i],[xStart+distance+1, yStart
+i]',i=1..min(arenaSize-yStart-2, distance))],
BARRIER):
setBackground([seq('[xStart+distance-1, yStart-i],[xStart+distance+1, yStart-
i]',i=1..min(yStart-2, distance))],
BARRIER):
#set target location
setBackground([seq('[xStart+distance, yStart+yTarget]',i=0..distance)],TARGET):
#Draw left wall at intersection
setBackground([seq('[xStart+L1-1, yStart+i],[xStart+L1+1, yStart+i]',i=1..min(arenaSize-
yStart-2, distance))],
BARRIER):
setBackground([[xStart+L1, yStart-1]],
BARRIER):
#Draw right wall at intersection
setBackground([seq('[xStart+R1-1, yStart-i],[xStart+R1+1, yStart-i]',i=1..min(yStart-2,
distance))],
BARRIER):
setBackground([[xStart+R1, yStart+1]],
BARRIER):
setBackground([seq('[xStart+distance-1, yStart-i],[xStart+distance+1, yStart-i]',i=1..min(yStart-2, distance))],
BARRIER):
drawBackground(): #Create a plot of the wall and the target for later use, but don't display it
yet
carMovie(stateTable); #Show initial configuration
-
7/27/2019 Scripting and Programming
231/415
goToFourWay := proc()
while isTouching(-Pi,'bt') or(isTouching(Pi/2,'bt')) or (isTouching(-Pi/2,'bt')) or
(isTouching(0)) do
move(1);
end do;
turn(Pi/2);while not(isTouching(Pi/2,'bt')) do
move(1);
end do;
end proc:
run(goToFourWay);
carMovie(stateTable);
-
7/27/2019 Scripting and Programming
232/415
#Generate test problem
#Turn left at the first 4 way intersection, go to target.
#Reset all assignments in Maple so that they are forgotten
restart;
#Look for packages in the current directory (".") as well as
#built-in Maple library.
libname := ".", libname;
#Load CarSimulator package
with(CarSimulator):
StringTools[Randomize]();
randomize():
#Pick a starting location for the car
xStart := 5:
yStart := 15:
#Pick lengths of rectanglebottomTop := rand(4..10)();
leftRight := rand(4..10)();
#Initialize car simulator
initialize():
#Size of arena
arenaSize := 40:
establishBoundaries(arenaSize,arenaSize):
#draw a sandbox for the car to roam around in. Coordinates range from [0,0] to [arenaSize,Si ]
-
7/27/2019 Scripting and Programming
233/415
#draw a sandbox for the car to roam around in. Coordinates range from [0,0] to [arenaSize,arenaSize].
#Set the starting location by resetting the state table and CURRENTLOCATION
stateTable[0] := [ xStart,yStart,0,CARNORMAL]:
CURRENTLOCATION := [xStart,yStart,0,CARNORMAL]:
minLR := min(L1,R1):
maxLR := max(L1,R1):
#draw bottom and top walls
setBackground([seq('[xStart+i, yStart+1],[xStart+i, yStart+leftRight]',i=0..bottomTop)],
BARRIER):
#draw left and right walls
setBackground([seq('[xStart, yStart+i],[xStart+bottomTop, yStart+i]',i=1..leftRight)],
BARRIER):
drawBackground(): #Create a plot of the wall and the target for later use, but don't display it
yet
carMovie(stateTable); #Show initial configuration
-
7/27/2019 Scripting and Programming
234/415
turnFourTimes := proc()
local bt,angle;
#Move forward while touching a wall, then
#turn left
#Initial scan angle is Pi/2
angle := Pi/2;
while isTouching(angle,'bt') do
move(1);
end do;
turn(Pi/2);
move(1);
angle := angle+Pi/2; #angle is now Pi
#Do that again.
while isTouching(angle,'bt') do
move(1);
end do;
turn(Pi/2);move(1);
angle := angle + Pi/2; #angle now 3*Pi/2
#Do that again.
while isTouching(angle,'bt') do
move(1);
end do;
turn(Pi/2);
move(1);
angle := angle+Pi/2; #2*Pi is same as 0.
#Do that one more time
while isTouching(angle,'bt') domove(1);
end do;
turn(Pi/2);
move(1);
end proc:
run(turnFourTimes);
-
7/27/2019 Scripting and Programming
235/415
carMovie(stateTable);
-
7/27/2019 Scripting and Programming
236/415
#Generate test problem
#Turn left at the first 4 way intersection, go to target.
#Reset all assignments in Maple so that they are forgotten
restart;
#Look for packages in the current directory (".") as well as
#built-in Maple library.
libname := ".", libname:
#Load CarSimulator package
with(CarSimulator):
StringTools[Randomize]():
randomize():
#Pick a starting location for the car
xStart := 5:
yStart := 15:
#Pick lengths of rectangle
bottomTop := rand(4..10)():
leftRight := rand(4..10)():
#Initialize car simulator
initialize():
#Size of arena
arenaSize := 40:
establishBoundaries(arenaSize,arenaSize):
#draw a sandbox for the car to roam around in. Coordinates range from [0,0] to [arenaSize,
arenaSize].
#Set the starting location by resetting the state table and CURRENTLOCATION
stateTable[0] := [ xStart,yStart,0,CARNORMAL]:
CURRENTLOCATION := [xStart,yStart,0,CARNORMAL]:
minLR := min(L1,R1):
maxLR := max(L1,R1):
#draw bottom and top walls
setBackground([seq('[xStart+i, yStart+1],[xStart+i, yStart+leftRight]',i=0..bottomTop)],
BARRIER):
#draw left and right walls
setBackground([seq('[xStart, yStart+i],[xStart+bottomTop, yStart+i]',i=1..leftRight)],
BARRIER):
-
7/27/2019 Scripting and Programming
237/415
drawBackground(): #Create a plot of the wall and the target for later use, but don't display it
yet
carMovie(stateTable); #Show initial configuration
turnFourTimesA := proc()
local bt,angle,count;
#Move forward while touching a wall, then
#turn left
#Initial scan angle is Pi/2
angle := Pi/2;
#Initial count is zero.
count := 0;
while(count
-
7/27/2019 Scripting and Programming
238/415
g( g , )
move(1);
end do;
turn(Pi/2);
move(1);
angle := angle+Pi/2;
count := count +1;
end do;
end proc:
run(turnFourTimesA);carMovie(stateTable);
-
7/27/2019 Scripting and Programming
239/415
#Given two sides of a right triangle, compute
#the hypotenuse and the angle formed by it.
triangleInfo := proc (side1, side2)
local hypotenuse, angle, dAngle;
#Use whatever values are provided when the
#procedure is invoked
angle := arctan(side1,side2);
dAngle := convert(angle,units,radians,degrees);
hypotenuse := sqrt(side1^2+side2^2);
#Return a list of the hypotenuse and the angle
#(in degrees).
[hypotenuse, dAngle];
end proc:
-
7/27/2019 Scripting and Programming
240/415
findPower := proc (x,tol)
local i;
i := 0;
term := x^i;
while (term>tol) do
term := x^i;
Warning, premature end of input,
use + to avoid this
message.
findPower := proc (x,tol)
local i;
i := 0;
term := x^i;
while (term>tol) do
term := x^i;
i := i+1;
end proc;
Error, reserved word `proc` unexpected
findPower := proc (x,tol)
local i;
i := 0;
term := x^i;
while (term>tol) do
term := x^i;
i := i+1;end do;
end proc;
-
7/27/2019 Scripting and Programming
241/415
end proc;
Warning, `term` is implicitly declared
local to procedure `findPower`
findPower := proc (x,tol)
local i,term;
i := 0;
term := x^i;
while (term>tol) do
term := x^i;
i := i+1;
end do;
end proc;
-
7/27/2019 Scripting and Programming
242/415
findPower := proc (x,tol)
local i,term;
i := 0;
term := x^i;
while (term>tol) do
term := x^i;
i := i+1;
end do;i -1;
end proc;
findPower := proc (x,tol)
local i,term;
i := 0;
term := x^i;
while (term>tol) do
i := i+1;term := x^i;
-
7/27/2019 Scripting and Programming
243/415
end do;
i -1;
end proc;
g := proc(a,n);local i, count;
count := 0;
while count
-
7/27/2019 Scripting and Programming
244/415
g := proc(a,n)
local i, result,count;
result := a;for i from 1 to n do
result := sqrt(result); #take multiple square roots
count := count +1;
end do;
return result;
end;
eApprox2 := proc(x, tol)
local term,i;
s := 1;
term := 1;
i := 1;
while abs(term)>=tol doterm := term*x/i;
s := s+term;
-
7/27/2019 Scripting and Programming
245/415
i := i+1;
end do;
return s;
end proc:
Warning, `s` is implicitly declared local to
procedure `eApprox2`
x := 0;
while (x
-
7/27/2019 Scripting and Programming
246/415
i : 1;
while abs(term)>=tol do
term := term*x/i;
s := s+term;
i := i+1;
end do;
return s;
end proc:
x := 3.0;
while (x
-
7/27/2019 Scripting and Programming
247/415
-
7/27/2019 Scripting and Programming
248/415
-
7/27/2019 Scripting and Programming
249/415
-
7/27/2019 Scripting and Programming
250/415
-
7/27/2019 Scripting and Programming
251/415
#Generate test problem
#Turn left at the first 4 way intersection, go to target.
#Reset all assignments in Maple so that they are forgotten
restart;
#Look for packages in the current directory (".") as well as
#built-in Maple library.
libname := ".", libname;
#Load CarSimulator package
with(CarSimulator):
StringTools[Randomize]();
randomize():
#Pick a starting location for the car
xStart := 5:
yStart := 15:
#Pick lengths of rectangle
bottomTop := rand(4..10)();
leftRight := rand(4..10)();
#Initialize car simulator
initialize():
#Size of arena
arenaSize := 40:
establishBoundaries(arenaSize,arenaSize):
#draw a sandbox for the car to roam around in. Coordinates range from [0,0] to [arenaSize,
arenaSize].
#Set the starting location by resetting the state table and CURRENTLOCATION
stateTable[0] := [ xStart,yStart,0,CARNORMAL]:
CURRENTLOCATION := [xStart,yStart,0,CARNORMAL]:
minLR := min(L1,R1):
maxLR := max(L1,R1):
#draw bottom and top walls
setBackground([seq('[xStart+i, yStart+1],[xStart+i, yStart+leftRight]',i=0..bottomTop)],
BARRIER):
#draw left and right walls
setBackground([seq('[xStart, yStart+i],[xStart+bottomTop, yStart+i]',i=1..leftRight)],
BARRIER):
drawBackground(): #Create a plot of the wall and the target for later use, but don't display it
yet
carMovie(stateTable); #Show initial configuration
-
7/27/2019 Scripting and Programming
252/415
-
7/27/2019 Scripting and Programming
253/415
turnFourTimesWithFor := proc()
local bt,angle,i;
#Move forward while touching a wall, then
#turn left
#Initial scan angle is Pi/2angle := Pi/2;
for i from 1 to 4 do
while isTouching(angle,'bt') do
move(1);
end do;
turn(Pi/2);
move(1);
angle := angle+Pi/2; #angle is now Pi
end do;
end proc:
run(turnFourTimesWithFor);
carMovie(stateTable);
-
7/27/2019 Scripting and Programming
254/415
#Generate test problem
#Turn left at the first 4 way intersection, go to target.
#Reset all assignments in Maple so that they are forgotten
restart;
#Look for packages in the current directory (".") as well as
#built-in Maple library.
libname := ".", libname;
#Load CarSimulator package
with(CarSimulator):
StringTools[Randomize]();
randomize():
#Pick a starting location for the car
xStart := 5:
yStart := 15:
#Pick lengths of rectangle
bottomTop := rand(4..10)():
leftRight := rand(4..10)():
#Initialize car simulator
initialize():
#Size of arena
arenaSize := 40:
establishBoundaries(arenaSize,arenaSize):
#draw a sandbox for the car to roam around in. Coordinates range from [0,0] to [arenaSize,
arenaSize].
#Set the starting location by resetting the state table and CURRENTLOCATION
stateTable[0] := [ xStart,yStart,0,CARNORMAL]:
CURRENTLOCATION := [xStart,yStart,0,CARNORMAL]:
minLR := min(L1,R1):
maxLR := max(L1,R1):
#draw bottom and top walls
setBackground([seq('[xStart+i, yStart+1],[xStart+i, yStart+leftRight]',i=0..bottomTop)],
BARRIER):
#draw left and right walls
setBackground([seq('[xStart, yStart+i],[xStart+bottomTop, yStart+i]',i=1..leftRight)],BARRIER):
-
7/27/2019 Scripting and Programming
255/415
drawBackground(): #Create a plot of the wall and the target for later use, but don't display it
yet
carMovie(stateTable); #Show initial configuration
turnFourTimesWithAngleFor := proc()
local bt i;
-
7/27/2019 Scripting and Programming
256/415
local bt,i;
#Do the following for each of four angles:
for i from 1 to 4 do
#Move forward while touching a wall, then
while isTouching(Pi/2*i,'bt') do
move(1);
end do;
#turn left and move forward one squareturn(Pi/2);
move(1);
end do;
end proc:
run(turnFourTimesWithAngleFor);
carMovie(stateTable);
-
7/27/2019 Scripting and Programming
257/415
to 5 do
print("I will not burp in class.");
end do;
print("x and sin(x)");
for x from 1 by -.3 to -1 do
print( x, evalf(sqrt(x)));
end do;
-
7/27/2019 Scripting and Programming
258/415
grade := 3.7: #This value will cause execution of the "then" code
if (grade >= 3.5)
then
print("An excellent grade:", grade);
end if;
grade := 3.2: #This value will cause execution of the "else" code
-
7/27/2019 Scripting and Programming
259/415
g
if (grade >= 3.5)
then
print("An excellent grade", grade);
else
print("A grade:", grade);
end if;
#Initialize a list with some numbers, some grade point
# averages
L := [3.6, 3.9, 2.1, 3.9, 2.1, 0.7, 1.3, 2.8, 3.6, 3.7, 2.9];
#Initialize some accumulation variables. We will use them
#to count how many grades are 3.5 or above, and
#how many are below.
countHigh := 0;
countLow := 0;
#This goes first, before a loop, as a table header
printf("Grades at least 3.5:\n");
#i steps through the possible index values for L.
#Recall that nops(L) is the length of the list.
for i from 1 to nops(L) do
if (L[i] >= 3.5)
then
print( L[i]);
countHigh := countHigh+1; #add one to the "high" counterelse
countLow := countLow+1; #add one to the "low" counter
end if;
end do:
print("There were",countHigh,"grades at or above, and ", countLow, "grades below 3.5.");
Grades at least 3.5:
-
7/27/2019 Scripting and Programming
260/415
restart; #unassign any previous values
L := [3.6, 3.9, 2.1, 3.9, 2.1, 0.7, 1.3, 2.8, 3.6, 3.7, 3.8, 2.0]:
deanCount := 0:
#Compute the average for all students by
#using sum to add up all the grades, and dividing by the#number of grades
avg := sum(L[i],i=1..nops(L))/nops(L):
#Print out table header.
printf("Grade average of %d students is: %f.\n", nops(L), avg);
printf("Grades at least one point above average (Dean's List):\n");
for i from 1 to nops(L) do
if (L[i] >= (avg+1) )
thenprintf("%.2f\n", L[i]);
deanCount := deanCount+1;
end if;
end do:
printf("\nThere were %d students making the Dean's List.",deanCount);
Grade average of 12 students is: 2.791667.
Grades at least one point above average (Dean's List):
3.90
3.90
3.80
There were 3 students making the Dean's List.
-
7/27/2019 Scripting and Programming
261/415
#Initialize L to be a list of grades
L := [3.6, 3.9, 0.9, 2.1, 3.9, 2.1, 0.7, 1.3, 2.8, 3.6, 3.7, 2.9, 1.3];
#Initialize counters foer the number of As, Bs, etc. (silently)
countA := 0:
countB := 0:
countC := 0:
countD := 0:
countF := 0:
#Process each element of the list, with the following
#cases:
#As (3.3 and above)
#Bs (at or above 2.7 and below an A)
#Cs (at or above 1 7 and
-
7/27/2019 Scripting and Programming
262/415
#Cs (at or above 1.7 and
#Ds (at or above 1.0 and below a C)
#Fs (below 1.0).
for i from 1 to nops(L) do
x := L[i]; #Use "x" instead of typing L[i] several times
if (x >= 3.3)then
countA := countA +1;
elif (x >=2.7) #this is tried only if first condition false
then
countB := countB +1;
elif (x >=1.7)
then
countC := countC +1;
elif (x>=1.0)
then
countD := countD +1;
else #we will be here only if everything else hasn't worked
# so we know x
-
7/27/2019 Scripting and Programming
263/415
randomize():
#Starting location of the car is the default
#Initialize car simulator
initialize():
#Size of arena
arenaSize := 40:
establishBoundaries(arenaSize,arenaSize):
#draw a sandbox for the car to roam around in. Coordinates range from [0,0] to [arenaSize,
arenaSize].
numTurns := rand(4..14)():
lastLocX := 0:
lastLocY := 0:
for i from 1 to numTurns do
distance := rand(2..5)();
if i mod 2 = 1
then #move horiz
nextLocX := lastLocX +distance;
nextLocY := lastLocY;
setBackground([nextLocX, nextLocY], REDSIGN);
else #move vertical
nextLocX := lastLocX;
nextLocY := lastLocY + distance;
setBackground([nextLocX, nextLocY], YELLOWSIGN);
end if;
lastLocX := nextLocX;
lastLocY := nextLocY;
end do:
distance := rand(2..5)();
if numTurns+1 mod 2 = 1
then #move horiz
nextLocX := lastLocX +distance;
nextLocY := lastLocY;setBackground([nextLocX, nextLocY], PURPLESIGN);
else #move vertical
nextLocX := lastLocX;
nextLocY := lastLocY + distance;
setBackground([nextLocX, nextLocY], PURPLESIGN);
end if;
drawBackground(): #Create a plot of the wall and the target for later use, but don't display it
yet
carMovie(stateTable); #Show initial configuration
-
7/27/2019 Scripting and Programming
264/415
-
7/27/2019 Scripting and Programming
265/415
turnRedYellow := proc();
#No local variables needed in this procedure
#Move while you're not on a purple sign
while not(onTopOfSign(PURPLESIGNHIT)) do
#turn right if you're on a red sign
if onTopOfSign(REDSIGNHIT)
then turn(Pi/2);
#turn left if you're on a yellow sign
elif onTopOfSign(YELLOWSIGNHIT)
then turn(-Pi/2);
end if;
move(1); #move forward regardless of whether
#you've turned.
end do;
end proc:
run(turnRedYellow);
carMovie(stateTable);
-
7/27/2019 Scripting and Programming
266/415
int countThree, countSubThree;
int [] L= {1,5,9,2,6,7,-2,1,7};
// Set up counters
countThree=0;
countSubThree=0;
// Loop through L counting how many are above three
for (int i=0; i3)
\011{ countThree++;}
else
\011{countSubThree++;}
};
System.out.println("three and above:"+countThree+
\011 " below three: "+ countSubThree);
%Program to count how many elements of a vector/list are greater than and less %than three
countThree=0;
countSubThree=0;
-
7/27/2019 Scripting and Programming
267/415
L=[1,5,9,2,6,7,-2,1,7];
[r,c]=size(L);
%Loop for i from 1 to c, where c is the number of items in L (length of L)
for i=1:c
if L(i)>3
countThree=countThree+1;
else
countSubThree=countSubThree+1;
end;
end;
%Use formatted printing to print out things.
sprintf('Three and Above: %d, Below three: %d', [countThree, countSubThree])
x := 3;
h 0
-
7/27/2019 Scripting and Programming
268/415
countThree := 0;
countSubThree := 0;
if x>3
then
countThree := countThree +1;else
countSubThree := countSubThree +1;
end if;
printf("three and above:%d, below three: %d",
countThree, countSubThree);
three and above:0, below three: 1
x := 3;
countThree := 0;
countSubThree := 0;
if x>3
countThree := countThree +1;
else
countSubThree := countSubThree +1;
end if;
printf("three and above:%d, below three: %d",
countThree, countSubThree);
Error, missing operator or `;`
-
7/27/2019 Scripting and Programming
269/415
x := 3;
countThree := 0;
countSubThree := 0;
if x>3
then
countThree := countThree +1;
else
countSubThree := countSubThree +1;
printf("three and above:%d, below three: %d",
countThree, countSubThree);
Warning, premature end of input, use + to
avoid this message.
x := 3;countThree := 0;
countSubThree := 0;
if x>3
then
countThree := countThree +1;
else
countSubThree := countSubThree +1;
end do;
printf("three and above:%d, below three: %d",countThree, countSubThree);
-
7/27/2019 Scripting and Programming
270/415
Error, reserved word `do` unexpected
x := 3;
countThree := 0;
countSubThree := 0;
if x>3
then
countThree := countThree +1;
else
countSubThree := countSubThree +1;
end;
printf("three and above:%d, below three: %d",
countThree, countSubThree);
three and above:0, below three: 1
x := 3;
countThree := 0;
countSubThree := 0;
if x>3
then
countThree := countThree +1;
else
countSubThree := countSubThree +1;
end if
printf("three and above:%d, below three: %d",
countThree, countSubThree);
Error, missing operator or `;`
-
7/27/2019 Scripting and Programming
271/415
, g p ;
x := 3;
countThree := 0;countSubThree := 0;
IF x>3
THEN
countThree := countThree +1;
ELSE
countSubThree := countSubThree +1;
END IF;
printf("three and above:%d, below three: %d",
countThree, countSubThree);
Error, missing operator or `;`
for angle from 5.0 to 10.0 by .5 do
x := convert(angle, units, degrees,
radians);
print("Cosecant of ", angle, " degreesis", csc(x));
end do:
-
7/27/2019 Scripting and Programming
272/415
#print out a message if a sin of a multiple of
Pi/4 is 1 or -1.
for i from 1 to 10 do
if abs(sin(i*Pi/4)) = 1
then
print(i,"sin(",i*Pi/4," ) is ",sin(i*Pi/4));
#i*Pi/4 automatically reduced to simplest
terms in output
end if;
end do;
grade := 3.2: #This value will cause execution
of the "else" code
if (grade >= 3.5)
then
print("An excellent grade", grade);
else
print("A grade:", grade);
end if;
for i from 1 to 10 do
if i=1
then
print("i is one");
elif isprime(i) #built-in primality
-
7/27/2019 Scripting and Programming
273/415
p ( ) p y
testing function
#Returns true if its argument is a
prime number
#See on-line documentation for
isprime.then
print (i, "is prime");
elif i mod 2 = 0 #test for divisibility by
two
#See on-line documention for "mod"
then
print(i, "is even");
else
print(i, "is neither prime nor evennor one");
end if;
end do;
-
7/27/2019 Scripting and Programming
274/415
-
7/27/2019 Scripting and Programming
275/415
The answer is: x = x^2+1
The answer is : 0.009095
0.00910
2432902008176640000.00000
0.750000
The answer is:
-
7/27/2019 Scripting and Programming
276/415
9.095277e-03
9.09528e-03
3.628800e+06
7.500000e-01
2.43290e+18
7.500000e-01
The answer is:
2432902008176640000
-
7/27/2019 Scripting and Programming
277/415
3
The color is DodgerBlue.
One two,
buckle my shoe.
3, 4,
shut the door.
1,\0112,\011buckle my shoe.
3\0114,\011shut the door.
-
7/27/2019 Scripting and Programming
278/415
The last element of the list is 0.800000.
There are 4 elements in this list.
The largest is 9.048374e-01.
L has 4 elements. The largest is 9.048374e-01.
There are 4.000000 elements in this list. The
largest is
[.9048374180, .8187307531, .7408182207, 4/5]
-
7/27/2019 Scripting and Programming
279/415
-
7/27/2019 Scripting and Programming
280/415
n = 3
x = 4.7
print (*,35) n,x
35 format('There were',i2,' values, and the answer was',f5.1, '.')
stop
end
-
7/27/2019 Scripting and Programming
281/415
n := 5;
result := 0;
result2 := 1;
for i to n do
result := result+i;result2 := result2*i^2;
end do;
printf("The sum of the numbers from 1 to %d
is: %d.\n", n, result);
printf("The product of squares i^2 from i=1 to
%d is %d.\n",n,result2);
The sum of the numbers from 1 to 5 is:
15.
The product of squares i^2 from i=1 to 5
is 14400.
n := 10;
lt 0
-
7/27/2019 Scripting and Programming
282/415
result := 0;
result2 := 1;
for i to n do
result := result+i;result2 := result2*i^2;
end do:
printf("The sum of the numbers from 1 to %d
is: %d.\n", n, result);
printf("The product of squares i^2 from i=1 to%d is %d.\n",n,result2);
The sum of the numbers from 1 to 10 is:
55.
The product of squares i^2 from i=1 to
10 is 13168189440000.
n := 10;
result := 0;
result2 := 1;
for i to n do
result := result+i;
result2 := result2*i^2;
printf("i is: %2d, result2 is: %4.2e
\n",i,result2);
end do:
printf("The sum of the numbers from 1 to %dis: %d.\n", n, result);
printf("The product of squares i^2 from i=1 to
%d is %d.\n",n,result2);
i is: 1, result2 is: 1.00e+00
i is: 2, result2 is: 4.00e+00i is: 3, result2 is: 3.60e+01
i is: 4, result2 is: 5.76e+02
i is: 5, result2 is: 1.44e+04
i is: 6, result2 is: 5.18e+05
i is: 7, result2 is: 2.54e+07
i is: 8, result2 is: 1.63e+09
i is: 9, result2 is: 1.32e+11
i i 10 l 2 i 1 32 13
-
7/27/2019 Scripting and Programming
283/415
i is: 10, result2 is: 1.32e+13
The sum of the numbers from 1 to 10 is:
55.
The product of squares i^2 from i=1 to10 is 13168189440000.
maxSum := proc(xdata, ydata)
local s,i, n;
#Loop over the 1st, 2nd, etc. value of the
lists
for i from 1 to n do
#Compare them and add the larger to s.
if xdata[i]>ydata[i]
then s := s + xdata[i];
else s := s + ydata[i];
end if;
end do;
return s;
end proc;
-
7/27/2019 Scripting and Programming
284/415
maxSum := proc(xdata, ydata)local s,i, n;
n := nops(xdata);
#Loop over the 1st, 2nd, etc. value of the
lists
for i from 1 to n do
#Compare them and add the larger to s.
if xdata[i]>ydata[i]
then s := s + xdata[i];
else s := s + ydata[i];
end if;
end do;return s;
end proc;
-
7/27/2019 Scripting and Programming
285/415
-
7/27/2019 Scripting and Programming
286/415
maxSum := proc(xdata, ydata)
local s,i, n;n := nops(xdata);
s := 0; #Initialize accumulation variable
to zero.
#Loop over the 1st, 2nd, etc. value of the
lists
for i from 1 to n do
#Compare them and add the larger to s.
if xdata[i]>ydata[i]
then s := s + xdata[i];else s := s + ydata[i];
end if;
end do;
return s; #Return accumulated sum as the
result.
end proc;
-
7/27/2019 Scripting and Programming
287/415
L has 4 elements.
The largest is 9.048374e-01.
-
7/27/2019 Scripting and Programming
288/415
-
7/27/2019 Scripting and Programming
289/415
-
7/27/2019 Scripting and Programming
290/415
-
7/27/2019 Scripting and Programming
291/415
-
7/27/2019 Scripting and Programming
292/415
-
7/27/2019 Scripting and Programming
293/415
-
7/27/2019 Scripting and Programming
294/415
-
7/27/2019 Scripting and Programming
295/415
-
7/27/2019 Scripting and Programming
296/415
-
7/27/2019 Scripting and Programming
297/415
a=1, b is 3.141593, c is
arctan(1/3*Pi)
a=1.000000, b is 3.141593, c is
0.808449
-
7/27/2019 Scripting and Programming
298/415
-
7/27/2019 Scripting and Programming
299/415
#This procedure takes two lists of scores from n tests.
#It finds the average of the best scores, and the average
#of the worst scores
maxMinAvg := proc(list1, list2)
local i, n, smax, smin;
#Initialize the accumulator variables
smax, smin:= 0,0;
n:= nops(list1);
#Add together the maximum and minimums.
for i from 1 to n do
smax := smax + max(list1[i], list2[i]);
smin := smin + min(list1[i], list2[i]);
end do;
return smin/n, smax/n;
end proc;
L1 := [ 95, 75, 74, 83];
L2 := [ 72, 98, 0, 85];
worst, best := maxMinAvg(L1, L2);
printf("Avg of best scores is: %4.2f\nAvg of worst scores is: %4.2f", best,worst);
L1 := [ 10, 9, 7.5, 6.4, 7.4];
L2 := [ 8, 8, 8, 8, 9];
worst, best := maxMinAvg(L1, L2):#suppress output with a colon
printf("Avg of best scores is: %4.2f\nAvg of worst scores is: %4.2f", best,worst);
-
7/27/2019 Scripting and Programming
300/415
Avg of best scores is: 88.00Avg of worst scores is: 57.50
Avg of best scores is: 8.80
Avg of worst scores is: 7.46
interestCalculator := proc (initialAmt, rate,
numYears)
local i, A, newA;
#initialize A
A := initialAmt;
#Calculate new amount based on present amt
#and interest rate
for i from 1 to numYears do
newA := A + rate*A;
A := newA; #Get ready for next loop trip
end do;
-
7/27/2019 Scripting and Programming
301/415
end do;
return A;
end proc;
interestCalculator2 := proc (initialAmt, rate,
numYears)
local i, A, newA, yearTab, amtTab;
#initialize A
A := initialAmt;
#initialize tables
yearTab := table();
amtTab := table();
yearTab[0] := 0;
amtTab[0] := initialAmt;
#Calculate new amount based on present amt
#and interest rate
for i from 1 to numYears do
-
7/27/2019 Scripting and Programming
302/415
newA := A + rate*A;A := newA;
#Remember this year's info.
yearTab[i] := i;
amtTab[i] := A;
end do;
return convert(yearTab,list),
convert(amtTab,list);
end proc;
interestCalculator3 := proc (initialAmt, rate,
numYears)
local i, A, newA, yearTab, amtTab;
#initialize A
A := initialAmt;
#initialize tables
yearTab := table();
amtTab := table();
yearTab[0] := 0;
amtTab[0] := initialAmt;
-
7/27/2019 Scripting and Programming
303/415
#Calculate new amount based on present amt
#and interest rate
for i from 1 to numYears do
newA := A + rate*A;
A := newA;
#Remember this year's info.
yearTab[i] := i;
amtTab[i] := A;
end do;
return plot(convert(yearTab,list),
convert(amtTab,list),
style=POINT, color="Red", symbol=diamond,
labels=["years", "dollars"],
title=sprintf("Growth of a $%5.2f deposit
at %4.2f percent interest",initialAmt,
rate*100));
end proc:
interestCalculator4 := proc (initialAmt, rate,
numYears)
local i, A, newA, yearTab, amtTab;
#initialize A
A := initialAmt;
-
7/27/2019 Scripting and Programming
304/415
#initialize tablesyearTab := table();
amtTab := table();
yearTab[0] := 0;
amtTab[0] := initialAmt;
#Calculate new amount based on present amt
#and interest rate
for i from 1 to numYears do
newA := A + rate*A;
A := newA;
#Remember this year's info.
yearTab[i] := i;
amtTab[i] := A;
end do;
return
amtTab[numYears],plot(convert(yearTab,list),
convert(amtTab,list),
style=POINT, color="Red",
labels=["years", "dollars"],title=sprintf("Growth of a $%5.2f deposit
at %4.2f percent interest",initialAmt,
rate*100));
end proc:
-
7/27/2019 Scripting and Programming
305/415
interestCalculator5 := proc (initialAmt, rate,numYears)
local i, A, newA, yearTab, amtTab;
#initialize A
A := initialAmt;
#initialize tables
yearTab := table();
amtTab := table();
yearTab[0] := 0;amtTab[0] := initialAmt;
#Calculate new amount based on present amt
#and interest rate
for i from 1 to numYears do
amtTab[i] := amtTab[i-1] + rate*amtTab[i-1];
#Remember this year's info.
yearTab[i] := i;
end do;
return
amtTab[numYears],plot(convert(yearTab,list),
convert(amtTab,list),
style=POINT, color="Red",
labels=["years", "dollars"],
title=sprintf("Growth of a $%5.2f deposit
at %4.2f percent interest",initialAmt,
rate*100));
end proc:
-
7/27/2019 Scripting and Programming
306/415
-
7/27/2019 Scripting and Programming
307/415
a=1, b is 3.141593, c is arctan(1/3*Pi)
a=1.000000, b is 3.141593, c is 0.808449
-
7/27/2019 Scripting and Programming
308/415
restart;
#Use drawBoxB function to draw a box
drawBoxB := proc(x0, y0, width, height, c, plotTitle)
local L1, L2, L3, L4, ptlf;
#short hand abbreviation of line
ptlf := plottools[line];#this avoids having to remember to do "with(plottools)" in the session.
L1 := ptlf([x0, y0], [x0+width, y0], color=c);
L2 := ptlf([x0, y0], [x0, y0 + height], color=c);
L3 := ptlf([x0+width, y0], [x0+width, y0+height], color=c);
L4 := ptlf([x0, y0+height], [x0+width, y0+height], color=c);
plots[display]([L1, L2, L3, L4], title=plotTitle);
end proc;
movie1 := proc(u1, u2, numsteps1, numsteps2, boxColor)
-
7/27/2019 Scripting and Programming
309/415
local indexTab, frames, frames2, i,
h, uh, s1, s2, s3, L1;
#initialize tables
frames, frames2:= table(), table();
#part 1 -- draw square u1 units wide moving from (0,0) to (0,1) taking numsteps1 frames.
#h is the step distance
h := (1-0)/numsteps1; #step size of movement
for i from 0 to numsteps1 do
frames[i] := drawBoxB(0,0+i*h,u1, u1, boxColor, "Part 1");
end do;
#part 2 -- draw square growing from u1 units wide
#to u2 units wide in numsteps2 steps,
#moving from (0,1) to (1,1) in numsteps2 frames.
#h is the distance step size
#uh is the size step size
h := 1/numsteps2;uh := (u2-u1)/numsteps2;
frames2 := table();
for i from 0 to numsteps2 do
frames2[i] := drawBoxB(0+h*(i),1,u1+uh*(i), u1+uh*(i), boxColor, "Part 2");
end do;
#Convert tables into lists, then the two lists into sequences
#Section 6.8 (p. 85) talked about this conversion.
s1 := op(convert(frames,list));s2 := op(convert(frames2,list));
#Merge the two sequences into a single sequence
s3 := s1, s2;
#convert the sequence into a list
L1 := [s3];
#Create a movie of all the frames
plots[display](L1, insequence=true);
end proc;
#Test 0 Draw a box
printf("Test 0 -- draw a box\n");
drawBoxB(0, 0, 1, 1,"Red", "Box Test");
#Test 1
#draw red square 1 unit in size changing into
#a square 1.5 units in size.
-
7/27/2019 Scripting and Programming
310/415
#a square 1.5 units in size.
#Part 1 is 11 frames long, Part 2 is 16 frames long.
printf("Test 1 -- do a two part movie\n");
movie1(1, 1.5, 10, 15,"Red");
Test 0 -- draw a box
-
7/27/2019 Scripting and Programming
311/415
restart;
#Use drawBoxB function to draw a box
drawBoxB := proc(x0, y0, width, height, c, plotTitle)
local ptlf;#short hand abbreviation of line
ptlf := plottools[line];
#this avoids having to remember to do "with(plottools)" in the session.
plots[display]([ ptlf([x0, y0], [x0+width, y0], color=c),
ptlf([x0, y0], [x0, y0 + height], color=c),
ptlf([x0+width, y0], [x0+width, y0+height], color=c),
ptlf([x0, y0+height], [x0+width, y0+height], color=c)], title=plotTitle);
end proc;
movie1 := proc(u1, u2, numsteps1, numsteps2, boxColor)
-
7/27/2019 Scripting and Programming
312/415
local indexTab, frames, frames2, i, h, uh;
#initialize tables
frames, frames2:= table(), table();
#part 1 -- draw square u1 units wide moving from (0,0) to (0,1) taking numsteps1 frames.
#h is the step distance
h := (1-0)/numsteps1; #step size of movement
for i from 0 to numsteps1 do
frames[i] := drawBoxB(0,0+i*h,u1, u1, boxColor, "Part 1");
end do;
#part 2 -- draw square growing from u1 units wide
#to u2 units wide in numsteps2 steps,
#moving from (0,1) to (1,1) in numsteps2 frames.
#h is the distance step size
#uh is the size step size
h := 1/numsteps2;uh := (u2-u1)/numsteps2;
frames2 := table();
for i from 0 to numsteps2 do
frames2[i] := drawBoxB(0+h*(i),1,u1+uh*(i), u1+uh*(i), boxColor, "Part 2");
end do;
#Convert tables into lists, then the two lists into sequences
plots[display]([op(convert(frames,list)), op(convert(frames2,list))],
insequence=true);
end proc: #Suppress listing the procedure
#Test 0 Draw a box
printf("Test 0 -- draw a red box\n");
drawBoxB(0,0,1,1,"Red", "Box Test");
#Test 1
#draw red square 1 unit in size changing into
#a square 1.5 units in size.#Part 1 is 11 frames long, Part 2 is 16 frames long.
printf("Test 1 -- do a two part movie\n");
movie1(1, 1.5, 10, 15, "Green");
-
7/27/2019 Scripting and Programming
313/415
restart;
sumIt := proc(x,tol)
local term, s, i; #Variables that will be used within the procedure.
# Initialize script parameters
#tol is the size of the stopping criterion.#x is the value of sin(x) that we are trying to approximate
#Initialize accumulation variables to first term of sum.
term := x;
s := x; #s accumulates the sum, we initialize it to the first term.
#add on successive terms
for i from 3 by 2 while abs(term)>=tol do
term := (-1)^((i-1)/2)*(x^i)/i!;
printf("term is %e\n", term);s := s+term;
end do;
end proc;
#Test 1
x := .3;
tol := 10^(-2); #want at least two decimal digits to be stable.
#Print out the final sum, and compare it to what it's supposed to be.
printf("Test 1, sum is: %e compared to %e.",sumIt(x,tol), sin(x));
#Test 2 -- same argument for x, smaller tolerance.
x := .3;
tol := 10^(-5); #want at least five decimal digits to be stable.
#Print out the final sum, and compare it to what it's supposed to be.
printf("Test 2, sum is: %e compared to %e.",sumIt(x,tol), sin(x));
-
7/27/2019 Scripting and Programming
314/415
term is -4.500000e-03
Test 1, sum is: 2.955000e-01 compared to 2.955202e-01.
term is -4.500000e-03
term is 2.025000e-05
term is -4.339286e-08
Test 2, sum is: 2.955202e-01 compared to 2.955202e-01.
-
7/27/2019 Scripting and Programming
315/415
to 5 doprint("I will not teach others to fly");
end do;
for t from 0 by .1 to 1 do
printf("arccos(%f) = %f\n", t, arccos(t))
end do;
arccos(0.000000) = 1.570796
arccos(0.100000) = 1.470629
arccos(0.200000) = 1.369438
arccos(0.300000) = 1.266104
arccos(0.400000) = 1.159279arccos(0.500000) = 1.047198
arccos(0.600000) = 0.927295
arccos(0.700000) = 0.795399
arccos(0.800000) = 0.643501
arccos(0.900000) = 0.451027
arccos(1.000000) = 0.000000
x := .3: #Parameter
prod := 1: #Accumulation variable
#"sum" and "product" are reserved Maple names
#So we should use something else like "s" or "prod"
#for variables.
#Compute the product of (x+2)*(x+3)*(x+4)*...(x+15)
for i by 2 to 15 do
prod := (x+i)*prod;
end do: #Suppress execution trace inside the loop
print(prod);
-
7/27/2019 Scripting and Programming
316/415
#script parameters
x := .2:
tol := 10^(-6):
#accumulation variables, initialized
i := 1:
s := 1: #First term of sum
term := 1: #Next term of sum
while (abs(term)>=tol) do
term := (x^(i))/(i!);
s := s + term;
i := i+1;
end do:
printf("compare %e to %e\n", s, exp(x));
compare 1.221403e+00 to 1.221403e+00
-
7/27/2019 Scripting and Programming
317/415
#Lab 2 Problem 2.1
restart; #just in case there are unwanted assignments hanging around from
#previous runs.
# Chemical reaction modelled by Lotka-Volterra Model
#Discrete approximation
numTimeSteps := 50;
#Initial concentrations
A0 := 1000;
B0 := 0;
X0 := 10;
Y0 := 50;
#Chemical constants
k1 := .001;
k2 := .001;
k3 := .1;
Atab := table();
indexTab := table();
A := A0; B := B0; X := X0; Y := Y0;
for i from 1 to numTimeSteps do
#Calculate next values of chemical concentrations
newA := A - k1*A*X;
# newX :=
# newY :=# newB :=
#Update all four chemicals
A := newA; #B, X, Y updated similarly.
indexTab[i] := i;
Atab[i] := A; #Each entry is a (time, concentration)
end do:
with(plots): with(plottools):
#Given a table of x coordinates and a table of y coordinates,
#Create a point plot.Fplot := (itab,vtab,COLOR) ->
l t( t(it b li t) t( t b li t) l COLOR b l di d t l i t)
-
7/27/2019 Scripting and Programming
318/415
plot(convert(itab,list),convert(vtab,list), color=COLOR, symbol=diamond, style=point);
#Display this point plot, along with a title and labels.
display([Fplot(indexTab,Atab,"Green")], title="graph of A", labels=["time", "concentration"]);
#We want to find the smallest value in a list of numbers
L := [3, 6, -4, 2, 1, 9];
#minSoFar's role is an accumulator variable:# it keeps track of the "smallest number so far". We initialize it to infinity
# so that whatever we see first will automatically become the smallest so far.
minSoFar := infinity;
#Step through all the positions of L: 1, 2, 3... 6
for i from 1 to nops(L) do
printf("Comparing %a to %d\n", minSoFar, L[i]);
#If the previously seen minimum is larger than L[i]....
if minSoFar>L[i]#Make it the new previously computed minimum
then minSoFar := L[i];
-
7/27/2019 Scripting and Programming
319/415
then minSoFar := L[i];
end if;
end do;
printf("The minimum is: %a.", minSoFar);
Comparing infinity to 3
Comparing 3 to 6
Comparing 3 to -4
Comparing -4 to 2
Comparing -4 to 1
Comparing -4 to 9
The minimum is: -4.
infinity
infinity
-
7/27/2019 Scripting and Programming
320/415
#initialize table
valueTab := table();
#fill up the table
for i from 1 to 13 do
valueTab[i] := i^2;
end do;
#Produce list as result
L := convert(valueTab,list):
printf("L is: %a", L);
L is: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169]
-
7/27/2019 Scripting and Programming
321/415
#initialize table
#Create a sequence
s := seq(i^2,i=1..13);
#Convert it to a list
L := [s];
printf("L is: %a", L);
L is: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169]
-
7/27/2019 Scripting and Programming
322/415
-
7/27/2019 Scripting and Programming
323/415
interestCalculator6 := proc (initialAmt, loRate, hiRate, numYears,
targetAmt)
local i, A, newA, yearTab, amtTab, rate,
frame, frameIndex;
#Initialize frame (a table of plots) and frameIndex (a counter)
frame := table(); #A table of graphs
frameIndex := 0;
#Try for a variety of rates
for rate from loRate by .001 to hiRate do
#initialize A, and tables.
A, yearTab, amtTab := initialAmt, table(), table();
#initialize first entry of table before looping
yearTab[0] := 0;amtTab[0] := initialAmt;
#Calculate new amount based on present amt
#and interest rate.
for i from 1 to numYears while amtTab[i-1]
-
7/27/2019 Scripting and Programming
324/415
-
7/27/2019 Scripting and Programming
325/415
-
7/27/2019 Scripting and Programming
326/415
-
7/27/2019 Scripting and Programming
327/415
restart;
sumIt := proc(x,tol)
local term, s, i; #Variables that will be used within the procedure.
# Initialize script parameters
#tol is the size of the stopping criterion.
#x is the value of sin(x) that we are trying to approximate
#Initialize accumulation variables to first term of sum.
term := x;
s := x; #s accumulates the sum, we initialize it to the first term.
#add on successive terms
for i from 3 by 2 while abs(term)>=tol do
term := (-1)^((i-1)/2)*(x^i)/i!;
printf("term is %e\n", term);
s := s+term;
end do;
end proc;
#Test 1
x := .3;tol := 10^(-2); #want at least two decimal digits to be stable.
#Print out the final sum, and compare it to what it's supposed to be.
printf("Test 1, sum is: %e compared to %e.",sumIt(x,tol), sin(x));
term is -4.500000e-03
-
7/27/2019 Scripting and Programming
328/415
term is 4.500000e 03
Test 1, sum is: 2.955000e-01 compared to 2.955202e-01.
-
7/27/2019 Scripting and Programming
329/415
-
7/27/2019 Scripting and Programming
330/415
-
7/27/2019 Scripting and Programming
331/415
-
7/27/2019 Scripting and Programming
332/415
alpha
-
7/27/2019 Scripting and Programming
333/415
-
7/27/2019 Scripting and Programming
334/415
-
7/27/2019 Scripting and Programming
335/415
-
7/27/2019 Scripting and Programming
336/415
-
7/27/2019 Scripting and Programming
337/415
-
7/27/2019 Scripting and Programming
338/415
-
7/27/2019 Scripting and Programming
339/415
-
7/27/2019 Scripting and Programming
340/415
findMin := proc(expr,var, interval)
-
7/27/2019 Scripting and Programming
341/415
p ( p , , )
local deriv, soln, valAtPoint;
print(plot(expr,var=interval));
deriv := diff(expr,var);
soln := solve(deriv,var);
valAtPoint := eval(expr, var=soln);
return [evalf(valAtPoint), var=soln];
end;
-
7/27/2019 Scripting and Programming
342/415
-
7/27/2019 Scripting and Programming
343/415
-
7/27/2019 Scripting and Programming
344/415
-
7/27/2019 Scripting and Programming
345/415
-
7/27/2019 Scripting and Programming
346/415
-
7/27/2019 Scripting and Programming
347/415
-
7/27/2019 Scripting and Programming
348/415
-
7/27/2019 Scripting and Programming
349/415
-
7/27/2019 Scripting and Programming
350/415
-
7/27/2019 Scripting and Programming
351/415
-
7/27/2019 Scripting and Programming
352/415
-
7/27/2019 Scripting and Programming
353/415
-
7/27/2019 Scripting and Programming
354/415
-
7/27/2019 Scripting and Programming
355/415
-
7/27/2019 Scripting and Programming
356/415
-
7/27/2019 Scripting and Programming
357/415
-
7/27/2019 Scripting and Programming
358/415
-
7/27/2019 Scripting and Programming
359/415
-
7/27/2019 Scripting and Programming
360/415
-
7/27/2019 Scripting and Programming
361/415
-
7/27/2019 Scripting and Programming
362/415
More mathematical modeling: area-finding, piecewise
expressions, and splines
Chapter Overview
We introduce three more computational features from Maple's built-in library:
Computing definite and indefinite integralsPiecewise expressions
Curve fitting using splines
Many scientific or engineering problems benefit from the use of these mathematical concepts.
Recalling basic facts about integration
We review some of the basic facts and terminology about integration from your calculus courses.
Definite integration can be related to finding the area under a curve described by a function. The
definite integral
a
b
f x dx
is notation for this. If an answer for a definite integration problem exists, then it is a number which
could be described exactly or as an approximation.
Indefinite integration is sometimes called theproblem ofantidifferentiation: given a functionf,
find another functionFwhose derivative isf:
d
dxF x = f x .
Another way of writing this relationship is:
-
7/27/2019 Scripting and Programming
363/415
Another way of writing this relationship is:
f x dx =F x .
hard part of doing indefinite integration is finding F(x) without the constant and since the name of
the constant can be freely chosen, systems that can compute antiderivatives often omit the constant
in their answers, letting the user add in the constant of their choice later if they want to.
The answer F(x) is expected to be a formula involving expressions and functions that the user
already knows about. F(x), when it exists, is referred to as a closed form solution to the integration
problem or the antiderivative of f(x). For calculus freshmen, these are expressions that involve
numbers, polynomials, roots, rational functions, trig, logarithm and exponential functions. For
more advanced mathematics students, it might include other functions: erf (error function),Bessel,
G, etc.
The Fundamental Theorem of Calculus (FTC) states that the definite integral can computed if
the antiderivative is known:
a
b
f x dx =F b KF a ,
whereFis any function such that
d
dxF x =f x .
Programs for doing indefinite integration/antidifferentiation have been around from the early '60s.
They can be found in a number of symbolic systems: Maple, Mathematica, Macsyma (now found
in SAGE), and MuPad (now part of Matlab). It is not an easy program to write. Even today in the
era of "open source", the quality and power of such programs varies significantly.
The ideas for how to program an indefinite integrator comes from seminal work done by Robert
Risch in the late 1960s, who was in turn continuing work done previously by other mathematicians
such as Joseph Liouville (1809-1882). Earlier approaches in the early '60s treated symbolic anti-
differentiation as a problem in artificial intelligence. Those AI programs (such as SAINT by James
Slagle (1934-1994) and SIN by Joel Moses (1941-) tried to simulate the expertise of a college
calculus student. However, they would sometimes give up on problems that could be solved
through a clever trick of substitution or other reformulation. Risch's work used advanced
mathematics to come up with programs that would guarantee that they could find a solution if there
was one to be found, for certain classes of integrands. However, while programs using Risch's
techniques are more powerful, they don't necessarily give the answer in simplest terms as a
mathematics textbook would. That may require more symbolic manipulation, under human
guidance.
-
7/27/2019 Scripting and Programming
364/415
g
Indefinite integration
(1.3.1.4)(1.3.1.4)
(1.3.1.1)(1.3.1.1)
(1.3.1.2)(1.3.1.2)
(1.3.1.3)(1.3.1.3)
(1.3.1.5)(1.3.1.5)
Indefinite integration with int
The clickable interface can be used to input an indefinite integration problem for Maple to
compute. Use the fdx item in the Expression Palette, then edit the expressionfand the
variable of integrationx. This computes the antiderivative of the expression, with respect to the
variable of integration.
The general form of the textual version of the integration command is
int( expression , var)
Indefinite integration with int
Example! Commentary1
xdx
ln x
cos x2
dx
1
2cos x sin x C
1
2x
cos2x dx
1
2cos x sin x C
1
2x
The clickable interface can be used to enter the
integration problem.
In the clickable interface there are two ways
of entering what is "cos x times cos x ".
int sqrt x , x
2
3
x3 / 2
int cos a $sina
2, a
1
3cos
3
2a C cos
1
2a
The textual form of integration works in the
same way.
-
7/27/2019 Scripting and Programming
365/415
This uses the textual form of the command ina small script. The script computes the
symbolic integral and stores it as the value of
expr := tan(t) + x^2;i1 := int(expr,t);d1 := diff(i1,t);
difference := d1-expr;printf("difference: %a\n",difference);printf("simplifieddifference: %a\n",
simplify(difference));
tan t Cx2
ln cos t Cx2t
sin tcos t
Cx2
sin t
cos tK tan t
difference: sin(t)/cos(t)-tan(t)simplified difference: 0
Calculus, d1 should be the same as expr.
However, we have to get Maple to simplify
the difference in order to have it actually be
reduced to zero, using the built-in libraryproceduresimplify.
Understanding the answers from int
If Maple does not know how to find an answer it will return an expression that is the display
version of the input expression. It does this instead of giving an error message or NULL so that
a script or program using integration can continue even if the attempt to integrate fails. For
example, even if you don't have the anti-derivative of the integrand, you can still try to do an
approximate numerical definite integration. We discuss that in section 20.4.
Sometimes the answer given by the symbolic integrator will be given in terms of functions that
beginning students haven't heard of. This can be confusing, but unfortunately most systems
written for professionals to use do not have a "beginners mode" that avoids using advanced
-
7/27/2019 Scripting and Programming
366/415
math. If you see a function you don't know of, you can get a brief explanation of it through on-
line help.
Whether or not the answer computed for you is satisfactory depends on the situation. A
To summarize, the responsibilities that users of symbolic integration programs have are a) to be
prepared to learn about more advanced functions if answer is of that form, b) understand when
the computer system is saying that it can't find a closed form solution.
Integration answers using advanced functions
ex2
dx
1
2I p erf Ix
(1.3.2.1)
You wouldn't see this integration problem in
an elementary calculus textbook, because the
answer involves the "error function". The
term "error function" is not indication thatthere is a mistake, It's the official
terminology for the function, in the same way
that "sine", "cotangent" or "exponential" are
names of functions. The name arose from
the function's original use in probability and
statistics. You can read more about the error
function by typing "erf" or "error function"
into Maple's on-line help.
Integration answers when Maple can't find an expression for the anti-derivative
answerdint f x , x
f x dx (1.3.2.2)
answer2 := int(g(t),t);printf("The answer was:%a",answer2);
g t dt
The answer was: int(g(t),t)
Maple can't find the antiderivative because we
haven't told it what the function f is.
It can't find the symbolic integral for g either,
for the same reason. We see thatprintfsays
that the value of answer2 is, in textual form,
the same as the input was.
int 1/ sqrt log sin x C 1C exp x , x Here's another example of an integral that
M l ' d h h h i
-
7/27/2019 Scripting and Programming
367/415
1
ln sin x C 1C ex dx (1.3.2.3)
Maple can't do even though the expression
being integrated is something that itrecognizes.
1.1.
3.3.
(1.3.3.2)(1.3.3.2)
(1.3.3.3)(1.3.3.3)
2.2.
(1.3.3.1)(1.3.3.1)
(1.3.3.4)(1.3.3.4)
Checking that your integration answer is correct
Like everything else produced by a computer, it's still your responsibility to verify that the
computed answer is correct. After all, even if you think it unlikely that there's a programmingerror in int's programming, you might have mis-entered the function you want to integrate.
However, it's easy to use Maple to check integration answers it provides:
Is the derivative of your answer the same or equivalent to what you started with?
Because of the relationship between integration and differentiation, it is possible to check
integration answers without having to do the integration by hand:
Use intto calculate the answer
Apply diffto the answer. The result of differentiation should be the function you were
originally tried to integrate with int.
If the derivative does not look the same as the integrand, trysimplify on the difference
between the result of step 2 and the original expression. If you get zero, the two are
equivalent.
Checking answers
Example Commentary
exprd 1CxCcos x
2
1CxC 12
cos x
answerdint expr, x
xC1
2x
2C
1
2sin x
dAnswerddiff answer, x
1C
xC
1
2 cos x
exprK dAnswer
0
The derivative of the symbolic integral is the
same as the original expression. This is
obvious just by "eyeballing" it, but we can
also confirm that the difference between expr
and the derivative is 0.
exprd sin x2 The derivative of the symbolic integral does
-
7/27/2019 Scripting and Programming
368/415
(1.3.3.5)(1.3.3.5)
p
sin x2
answerdint expr, x
1 1
not appear at first glance to be the same as the
original expression. However, the differencebetween them does simplify to zero, so they
are equivalent Maple does not automatically
(1.3.3.10)(1.3.3.10)
(1.3.3.8)(1.3.3.8)
(1.3.3.13)(1.3.3.13)
(1.3.3.9)(1.3.3.9)
(1.3.3.11)(1.3.3.11)
(1.3.3.12)(1.3.3.12)
dd exprK dAnswer
1
2sin x
2C
1
2cos x
2K
1
2
simplify d
0
exprd x^2C 1 / x 1
x2C 1
xK 1answerd int expr, x
1
2x
2CxC 2ln xK 1
dAnswerddiff answer, x
xC 1C2
xK 1
simplify exprK dAnswer
0
Here is another example of a symbolic
integration answer whose derivative does not
appear to be the same as the starting
expression. However, simplification of thedifference between the two indicates that the
two are equivalent because their difference
simplifies to zero.
Sometimes the "simplify the difference to zero" trick is not needed to check work because it is
obvious that the two are the same. Maple has other simplification operations such as normal,
factor, convert, combine, and expandthat will attempt to put results into alternative forms. Seeon-line help aboutsimplify and the other operations for more information.
Troubleshooting int for indefinite integration problems
intis intended to work with expressions involving the variable of integration, not names of
functions. If an expression does not seem to involve the variable of integration, it is treated as asymbolic constant, regardless of whether it is also the name of a function. This can lead to
misleading "answers"
Variables assigned expressions work with int, but not variables assigned function
definitions
-
7/27/2019 Scripting and Programming
369/415
(1.3.4.1)(1.3.4.1)
badResultd cos dt
cost
Since the integrand is just cos, and does not
involve the variable of integration, Mapletreats it as a symbolic constant. Since