# Function Average Maplet # Copyright 2002 Waterloo Maple Inc. # # The Function Average maplet computes and plots the average of a function over an interval [a,b]. # # If f is integrable on [a, b], then the average value or mean value of f on [a,b] is Int(f(x),x=a..b)/(b-a); # # The user enters a function. The maplet displays a plot of the function and its average. It also displays the calculation of the average, including its numeric value. # # To run this maplet, click the Execute (!!!) button in the context bar. # restart; FunctionAverageMaplet := module() ############################################################ export iniMathML, addMathML, getFunctionAverage, runFunctionAverageMaplet: local helpStr, defStr: ############################################################ ############################################################ helpStr := "The Function Average maplet computes and plots the average of a function over an interval [a,b]. Enter a function and click the 'Plot' button. The maplet displays a plot of the function and its average. It also displays the calculation of the average, including its numeric value.": ############################################################ ############################################################ defStr := " Average value of a function If a function f is integrable on [a,b], then the average value of f on [a,b] is a b f x x b - a ": ############################################################ ############################################################ # pre: iniEqn :: algebraic expression # post: returns the Presentation MathML of iniEqn iniMathML := proc(iniEqn) MathML:-ExportPresentation(iniEqn); end proc: ############################################################ ############################################################ # pre: mathMLStr :: string, in form of Presentation MathML # addEqn :: algebraic expression addMathML := proc(mathMLStr, addEqn) local tree, cmc, child, children, nl, eqnSign; use XMLTools in tree := FromString(mathMLStr); cmc := ContentModelCount(tree); child := FromString(MathML:-ExportPresentation(addEqn)); children := ContentModel(child); nl := FromString(" "); eqnSign := Element("mo","="); tree := AddChild(tree,nl,cmc); tree := AddChild(tree,eqnSign,cmc+1); for child in children do cmc := ContentModelCount(tree); tree := AddChild(tree,child,cmc); end do: tree := MakeElement("mrow", [], ContentModel(tree) ): tree := Element("math", tree): ToString(tree): end use: end proc: ############################################################ ############################################################ getFunctionAverage := proc(boo1, boo2) local temp, temp2, str, view, value: use Maplets:-Tools, Student:-Calculus1, StringTools in str:=Trim(Get('TF_fun')): if str="" then Set('ML'=""): return plots[textplot]([1,1,"Input a valid function"]): end if: temp:=Trim(Get('x1')): temp2:=Trim(Get('x2')): if temp="" or temp2="" then Set('ML'=""): return plots[textplot]([1,1,"Input end points of the interval"]): end if: str:=cat(str, ", ", temp, "..", temp2): temp := iniMathML( FunctionAverage(parse(str), output=integral) ): value := FunctionAverage(parse(str)): temp := addMathML( addMathML(temp,value), evalf(value) ): Set('ML' = temp): if not boo1 then str := cat(str, ", showfunction=false") end if: if not boo2 then str := cat(str, ", showaverage=false") end if: if not(boo1 or boo2) then return plots[textplot]([1,1,"Select function or average"]): end if: view := ["DEFAULT","DEFAULT"]: temp:=Trim(Get('x1')): temp2:=Trim(Get('x2')): if temp<>"" and temp2<>"" then view[1]:=cat(temp, "..", temp2): end if: temp:=Trim(Get('y1')): temp2:=Trim(Get('y2')): if temp<>"" and temp2<>"" then view[2]:=cat(temp, "..", temp2): end if: temp:=cat(", view=[", view[1], ", ", view[2], "]"): str := cat(str, temp): FunctionAverage(parse(str), output=plot): end use: end proc: ############################################################ ############################################################ runFunctionAverageMaplet := proc() local maplet, b, s, c, dc, lc: b := 'border'=true: s := 'inset'=0, 'spacing'=0: c := 'background'="#DDFFFF": dc := 'background'="#CCFFFF": lc := 'background'="#EEFFFF": use Maplets:-Elements in ############################################################ maplet := Maplet( 'onstartup'=RunWindow('mainWin'), ############################################################ Font['F1']('family'="Default", 'bold'='true', 'italic'='true'), Font['F2']('family'="Default", 'bold'='true', 'size'=14), ############################################################ MenuBar['MB']( Menu("File", MenuItem("Plot", 'onclick'=Evaluate('P'='getFunctionAverage(CB1,CB2)')), MenuSeparator(), MenuItem("Close", 'onclick'=Shutdown()) ), # end Menu/File Menu("Help", MenuItem("Function Average Definition", 'onclick'=RunWindow('defWin')), MenuSeparator(), MenuItem("Using this Maplet", 'onclick'=RunWindow('helpWin')) ) # end menu/Help ), # end MenuBar ############################################################ Window['mainWin']('resizable'='false', 'title'="Calculus 1 - Function Average", 'menubar'='MB', BoxRow(s, c, BoxColumn(s, c, b, 'caption'="Plot Window", Plotter['P'](lc) ), # end BoxColumn BoxColumn(s, c, BoxRow(s,c,b,'caption'="Enter a function", Label("Function ", 'font'='F2', c), TextField['TF_fun'](16, lc, 'value'=exp(x), 'tooltip'="Enter a function") ), # end BoxRow BoxRow(s, c, b, 'caption'="Domain and Range", Label("Domain", 'font'='F2', c), Label(" = ", 'font'='F1', c), TextField['x1'](2, lc, 'value'=-2), Label(" .. ", 'font'='F2', c), TextField['x2'](2, lc, 'value'=2), Label(" Range", 'font'='F2', c), Label(" = ", 'font'='F1', c), TextField['y1'](2, lc), Label(" .. ", 'font'='F2', c), TextField['y2'](2, lc) ), # end BoxRow BoxRow(s, c, b, 'caption'="Function Average", MathMLViewer['ML']('height'=220, 'width'=240, lc) ), # end BoxRow BoxRow('inset'=0, 'spacing'=0, c, BoxRow(s, c, b, 'caption'="Display Options", CheckBox['CB1']('caption'="Show function", 'value'=true, c ), CheckBox['CB2']('caption'="Show average", 'value'=true, c ) ), # end BoxRow Button("Plot", dc, 'tooltip'="Plot", 'onclick'=Evaluate('P'='getFunctionAverage(CB1,CB2)') ), Button("Close", Shutdown(), dc) ) # end BoxRow ) # end Column ) # end BoxRow ), # end Window ############################################################ Window['defWin']( 'resizable'='false', 'title'="Function Average Definition", BoxColumn(b, 'inset'=0, 'spacing'=4, c, BoxRow(s, c, MathMLViewer['ML_def']('height'=200, 'width'=300, 'value'=defStr, lc) ), # end BoxRow BoxRow(s, c, Button("Close", dc, CloseWindow('defWin')) ) # end BoxRow ) # end BoxColumn ), # end defWin ############################################################ Window['helpWin']( 'resizable'='false', 'title'="Using the Function Average Maplet", BoxColumn(b, c, 'inset'=0, 'spacing'=4, BoxRow(s, c, TextBox('height'=9, 'width'=25, c, 'font'='F2','foreground'="#333399", 'editable'='false', 'value'=helpStr ) # end TextBox ), # end BoxRow BoxRow(s, c, Button("Close", dc, CloseWindow('helpWin') ) ) # end BoxRow ) # end BoxColumn ), # end helpWin ############################################################ Action['A']() ############################################################ ): # end Maplet ############################################################ Maplets:-Display(maplet): end use: # end use end proc: # end proc ############################################################ end module: # end module FunctionAverageMaplet:-runFunctionAverageMaplet();