Computing Derivatives Using DataAndrew Mora and Cory LuskMath 151 Honors Section Fall 2013Instructor: Philip Yasskin
I renamed some variables.I made all the values nonzero so you would not need to redefine them for the quotient rule.The power rules need p=power, but this can be done always, not just cases 6 & 7.Hints would have been nice.restart;with(Maplets[Tools]): with(Maplets[Elements]):
StartEngine();with(RandomTools):randomize():funcs:=[f(x)*g(x),
f(x)/g(x), g(x)/f(x),
f(g(x)), g(f(x)),
f(x)^p, g(x)^p]:Dhs:=[f(x)*Dg(x)+g(x)*Df(x),
(g(x)*Df(x)-f(x)*Dg(x))/g(x)^2, (f(x)*Dg(x)-g(x)*Df(x))/f(x)^2,
Df(g(x))*Dg(x), Dg(f(x))*Df(x),
p*(f(x)^(p-1))*Df(x), p*(g(x)^(p-1))*Dg(x)]:Dhvals:=[Df0*g0+f0*Dg0,
(g0*Df0-f0*Dg0)/g0^2, (f0*Dg0-g0*Df0)/f0^2,
Df0*Dg0, Dg0*Df0,
p*f0^(p-1)*Df0, p*g0^(p-1)*Dg0]:randfunc:=rand(1..nops(funcs)):randpower:=rand(2..4):xvals:=[2,3,4]:
randx:=rand(1..3):randcr:=rand(2..4):NewProb:=proc()
global Dh,Dhval;
local func,f1,Df1,g1,Dg1,f2,Df2,g2,Dg2,f3,Df3,g3,Dg3,whichfunc,whichx,x0,power;
power:=randpower();
whichfunc:=randfunc();
func:=eval(funcs[whichfunc],[p=power]);
whichx:=randx();
x0:=xvals[whichx];
f1:=Generate(nonzeroint(range=-10..10));
Df1:=Generate(nonzeroint(range=-10..10));
g1:=Generate(nonzeroint(range=-10..10));
Dg1:=Generate(nonzeroint(range=-10..10));
f2:=Generate(nonzeroint(range=-10..10));
Df2:=Generate(nonzeroint(range=-10..10));
g2:=Generate(nonzeroint(range=-10..10));
Dg2:=Generate(nonzeroint(range=-10..10));
f3:=Generate(nonzeroint(range=-10..10));
Df3:=Generate(nonzeroint(range=-10..10));
g3:=Generate(nonzeroint(range=-10..10));
Dg3:=Generate(nonzeroint(range=-10..10));
Set(TFf1=f1);
Set(TFDf1=Df1);
Set(TFg1=g1);
Set(TFDg1=Dg1);
Set(TFf2=f2);
Set(TFDf2=Df2);
Set(TFg2=g2);
Set(TFDg2=Dg2);
Set(TFf3=f3);
Set(TFDf3=Df3);
Set(TFg3=g3);
Set(TFDg3=Dg3);
if whichfunc=4 then
g||(x0-1):=randcr();
while g||(x0-1)=x0 do
g||(x0-1):=randcr();
end do;
Set(TFg||(x0-1)=g||(x0-1));
if x0 = 2 then
if g||(x0-1)=2 then
Df1:=Df1;
elif g||(x0-1)=3 then
Df1:=Df2;
elif g||(x0-1)=4 then
Df1:=Df3;
end if;
end if;
if x0 = 3 then
if g||(x0-1)=2 then
Df2:=Df1;
elif g||(x0-1)=3 then
Df2:=Df2;
elif g||(x0-1)=4 then
Df2:=Df3;
end if;
end if;
if x0 = 4 then
if g||(x0-1)=2 then
Df3:=Df1;
elif g||(x0-1)=3 then
Df3:=Df2;
elif g||(x0-1)=4 then
Df3:=Df3;
end if;
end if;
end if;
if whichfunc=5 then
f||(x0-1):=randcr();
while f||(x0-1)=x0 do
f||(x0-1):=randcr();
end do;
Set(TFf||(x0-1)=f||(x0-1));
if x0 = 2 then
if f||(x0-1)=2 then
Dg1:=Dg1;
elif f||(x0-1)=3 then
Dg1:=Dg2;
elif f||(x0-1)=4 then
Dg1:=Dg3;
end if;
end if;
if x0 = 3 then
if f||(x0-1)=2 then
Dg2:=Dg1;
elif f||(x0-1)=3 then
Dg2:=Dg2;
elif f||(x0-1)=4 then
Dg2:=Dg3;
end if;
end if;
if x0 = 4 then
if f||(x0-1)=2 then
Dg3:=Dg1;
elif f||(x0-1)=3 then
Dg3:=Dg2;
elif f||(x0-1)=4 then
Dg3:=Dg3;
end if;
end if;
end if;
Set(TFh=('h'('x') = func));
Set(Question(caption)=sprintf("Find Dh(%a), the derivitive of h(x) at x = %a, using the given data.", x0, x0));
Set(LDhval(caption)=sprintf("Dh(%a) =", x0));
Dh:=eval(Dhs[whichfunc],[p=power]);
if x0=2 then
Dhval:=eval(Dhvals[whichfunc],[f0=f1,Df0=Df1,g0=g1,Dg0=Dg1, p=power]);
elif x0=3 then
Dhval:=eval(Dhvals[whichfunc],[f0=f2,Df0=Df2,g0=g2,Dg0=Dg2, p=power]);
elif x0=4 then
Dhval:=eval(Dhvals[whichfunc],[f0=f3,Df0=Df3,g0=g3,Dg0=Dg3, p=power]);
end if;
Set(TFDh="");
Set(replyDh="", replyDh(background)=white);
Set(TFDhval="");
Set(replyDhval="", replyDhval(background)=white);
Set(reply="", reply(background)=white);
end proc:CheckDh:=proc()
global func,Dh;
local user_Dh;
user_Dh:=Get(TFDh::anything, corrections=true, update=true);
if simplify(user_Dh-Dh)<>0 then
Set(replyDh="Incorrect", replyDh(background)=red, replyDh(foreground)=white);
Set(reply="That is incorrect.",
reply(background)=red, reply(foreground)=white);
else
Set(replyDh="Correct", replyDh(background)=green, replyDh(foreground)=black);
Set(reply="Way to go home skillet BISCUIT! You're right!",
reply(background)=green, reply(foreground)=black);
end if;
end proc:ShowDh:=proc()
global Dh,Dhval;
Set(TFDh=Dh);
Set(replyDh="Shown",
replyDh(background)=Orange, replyDh(foreground)=black);
Set(reply="Now try it on your own.", reply(background)=Orange, reply(foreground)=black);
end proc:CheckDhval:=proc()
global func,Dhval;
local user_Dhval;
user_Dhval:=Get(TFDhval::anything, corrections=true, update=true);
if simplify(user_Dhval-Dhval)<>0 then
Set(replyDhval="Incorrect", replyDhval(background)=red, replyDhval(foreground)=white);
Set(reply="Sorry, that is wrong.",
reply(background)=red, reply(foreground)=white);
else
Set(replyDhval="Correct", replyDhval(background)=green, replyDhval(foreground)=black);
Set(reply="That is correct. I am so proud of you!",
reply(background)=green, reply(foreground)=black);
end if;
end proc:ShowDhval:=proc()
global Dh,Dhval;
Set(TFDhval=Dhval);
Set(replyDhval="Shown",
replyDhval(background)=Orange, replyDhval(foreground)=black);
Set(reply="Now try it on your own.", reply(background)=Orange, reply(foreground)=black);
end proc:DerData:=Maplet(onstartup = RunWindow(MAIN),
Window[MAIN](title="Computing Derivatives Using Data",
[ valign=none, spacing=0, inset=0,
[ halign=none,
Button("New Problem", Evaluate(function="NewProb"), background=magenta),
HorizontalGlue(),
[ valign=none, inset=0, border=true,
caption="Goal: Consider the function:",
[ halign=none,
HorizontalGlue(),
TextField[TFh](width=10, editable=false, background=white),
HorizontalGlue()
],
[ halign=none,
Label[Question]("Find Dh(x0), the derivitive of h(x) at x = x0, using the given data."),
HorizontalGlue()
]
],
HorizontalGlue(),
Button("Quit", Shutdown(), background=pink)
],
[ halign=none,
HorizontalGlue(),
"f(2) = ", TextField[TFf1](width=5, editable=false, background=white),
HorizontalGlue(),
"Df(2) = ", TextField[TFDf1](width=5, editable=false, background=white),
HorizontalGlue(),
"g(2) = ", TextField[TFg1](width=5, editable=false, background=white),
HorizontalGlue(),
"Dg(2) = ", TextField[TFDg1](width=5, editable=false, background=white),
HorizontalGlue()
],
[ halign=none,
HorizontalGlue(),
"f(3) = ", TextField[TFf2](width=5, editable=false, background=white),
HorizontalGlue(),
"Df(3) = ", TextField[TFDf2](width=5, editable=false, background=white),
HorizontalGlue(),
"g(3) = ", TextField[TFg2](width=5, editable=false, background=white),
HorizontalGlue(),
"Dg(3) = ", TextField[TFDg2](width=5, editable=false, background=white),
HorizontalGlue()
],
[ halign=none,
HorizontalGlue(),
"f(4) = ", TextField[TFf3](width=5, editable=false, background=white),
HorizontalGlue(),
"Df(4) = ", TextField[TFDf3](width=5, editable=false, background=white),
HorizontalGlue(),
"g(4) = ", TextField[TFg3](width=5, editable=false, background=white),
HorizontalGlue(),
"Dg(4) = ", TextField[TFDg3](width=5, editable=false, background=white),
HorizontalGlue()
],
[ halign=none, border=true,
caption="Step 1: Find the general derivative:",
"Dh(x) =",
TextBox[TFDh](width=30, height=1, background=turquoise),
HorizontalGlue(),
Button[BCheckDh]("Check", Evaluate(function="CheckDh"), background=green),
TextField[replyDh](width=6, editable=false, background=white),
Button[BShowDh]("Show", Evaluate(function="ShowDh"), background="Orange")
],
[ halign=none, border=true,
caption="Step 2: Evaluate the derivative at the point:",
Label[LDhval]("Dh( ) ="),
TextBox[TFDhval](width=30, height=1, background=turquoise),
HorizontalGlue(),
Button("Check", Evaluate(function="CheckDhval"), background=green),
TextField[replyDhval](width=6, editable=false, background=white),
Button("Show", Evaluate(function="ShowDhval"), background="Orange")
],
[
TextBox[reply](width=60, height=2)
],
[ halign=none, inset=0, spacing=0,
"Instructor: Philip Yasskin",
HorizontalGlue(),
"Programmers: Andrew Mora and Cory Lusk",
HorizontalGlue(),
"Copyright: Yasskin 2013"
]
]
)
): Maplets[Display]( DerData );