Analisa Packham & Jeffrey MeyersMaxima and Minima on a Finite Intervalrestart;with(Maplets[Tools]): with(Maplets[Elements]): with(plots):
StartEngine();randomize():lightorange:="#FFB300":
lightcyan:="#DDFFFF":ColorFooter:=lightcyan:
FontFooter:=Font("helvetica",10):rand4:=rand(1..4):
rand5:=rand(1..5):
rand8:=rand(4..8):
rand9:=rand(-9..9):
rand55:=rand(-5..5):new:=proc()
global c1, c2, f, fp, minimum, maximum;
local a, c, b, d, xmin, ymin, xmax, ymax, x1;
a:=rand4();
c1:=rand9();
c2:=rand9();
c:=rand9();
fp:=sort(expand(a*(x-c1)*(x-c2)));
f:=sort(int(fp,x)+c);
b:=rand55();
d:=b+rand8();
#print(b,c1,c2,d);
#print(eval(f, x=b), eval(f, x=c1), eval(f, x=c2), eval(f, x=d));
while nops({eval(f, x=b), eval(f, x=c1), eval(f, x=c2), eval(f, x=d)})
< nops({b, c1, c2, d}) do
b:=rand55();
d:=b+rand8();
# print(b,c1,c2,d);
# print(eval(f, x=b), eval(f, x=c1), eval(f, x=c2), eval(f, x=d));
end do;
xmin:=b; ymin:=eval(f, x=b);
xmax:=b; ymax:=eval(f, x=b);
for x1 in ({c1,c2,d} minus {b}) do
if x1<b or x1>d then next end if;
if eval(f, x=x1)<ymin then
xmin:=x1; ymin:=eval(f, x=x1);
end if;
if eval(f, x=x1)>ymax then
xmax:=x1; ymax:=eval(f, x=x1);
end if;
end do;
minimum:=[xmin,ymin];
maximum:=[xmax,ymax];
#print(minimum,maximum);
Set(showf = MathML[Export](f));
Set(interval = [b,d]);
Set(reply="");
Set(deriv="");
Set(critpoint=" [ , ] ");
Set(testcritpoint="");
Set(critpointvalue="");
Set(max=" [ , ] ");
Set(min=" [ , ] ");
end proc:check_derivative:=proc()
global fp;
local userfp;
userfp:=Get(deriv::anything):
if simplify(fp-userfp)=0
then Set(reply="Correct derivative!")
else Set(reply="You're almost there. Keep trying.")
end if;
end proc:show_derivative:=proc()
global fp;
Set(deriv=fp);
Set(reply="Try one by yourself next time.");
end proc:check_critpoint:=proc()
global c1, c2;
local usercritpts,correctcritpts;
usercritpts:=Get(critpoint::[realcons,realcons]):
if (evalf[4](c1-usercritpts[1])=0. and
evalf[4](c2-usercritpts[2])=0.)
or (evalf[4](c2-usercritpts[1])=0. and
evalf[4](c1-usercritpts[2])=0.)
then Set(reply="Great Job!")
else Set(reply="Incorrect.")
end if;
end proc:show_critpoint:=proc()
global c1, c2;
Set(critpoint=sort([c1,c2]));
Set(reply="Try one by yourself next time.");
end proc:eval_critpoint:=proc()
global f;
local fa, usercritpt;
usercritpt:=Get(testcritpoint::realcons):
fa:=eval(f,x=usercritpt);
Set(critpointvalue=evalf[4](fa));
end proc:check_min:=proc()
global minimum;
local usermin;
usermin:=Get(min::[realcons,realcons]):
if (evalf[4](minimum[1]-usermin[1])=0. and
evalf[4](minimum[2]-usermin[2])=0.)
then Set(reply="Oh snap! You got it right!")
else Set(reply="You're almost there. Try again.")
end if;
end proc:show_min:=proc()
global minimum;
Set(min=evalf[4](minimum));
Set(reply="Try one by yourself next time.");
end proc:check_max:=proc()
global maximum;
local usermax;
usermax:=Get(max::[realcons,realcons]):
if (evalf[4](maximum[1]-usermax[1])=0. and
evalf[4](maximum[2]-usermax[2])=0.)
then Set(reply="You're doing awesome! Try another function.")
else Set(reply="Not quite. Try again.")
end if;
end proc:show_max:=proc()
global maximum;
Set(max=evalf[4](value(maximum)));
Set(reply="Try one by yourself next time.");
end proc:MaxMinFinInter:=Maplet(onstartup=RunWindow(MAIN),
Window[MAIN](title="Maxima and Minima on a Finite Interval",
defaultbutton=BEval,
[ halign=none,
[ Button("New Function", Evaluate(function = "new"), background=magenta),
HorizontalGlue(),
Button("Close", Shutdown(), background=pink)
],
BoxColumn( border=true,
[ halign=none,
"Find the critical points of the function: f(x) =",
MathMLViewer[showf](width=200, height=50),
HorizontalGlue()
],
[ halign=none,
"and determine the maximum and minimum on the interval:",
TextField[interval]("", width=20),
HorizontalGlue()
]
),
[ halign=none,
"f '(x)= ",
TextField[deriv]("", width=30),
HorizontalGlue(),
Button("Check", Evaluate(function="check_derivative"), background=green),
Button("Show Answer", Evaluate(function="show_derivative"), background=lightorange)
],
[ halign=none,
"List the Critical Points: ",
TextField[critpoint](" [ , ] ", width=20),
HorizontalGlue(),
Button("Check", Evaluate(function= "check_critpoint"), background=green),
Button("Show Answer", Evaluate(function="show_critpoint"), background=lightorange)
],
[ halign=none,
border=true,
caption="Find the function value at each critical point and endpoint:",
"Type x and click Evaluate.",
HorizontalGlue(),
"f(",
TextField[testcritpoint]("", width=4),
") =",
TextField[critpointvalue]("", width=10),
Button[BEval]("Evaluate", Evaluate(function="eval_critpoint"), background=lightorange),
HorizontalGlue()
],
[ halign=none,
"Minimum: [x, f(x)] =",
TextField[min](" [ , ] "),
HorizontalGlue(),
Button("Check", Evaluate(function="check_min"), background=green),
Button("Show Answer", Evaluate (function="show_min"), background=lightorange)
],
[ halign=none,
"Maximum: [x, f(x)] =",
TextField[max](" [ , ] "),
HorizontalGlue(),
Button("Check", Evaluate(function="check_max"), background=green),
Button("Show Answer", Evaluate(function="show_max"), background=lightorange)
],
[ TextField[reply](width=50,editable=false)
],
[ halign=none, background=ColorFooter,
Label("Programmers: Analisa Packham & Jeffrey Meyers", font=FontFooter),
HorizontalGlue(),
Label("Copyright: P. Yasskin 2008", font=FontFooter)
]
]
)
):Maplets[Display](MaxMinFinInter);