Function Grapher

This tool allows you to enter up to 6 functions to graph.  You must specify the minimum and maximum x values to plot; if you like, you may also enter the minimum and maximum y values.  When you're done entering your functions, click the "Graph!" button.  When the graph appears, click the play button to see all of the graphs.

Note: If you do not see a function that you tried to plot, it's most likely because you have entered the function in a way that Mathematica does not understand.  If you need help figuring out how to enter functions in Mathematica, please see MouseOver.

Off[General::"spell1"];
DynamicModule[{graphs,f,x0,x1,y0,y1,plots,range,style,button},
Module[{errors,warnings},

(* defaults *)
graphs=ListAnimate[{Plot[1,{x,0,1},PlotRange->{0,0.1},Axes->False,ImageSize->480]},AnimatorElements->{"ProgressSlider","ResetPlayButton"},Deployed->True,DisplayAllSteps->True,AnimationRunning->False];
f={Cos[x],E^x,x^2-3x+2,Null,Null,Null};
x0=-3;x1=3;y0=Null;y1=Null;

(* the button that does all the work *)
button=Button["Graph!",
(* first error check *)
errors={};

(* check plot range *)
If[!NumericQ[x0],AppendTo[errors,"Minimum x value must be a number"]];
If[!NumericQ[x1],AppendTo[errors,"Maximum x value must be a number"]];
If[!NumericQ[y0]&&ToString[y0]≠"Null",AppendTo[errors,"Invalid minimum y (you may leave this blank)"]];
If[!NumericQ[y1]&&ToString[y1]≠"Null",AppendTo[errors,"Invalid maximum y (you may leave this blank)"]];

If[Length[errors]>0,

(* if there are errors, pop up a dialog box and don't
 do anything else *)
CreateDialog[Join[{"Error:"},errors,{DefaultButton[]}],Modal->True],

(* no errors, go! *)
plots={};
style={};

(* first see whether the functions seem valid; if not, just put up a warning dialog but don't fail *)
On[General::plnr];
Quiet[On[Plot::plnr]];
warnings={};
Do[If[ToString[f[[i]]]≠"Null"&&
TrueQ[Quiet[Check[Plot[f[[i]],{x,x0,x1}],"Error"]]=="Error"],
AppendTo[warnings,f[[i]]]],{i,1,6}];
Off[General::plnr];
Off[Plot::plnr];

If[Length[warnings]>0,
CreateDialog[Join[{"Warning: The following functions may be entered incorrectly."},warnings,{DefaultButton[]}],Modal->True]];

(* get the plot range by graphing everything together on one plot and using whatever range is automatically generated *)
range=AbsoluteOptions[Plot[f,{x,x0,x1},DisplayFunction->Identity],PlotRange][[1]][[2]][[2]];

(* if the user specified y range, use those *)
If[NumericQ[y0],range[[1]]=y0];
If[NumericQ[y1],range[[2]]=y1];

(* expand the range just a little so the top and bottom don't get partly cut off *)
range=1.1*(range-(range[[2]]+range[[1]])/2)+(range[[2]]+range[[1]])/2;

(* now graph *)
Do[
AppendTo[style,{Hue[(i-1)/6],Thickness[0.004]}];
AppendTo[plots,Plot[Evaluate[Take[f,i]],{x,x0,x1},PlotRange->range,DisplayFunction->Identity,PlotStyle->style,ImageSize->480]],
{i,1,6}];

(* finally, animate *)
graphs=ListAnimate[plots,DefaultDuration->6,AnimationRepetitions->1,AnimatorElements->{"ProgressSlider","ResetPlayButton"},DisplayAllSteps->True,AnimationRunning->False]]];

Panel[
Grid[{{
(* top left of main grid *)
Grid[
Table[
{Style["-",Hue[(i-1)/6],Background->RGBColor[1,1,1],FontSize->24,FontWeight->Bold],
With[{i=i},InputField[Dynamic[f[[i]]]]]},{i,1,6}]
],

(* top right of main grid *)
Grid[
{{"Minimum x",
InputField[Dynamic[x0],FieldSize->4]},
{"Maximum x",
InputField[Dynamic[x1],FieldSize->4]},
{"Minimum y",
InputField[Dynamic[y0],FieldSize->4]},
{"Maximum y",
InputField[Dynamic[y1],FieldSize->4]},
{Button["Clear Settings",f={Null,Null,Null,Null,Null,Null};x0=Null;x1=Null;y0=Null;y1=Null],
SpanFromLeft
},
{button,SpanFromLeft}}
]
},
{},
{"After you hit the \"Graph!\" button, the graphs will appear below.",SpanFromLeft,SpanFromLeft,SpanFromLeft},
{"Click the triangular play button to see all of the graphs.",SpanFromLeft,SpanFromLeft,SpanFromLeft},
(* bottom of main grid *)
{Dynamic[graphs],SpanFromLeft,SpanFromLeft,SpanFromLeft}},
Spacings->{2,1},Alignment->{Left,Baseline}
]
]
]
]