# Howard Nathan Naylor, Michelle Andrea Florez # Making a Piecewise Function Differentiable # Math 151H-202 # Fall 2011 # Instructor: Phillip Yasskin restart: with(Maplets[Tools]): with(Maplets[Elements]): with(plots): StartEngine(): randomize(): rand55:=rand(-5..5): rand33:=rand(-3..3): pairs:=[['''b''','''c'''],['''b''','''f'''],['''e''','''c'''],['''e''','''f''']]: randpairs:=rand(1..4): vars:=[[C,K],[K,C]]: randvars:=rand(1..2): NewFunction:=proc() global x0,fun,a,b,c,d,e,f,p,q,vars,var1,var2,Cval,Kval,plotfun, Left,Right,LeftFn,RightFn,DerLess,DerMore,LeftDer,RightDer; local sol, pf, pp, p1; x0:=rand33(); a,b,c,d,e,f:=rand55(),rand55(),rand55(),rand55(),rand55(),rand55(); p,q:=op(pairs[randpairs()]); var1,var2:=op(vars[randvars()]); assign(p=var1,q=var2); Left:=a*x^2+b*x+c; Right:=d*x^2+e*x+f; fun:=piecewise(x < x0, Left, x > x0, Right); LeftFn:=eval(Left,x=x0); RightFn:=eval(Right,x=x0); DerLess:=2*a*x+b; DerMore:=2*d*x+e; LeftDer:=eval(DerLess,x=x0); RightDer:=eval(DerMore,x=x0); sol:=solve({LeftFn=RightFn,LeftDer=RightDer},{C,K}); Cval,Kval:=op(eval([C,K],sol)); plotfun:=eval(fun,[C=Cval,K=Kval]); pf:=plot(plotfun,x=-5..5, color=green, thickness=2): pp:=plot([[x0,limit(plotfun,x=x0)]], style=point, symbol=circle, symbolsize=16, color=blue, thickness=2): p1:=display(pf,pp); Set(MMLfun=('f'(x)=fun)); Set(MMLLeftFn(value)=Limit('f'(x),x=x0,left)); Set(MMLRightFn(value)=Limit('f'(x),x=x0,right)); Set(LDerLess(caption)=sprintf("If x < %a, df/dx =", 'a')); Set(LDerMore(caption)=sprintf("If x > %a, df/dx =", 'a')); Set(MMLLeftDer(value)=Limit(Diff('f',x),x=x0,left)); Set(MMLRightDer(value)=Limit(Diff('f',x),x=x0,right)); Set(reply="", reply(background)=white); Set(replyCK="", replyCK(background)=white); Set(replyDerLim="", replyDerLim(background)=white); Set(replyDer="", replyDer(background)=white); Set(replyFn="", replyFn(background)=white); Set(TFKval=""); Set(TFCval=""); Set(TFDerMore=""); Set(TFDerLess=""); Set(TFRightDer=""); Set(TFLeftDer=""); Set(TFLeftFn=""); Set(TFRightFn=""); Set(BPlot(enabled)=false); Set(myplot=p1); end proc: HintTop:=proc() Set(reply = "For a function to be differentiable\nthe limits from the Left and Right of f(x) must be equal,\nand the limits from the Left and Right of f'(x) must be equal."): Set(reply(background)="Orange"): end proc: HintFn:=proc() global x0,fun,a,b,c,d,e,f,p,q,vars,var1,var2,Cval,Kval,plotfun, Left,Right,LeftFn,RightFn,DerLess,DerMore,LeftDer,RightDer; Set(reply = sprintf(" The limit of the function from the left is the value of the function for x < %a evaluated at x= %a.\n The limit of the function from the right is the value of the function for x > %a evaluated at x= %a.", x0, x0, x0, x0)): Set(reply(background)="Orange"): end proc: HintDer:=proc() Set(reply = "Simply differentiate the two functions"): Set(reply(background)="Orange"): end proc: HintDerLim:=proc() Set(reply = sprintf("Take the limits of the derivatives of the functions as x approaches %a.", x0)): Set(reply(background)="Orange"): end proc: HintCK:=proc() Set(reply = "Equate the limits of the functions and the limits of the derivatives and solve for C and K."): Set(reply(background)="Orange"): end proc: CheckFn:=proc() global x0,fun,a,b,c,d,e,f,p,q,vars,var1,var2,Cval,Kval, Left,Right,LeftFn,RightFn,DerLess,DerMore,LeftDer,RightDer; local user_LeftFn,user_RightFn; user_LeftFn:=Get(TFLeftFn::polynom, corrections=true, update=true): user_RightFn:=Get(TFRightFn::polynom, corrections=true, update=true): if user_LeftFn-LeftFn=0 and user_RightFn-RightFn=0 then Set(replyFn="Correct", replyFn(background)=green, replyFn(foreground)=black); Set(reply="Great job. Go on to the next step.", reply(background)=green, reply(foreground)=black); elif user_LeftFn-LeftFn=0 then Set(replyFn="Incorrect", replyFn(background)=red, replyFn(foreground)=white); Set(reply="There is something wrong with the limit from the right. Try again.", reply(background)=red, reply(foreground)=white); elif user_RightFn-RightFn=0 then Set(replyFn="Incorrect", replyFn(background)=red, replyFn(foreground)=white); Set(reply="There is something wrong with the limit from the left. Try again.", reply(background)=red, reply(foreground)=white); else Set(replyFn="Incorrect", replyFn(background)=red, replyFn(foreground)=white); Set(reply="There is something wrong with both limits. Try again.", reply(background)=red, reply(foreground)=white); end if; end proc: ShowFn:=proc() global x0,fun,a,b,c,d,e,f,p,q,vars,var1,var2,Cval,Kval, Left,Right,LeftFn,RightFn,DerLess,DerMore,LeftDer,RightDer; Set(TFLeftFn=LeftFn); Set(TFRightFn=RightFn); Set(replyFn="Shown", replyFn(background)="yellow", replyFn(foreground)=black); Set(reply="", reply(background)=white); end proc: CheckDer:=proc() global x0,fun,a,b,c,d,e,f,p,q,vars,var1,var2,Cval,Kval, Left,Right,LeftFn,RightFn,DerLess,DerMore,LeftDer,RightDer; local user_DerLess,user_DerMore; user_DerLess:=Get(TFDerLess::polynom, corrections=true, update=true): user_DerMore:=Get(TFDerMore::polynom, corrections=true, update=true): if user_DerLess-DerLess=0 and user_DerMore-DerMore=0 then Set(replyDer="Correct", replyDer(background)=green, replyDer(foreground)=black); Set(reply="Great job. Go on to the next step.", reply(background)=green, reply(foreground)=black); elif user_DerLess-DerLess=0 then Set(replyDer="Incorrect", replyDer(background)=red, replyDer(foreground)=white); Set(reply="There is something wrong with the derivative on the right. Try again.", reply(background)=red, reply(foreground)=white); elif user_DerMore-DerMore=0 then Set(replyDer="Incorrect", replyDer(background)=red, replyDer(foreground)=white); Set(reply="There is something wrong with the derivative on the left. Try again.", reply(background)=red, reply(foreground)=white); else Set(replyDer="Incorrect", replyDer(background)=red, replyDer(foreground)=white); Set(reply="There is something wrong with both derivatives. Try again.", reply(background)=red, reply(foreground)=white); end if; end proc: ShowDer:=proc() global x0,fun,a,b,c,d,e,f,p,q,vars,var1,var2,Cval,Kval, Left,Right,LeftFn,RightFn,DerLess,DerMore,LeftDer,RightDer; Set(TFDerLess=DerLess); Set(TFDerMore=DerMore); Set(replyDer="Shown", replyDer(background)="yellow", replyDer(foreground)=black); Set(reply="", reply(background)=white); end proc: CheckDerLim:=proc() global x0,fun,a,b,c,d,e,f,p,q,vars,var1,var2,Cval,Kval, Left,Right,LeftFn,RightFn,DerLess,DerMore,LeftDer,RightDer; local user_LeftDer,user_RightDer; user_LeftDer:=Get(TFLeftDer::polynom, corrections=true, update=true): user_RightDer:=Get(TFRightDer::polynom, corrections=true, update=true): if user_LeftDer-LeftDer=0 and user_RightDer-RightDer=0 then Set(replyDerLim="Correct", replyDerLim(background)=green, replyDerLim(foreground)=black); Set(reply="Great job. Go on to the next step.", reply(background)=green, reply(foreground)=black); elif user_LeftDer-leftDer=0 then Set(replyDerLim="Incorrect", replyDerLim(background)=red, replyDerLim(foreground)=white); Set(reply="There is something wrong with the limit from the right. Try again.", reply(background)=red, reply(foreground)=white); elif user_RightDer-RightDer=0 then Set(replyDerLim="Incorrect", replyDerLim(background)=red, replyDerLim(foreground)=white); Set(reply="There is something wrong with the limit from the left. Try again.", reply(background)=red, reply(foreground)=white); else Set(replyDerLim="Incorrect", replyDerLim(background)=red, replyDerLim(foreground)=white); Set(reply="There is something wrong with both limits. Try again.", reply(background)=red, reply(foreground)=white); end if; end proc: ShowDerLim:=proc() global x0,fun,a,b,c,d,e,f,p,q,vars,var1,var2,Cval,Kval, Left,Right,LeftFn,RightFn,DerLess,DerMore,LeftDer,RightDer; Set(TFLeftDer=LeftDer); Set(TFRightDer=RightDer); Set(replyDerLim="Shown", replyDerLim(background)="yellow", replyDerLim(foreground)=black); Set(reply="", reply(background)=white); end proc: CheckCK:=proc() global x0,fun,a,b,c,d,e,f,p,q,vars,var1,var2,Cval,Kval, Left,Right,LeftFn,RightFn,DerLess,DerMore,LeftDer,RightDer; local user_Cval,user_Kval; user_Cval:=Get(TFCval::polynom, corrections=true, update=true): user_Kval:=Get(TFKval::polynom, corrections=true, update=true): if user_Cval-Cval=0 and user_Kval-Kval=0 then Set(replyCK="Correct", replyCK(background)=green, replyCK(foreground)=black); Set(reply="Great job. Go on to the next step.", reply(background)=green, reply(foreground)=black); Set(BPlot(enabled)=true); elif user_Cval-Cval=0 then Set(replyCK="Incorrect", replyCK(background)=red, replyCK(foreground)=white); Set(reply="There is something wrong with K. Try again.", reply(background)=red, reply(foreground)=white); elif user_Kval-Kval=0 then Set(replyCK="Incorrect", replyCK(background)=red, replyCK(foreground)=white); Set(reply="There is something wrong with C. Try again.", reply(background)=red, reply(foreground)=white); else Set(replyCK="Incorrect", replyCK(background)=red, replyCK(foreground)=white); Set(reply="There is something wrong with both C and K. Try again.", reply(background)=red, reply(foreground)=white); end if; end proc: ShowCK:=proc() global x0,fun,a,b,c,d,e,f,p,q,vars,var1,var2,Cval,Kval, Left,Right,LeftFn,RightFn,DerLess,DerMore,LeftDer,RightDer; Set(TFCval=Cval); Set(TFKval=Kval); Set(replyCK="Shown", replyCK(background)="yellow", replyCK(foreground)=black); Set(BPlot(enabled)=true); Set(reply="", reply(background)=white); end proc: PreviewCK:=proc() global x0,fun,a,b,c,d,e,f,p,q,vars,var1,var2,Cval,Kval, plotfun,user_Cval,user_Kval,user_plotfun,Left,Right, LeftFn,RightFn,DerLess,DerMore,LeftDer,RightDer; local sol, pf, pp1,pp2, p1; user_Cval:=Get(TFCval::polynom, corrections=true, update=true): user_Kval:=Get(TFKval::polynom, corrections=true, update=true): user_plotfun:=eval(fun,[C=user_Cval,K=user_Kval]); pf:=plot(user_plotfun,x=-5..5, color=green, thickness=2, discont=true): pp1:=plot([[x0,limit(user_plotfun,x=x0, left)]], style=point, symbol=circle, symbolsize=16, color=blue, thickness=3): pp2:=plot([[x0,limit(user_plotfun,x=x0, right)]], style=point, symbol=circle, symbolsize=16, color=blue, thickness=3): p1:=display(pf,pp1,pp2); Set(myplot=p1); Set(A1(run)=true); end proc: DP:=Maplet(onstartup = RunWindow(MAIN), Window[MAIN](title="Making a Piecewise Function Differentiable", [ [ Button("New Function", Evaluate(function="NewFunction"), background=cyan), HorizontalGlue(), Button("Hint", Evaluate(function="HintTop"), background="Orange"), Button("Quit", Shutdown(), background=cyan) ], [ border=true, caption="Find the values of C and K which make the following function differentiable.", " ", " ", " ", MathMLViewer[MMLfun](value=('f'(x)=piecewise(x < 2, x^2+K, x >2, 3-C*x)), width=250, height=75), " ", " ", " " ], [ border=true, halign=none, caption="Step 1: What are the limits of the function from the left and right?", MathMLViewer[MMLLeftFn](value=Limit(` f`(x),x='a',left), width=100, height=60), "=", TextField[TFLeftFn](width=10, background=turquoise), " ", MathMLViewer[MMLRightFn](value=Limit(` f`(x),x='a',right), width=100, height=60), "=", TextField[TFRightFn](width=10, background=turquoise), " ", Button("Hint", Evaluate(function="HintFn"), background="Orange"), Button("Check", Evaluate(function="CheckFn"), background=green), TextField[replyFn](width=6, background=white), Button("Show", Evaluate(function="ShowFn"), background=yellow) ], [ border=true, halign=none, caption="Step 2: Compute the derivative of f:", Label[LDerLess](sprintf("If x < %a, df/dx =", 'a')), TextField[TFDerLess](width=10, background=turquoise), " and ", Label[LDerMore](sprintf("If x > %a, df/dx =", 'a')), TextField[TFDerMore](width=10, background=turquoise), HorizontalGlue(), Button("Hint", Evaluate(function="HintDer"), background="Orange"), Button("Check", Evaluate(function="CheckDer"), background=green), TextField[replyDer](width=6, background=white), Button("Show", Evaluate(function="ShowDer"), background=yellow) ], [ border=true, halign=none, caption="Step 3: What are the limits of the derivative from the left and right?", MathMLViewer[MMLLeftDer](value=Limit(Diff('f',x),x='a',left), width=100, height=60), "=", TextField[TFLeftDer](width=6, background=turquoise), " ", MathMLViewer[MMLRightDer](value=Limit(Diff('f',x),x='a',right), width=100, height=60), "=", TextField[TFRightDer](width=6, background=turquoise), " ", Button("Hint", Evaluate(function="HintDerLim"), background="Orange"), Button("Check", Evaluate(function="CheckDerLim"), background=green), TextField[replyDerLim](width=6, background=white), Button("Show", Evaluate(function="ShowDerLim"), background=yellow) ], [ border=true, halign=none, caption="Step 4: Find the values of C and K:", HorizontalGlue(), "C =", TextField[TFCval](width=6, background=turquoise), HorizontalGlue(), "K =", TextField[TFKval](width=6, background=turquoise), HorizontalGlue(), Button("Preview", Evaluate(function="PreviewCK"), background=SteelBlue, foreground=white), Button("Hint", Evaluate(function="HintCK"), background="Orange"), Button("Check", Evaluate(function="CheckCK"), background=green), TextField[replyCK](width=6, background=white), Button("Show", Evaluate(function="ShowCK"), background=yellow) ], [ halign=none, TextBox[reply](width=60, height=4), HorizontalGlue(), Button[BPlot]("Plot", enabled=false, Evaluate(function="PreviewCK"), background=tan) ], [inset=0, spacing=0, "Instructor: Philip Yasskin", "Programmers: Howard Nathan Naylor and Michelle Andrea Florez", "Copyright: Yasskin 2011" ] ] ), Window[PLOT](title="Plot of f(x)", [ [Plotter[myplot](width=300, height=300) ], Button("Quit", CloseWindow(PLOT), background=cyan) ] ), Action[A1](RunWindow(PLOT)) ): Maplets[Display](DP);