> |
restart:
with(plots):
with(Maplets[Elements]):
#Global variables
i:=0: #counts iterations of the bisection method
c := array(1..50): #stores the successive approximations to the root
FirstIt := 1: #when you zoom in, you will no longer graph the first approximations c1,c2,.... Start with FirstIt.
#Viewing Rectangle
yMin := -5:
yMax := 4:
xLeft :=2:
xRight := 3.01:
# Evaluates the function at a given point
fEval := proc(xP)
local g;
g := x-> evalf(x^2-5):
return g(xP):
end:
#Adjusts the viewing rectangle of the function and replots it
ZoomPlot:= proc(a,b,midp)
local curve, bisector, areacurve, leftright, approx,approxText:
global c,i, FirstIt, xLeft, xRight, yMin, yMax:
#We only show the most recents c_i's - drop the earlier ones
if (i>1) then
FirstIt := i-1:
end if:
#Adjust the height of the viewing rectangle
xLeft := evalf[10](a):
xRight := evalf[10](b + (b-midp)/128):
yMin := fEval(xLeft):
yMax := fEval(xRight):
#Create and display the elements of the plot
curve := implicitplot({y=x^2-5},x=xLeft..xRight,y=yMin..yMax,color=green):
bisector := implicitplot({x=midp},x=xLeft..xRight,y=yMin..yMax,color=red):
leftright := implicitplot({x=a,x=b},x=xLeft..xRight,y=yMin..yMax,color=blue, linestyle=DASH):
approx := pointplot({seq([c[j],fEval(c[j])],j=FirstIt..i)},color=green);
approxText := textplot({seq([c[j],fEval(c[j]),c_||j],j=FirstIt..i)},align={BELOW,RIGHT});
display(curve,bisector,leftright,approx,approxText);
end:
# Plots the function with the endpoints and bisection lines
BisectionPlot:= proc(a,b,midp,InitLeft,InitRight)
local curve, bisector, areacurve, leftright,approx, approxText:
global c, i:
#Record the next approximation c_i
i:=i+1:
c[i]:=midp:
#Create and display the elements of the plot
curve := implicitplot({y=x^2-5},x=xLeft..xRight,y=yMin..yMax,color=green):
bisector := implicitplot({x=midp},x=xLeft..xRight,y=yMin..yMax,color=red):
leftright := implicitplot({x=a,x=b},x=xLeft..xRight,y=yMin..yMax,color=blue, linestyle=DASH):
approx := pointplot({seq([c[j],fEval(c[j])],j=FirstIt..i)},color=green);
approxText := textplot({seq([c[j],fEval(c[j]),c_||j],j=FirstIt..i)},align={BELOW,RIGHT});
display(curve,bisector,leftright,approx,approxText);
end:
#Triggers the redraw of the graph zoomed in to the endpoints
ZoomIn:=()->Action(
Evaluate('PL1' = 'ZoomPlot(a,b,midp)')
):
#Triggers the redraw of the graph with new bisection points, maintaining the previous viewing rectangle
BisectIteration:=()->Action(
Evaluate('a' = `if`(fEval(midp)*fEval(a)>0,evalf(midp),a)),
Evaluate('b' = `if`(fEval(midp)*fEval(b)>0,evalf(midp),b)),
Evaluate('midp' = evalf[10]((a+b)/2)),
Evaluate('PL1' = 'BisectionPlot(a,b,midp,InitLeft,InitRight)'),
Evaluate('fmidp' = 'evalf[15](fEval(midp))')
):
maplet := Maplet(
Window('title'= "Bisection Method",
BoxLayout['BL1'](
BoxColumn(
BoxRow('halign'=left,"f(x): ", MathMLViewer['eq']('value' = MathML[Export](y=x^2-5), 'height'=50, 'width'=120)),
BoxRow('halign'=left,Label['lbla']("Endpoints: from "), TextField[a]("2", 'editable'=false, 'halign'=right, 'width'=10)),
BoxRow('halign'=left, " to " , TextField[b]("3", 'editable'=false, 'halign'=right, 'width'=10)),
BoxRow('halign'=left,Label['ci']("Midpoint: c_i = "), TextField[midp]("2.5", 'editable'=false, 'halign'=right, 'width'=10)),
BoxRow('halign'=left,"f(c_i) =", TextField[fmidp]("1.25", 'editable'=false, 'halign'=right, 'width'=10)),
BoxRow('halign'=left,Plotter['PL1'](BisectionPlot(2,3,2.5,0,3) )),
BoxRow('halign'=left, Button['Bisect']("Next midpoint", 'onclick'=BisectIteration()),Button['ZoomIn']("Zoom In", 'onclick'=ZoomIn()))
)
) #box layout
) #window
):
Maplets[Display](maplet); |