James Grubbs & Jesse LopezMaking Piecewise Functions Continuous and DifferentiableMath 151 HonorsFall 2011Instructor: Philip Yasskinrestart:with(Maplets[Tools]): with(Maplets[Elements]): with(plots):
StartEngine();randa:=rand(-3..3):randpow:=rand(0..3):NewLimit:=proc()
global funcleft, funcright, a,func, LimLeft,LimRight,DerLeft,DerRight, LimDerLeft, LimDerRight,P,Q;
local sol, p1, p2, p3;
a:=randa();
sol:=[];
while sol=[] do
funcleft:=sort(collect(randpoly(x, terms = 2, degree=3, coeffs=rand(-5..5))+p*x^randpow(),x));
funcright:=sort(collect(randpoly(x, terms = 2, degree=3, coeffs=rand(-5..5))+q*x^randpow(),x));
func:=piecewise(x<a,funcleft,a<x,funcright);
LimLeft:=eval(funcleft,x=a);
LimRight:=eval(funcright,x=a);
DerLeft:=diff(funcleft,x);
DerRight:=diff(funcright,x);
LimDerLeft:=eval(DerLeft,x=a);
LimDerRight:=eval(DerRight,x=a);
sol:=solve([LimLeft=LimRight, LimDerLeft=LimDerRight],[p,q]);
end do;
P,Q:=op(eval([p,q],sol[1]));
Set(MMLLim(value)=(f(x)=func));
Set(TFLimLeft="");
Set(TFLimRight="");
Set(TFLimDerLeft="");
Set(TFLimDerRight="");
Set(TFp="");
Set(TFq="");
Set(replyLimLeftRight="", replyLimLeftRight(background)=white);
Set(replyLimDerLeftRight="", replyLimDerLeftRight(background)=white);
Set(replyPQ="", replyPQ(background)=white);
Set(reply="", reply(background)=white);
Set(BPlot(enabled)=false);
end proc:HintLimLeftRight:=proc()
Set(reply="Take the limits of both sides and set them equal to each other"):
Set(reply(background)=Orange)
end proc:CheckLimLeftRight:=proc()
global a,func, LimLeft,LimRight,DerLeft,DerRight, LimDerLeft, LimDerRight,P,Q;
local user_LimLeft, user_LimRight;
user_LimLeft:=Get(TFLimLeft::algebraic, corrections=true, update=true);
user_LimRight:=Get(TFLimRight::algebraic, corrections=true, update=true);
if user_LimLeft=LimLeft and user_LimRight=LimRight then
Set(replyLimLeftRight="Correct", replyLimLeftRight(background)=green);
Set(reply="Great job! Now try step 2.", reply(background)=green);
elif user_LimLeft=LimLeft then
Set(replyLimLeftRight="Incorrect", replyLimLeftRight(background)=red);
Set(reply="There is an error in your limit from the right. Try again.", reply(background)=red);
elif user_LimRight=LimRight then
Set(replyLimLeftRight="Incorrect", replyLimLeftRight(background)=red);
Set(reply="There is an error in your limit from the left. Try again.", reply(background)=red);
else
Set(replyLimLeftRight="Incorrect", replyLimLeftRight(background)=red);
Set(reply="Neither of your limits are correct. Please try again.", reply(background)=red);
end if;
end proc:ShowLimLeftRight:=proc()
global a,func, LimLeft,LimRight,DerLeft,DerRight, LimDerLeft, LimDerRight,P,Q;
Set(TFLimLeft=LimLeft);
Set(TFLimRight=LimRight);
Set(replyLimLeftRight="Shown", replyLimLeftRight(background)=yellow);
Set(reply="Now try one on your own.", reply(background)=yellow);
end proc:HintLimDerLeftRight:=proc()
Set(reply="Take the DERIVATIVE of both sides and set them equal together"):
Set(reply(background)=Orange)
end proc:CheckLimDerLeftRight:=proc()
global a,func, LimLeft,LimRight,DerLeft,DerRight, LimDerLeft, LimDerRight;
local user_LimDerLeft, user_LimDerRight;
user_LimDerLeft:=Get(TFLimDerLeft::algebraic, corrections=true, update=true);
user_LimDerRight:=Get(TFLimDerRight::algebraic, corrections=true, update=true);
if user_LimDerLeft=LimDerLeft and user_LimDerRight=LimDerRight then
Set(replyLimDerLeftRight="Correct", replyLimDerLeftRight(background)=green);
Set(reply="Awesome! Now try solving for P and Q", reply(background)=green);
elif user_LimDerLeft=LimDerLeft then
Set(replyLimDerLeftRight="Incorrect", replyLimDerLeftRight(background)=red);
Set(reply="Sorry, there is an error in your limit of the derivative from the right.", reply(background)=red);
elif user_LimDerRight=LimDerRight then
Set(replyLimDerLeftRight="Incorrect", replyLimDerLeftRight(background)=red);
Set(reply="Sorry, there is an error in your limit of the derivative from the left.", reply(background)=red);
else
Set(replyLimDerLeftRight="Incorrect", replyLimDerLeftRight(background)=red);
Set(reply="Both of your limits of the derivative are incorrect. Please retry this step.", reply(background)=red);
end if;
end proc:ShowLimDerLeftRight:=proc()
global a,func, LimLeft,LimRight,DerLeft,DerRight, LimDerLeft, LimDerRight,P,Q;
Set(TFLimDerLeft=LimDerLeft);
Set(TFLimDerRight=LimDerRight);
Set(replyLimDerLeftRight="Shown", replyLimDerLeftRight(background)=yellow);
Set(reply="Now try one on your own.", reply(background)=yellow);
end proc:PrevPQ:=proc()
global funcleft, funcright, a,func, LimLeft,LimRight,DerLeft,DerRight, LimDerLeft;
local user_p, user_q, p1,p2,p3,L,L1,L2;
user_p:=Get(TFp::algebraic, corrections=true, update=true);
user_q:=Get(TFq::algebraic, corrections=true, update=true);
L1:=abs(eval(funcleft, [p=user_p,x=a]));
L2:=abs(eval(funcright, [q=user_q,x=a]));
L:=max(L1,L2);
p1:=plot(eval(funcleft, p=user_p), x=-4..a, -2*L..2*L, thickness=3, color=blue);
p2:=plot(eval(funcright, q=user_q), x=a..4, -2*L..2*L, thickness=3, color=red);
p3:=display([p1,p2]);
Set(myplot=p3);
Set(A1(run)=true);
end proc:HintPQ:=proc()
Set(reply="By using the system of equations above you can solve for P and Q"):
Set(reply(background)=Orange)
end proc:CheckPQ:=proc()
global a,func, LimLeft,LimRight,DerLeft,DerRight, LimDerLeft, LimDerRight,P,Q;
local user_p, user_q;
user_p:=Get(TFp::algebraic, corrections=true, update=true);
user_q:=Get(TFq::algebraic, corrections=true, update=true);
if user_p=P and user_q=Q then
Set(replyPQ="Correct", replyPQ(background)=green);
Set(reply="You got everything right you deserve 50 COOKIES!!!!!", reply(background)=green);
Set(BPlot(enabled)=true);
elif user_p=P then
Set(replyPQ="Incorrect", replyPQ(background)=red);
Set(reply="Sorry, your value for q is incorrect. Please try again.", reply(background)=red);
elif user_q=Q then
Set(replyPQ="Incorrect", replyPQ(background)=red);
Set(reply="Sorry, your value for p is incorrect. Please try again.", reply(background)=red);
else
Set(replyPQ="Incorrect", replyPQ(background)=red);
Set(reply="Sorry, your values for p and q are incorrect. Please try again.", reply(background)=red);
end if;
end proc:ShowPQ:=proc()
global a,func, LimLeft,LimRight,DerLeft,DerRight, LimDerLeft, LimDerRight,P,Q;
Set(TFp=P);
Set(TFq=Q);
Set(BPlot(enabled)=true);
Set(replyPQ="Shown", replyPQ(background)=yellow);
Set(reply="Now try one on your own.", reply(background)=yellow);
end proc:DPWF:= Maplet(onstartup = RunWindow(MAIN),
Window[MAIN](title="Making Piecewise Functions Continuous and Differentiable",
[
[
Button("New Limit", Evaluate(function="NewLimit"), background=magenta),
HorizontalGlue(),
HorizontalGlue(),
Button("Quit", Shutdown(), background=pink)
],
[ border=true, halign=none,
caption="Goal: Find the values of p and q which make the function differentiable.",
" ",
" ",
MathMLViewer[MMLLim](value = MathML[Export](" "), width=300, height=60),
" ",
" "
],
[ border=true, halign=none,
caption="Step 1: Find the limits from the left and right and set them equal:",
TextField[TFLimLeft](""),
"=",
TextField[TFLimRight](""),
HorizontalGlue(),
Button("Hint", Evaluate(function="HintLimLeftRight"), background="Orange"),
Button("Check", Evaluate(function="CheckLimLeftRight"), background=green),
TextField[replyLimLeftRight]("",width=5,editable=false,background=white),
Button("Show", Evaluate(function="ShowLimLeftRight"), background=yellow)
],
[ border=true, halign=none,
caption="Step 2: Find the limits of the derivative from the left and right and set them equal:",
TextField[TFLimDerLeft](""),
"=",
TextField[TFLimDerRight](""),
HorizontalGlue(),
Button("Hint", Evaluate(function="HintLimDerLeftRight"), background="Orange"),
Button("Check", Evaluate(function = "CheckLimDerLeftRight"), background=green),
TextField[replyLimDerLeftRight]("",width=5, editable=false, background=white),
Button("Show", Evaluate(function = "ShowLimDerLeftRight"), background=yellow)
],
[ border=true, halign=none,
caption="Step 3: Solve for p and q:",
HorizontalGlue(),
"p =",
TextField[TFp]("",width=5),
HorizontalGlue(),
"q =",
TextField[TFq]("",width=5),
HorizontalGlue(),
Button("Preview", Evaluate(function="PrevPQ"), background="Orange"),
Button("Hint", Evaluate(function="HintPQ"), background="Orange"),
Button("Check", Evaluate(function="CheckPQ"), background=green),
TextField[replyPQ]("",width=5, editable=false, background=white),
Button("Show", Evaluate(function="ShowPQ"), background=yellow)
],
[ halign=none,
HorizontalGlue(),
TextBox[reply](width=60,height=2,editable=false),
HorizontalGlue(),
Button[BPlot]("Plot It", Evaluate(function="PrevPQ"), enabled=false, background=tan)
],
[ inset=0, spacing=0,
"Instructor: Philip Yasskin",
"Programmers: James Grubbs & Jesse Lopez",
"Copyright: Yasskin 2011"
]
]
),
Window[PlotIt](title="The Plot",
[
[ background=tan,
Plotter[myplot](width=300, height=300)
],
[
Button("Close", CloseWindow(PlotIt), background=pink)
]
]
),
Action[A1](RunWindow(PlotIt))
):Maplets[Display](DPWF);