Drawing graphs for Math Classes

by Marcus McGuff

Graphing functions with horizontal/vertical asymptotes

Manipulate[Module[{plotOpts,plotOptsType,zoomScale,viewDomain,plotStyleList,graphList,rLimit,lLimit},

(* Compute domain/range based on location of center and zoom settings *)
zoomScale[factor_]=(factor^3);
viewDomain[centerpt_,var_:0,radius_:zoomScale[zoomFactor]]:=If[NumberQ[var],{-Abs[radius]+centerpt,Abs[radius]+centerpt},
{var,
-Abs[radius]+centerpt,Abs[radius]+centerpt}];


graphList={funct1};
plotStyleList={graphColor};
If[NumberQ[rLimit=N[Limit[funct1,x->∞]]]&&showHA,AppendTo[graphList,rLimit];AppendTo[plotStyleList,Directive[Dashed,Gray]]];
If[NumberQ[lLimit=N[Limit[funct1,x->-∞]]]&& showHA,AppendTo[graphList,lLimit];AppendTo[plotStyleList,Directive[Dashed,Gray]]];

(* Plot options common to all plots *)
plotOpts ={AxesOrigin->{0,0},PlotStyle->plotStyleList};
If[showGrid,AppendTo[plotOpts,{GridLines->Automatic,GridLinesStyle->Directive[Dotted,LightGray]}]];
plotOptsType=Options[Plot];

Plot[graphList,Evaluate[viewDomain[viewPt[[1]],x]],PlotRange->Evaluate[viewDomain[viewPt[[2]]]],Evaluate[FilterRules[plotOpts,plotOptsType]],
Exclusions->{(1/funct1)==0},ExclusionsStyle->If[showVA,Directive[Dashed,Gray],None]]

],
{{funct1,((3 x (-4+(x^2)) ArcTan[x])/(9 x-(x^3))),"f[x]="}},{{graphColor,Red,"Color"},Red},Style["Zoom"],
{{zoomFactor,1.8,""},6,0.1,ControlType->VerticalSlider},
{{showVA,True,"Display vertical asymptotes?"},{True,False}},
{{showHA,True,"Display horizontal asymptotes?"},{True,False}},
{{showGrid,False,"Display grid?"},{True,False}},
{{viewPt,{0,0},"Pan viewpoint"},{-20,-20},{20,20}},
ControlPlacement->{Top,Top,Left,Left,Bottom,Bottom,Bottom,Bottom}
]

Graphing piecewise functions

In the widget below, you can simply change the given piecewise function to whatever one you want.  You can delete or add additional lines as needed; Mathematica will then auto-detect where to put the open and closed points on your graph.  You can adjust your domain by panning the viewpoint and zooming in/out and choose a color for the graph and whether to show a grid.  If you want some different graphing options, you can either go in and modify the code yourself (double-click on the cell border just outside the box) or ask me about it.  (Warning:  The code has some tricks in it that make it kind of hard to read if you don't know a reasonable amount about Mathematica.)  Once you have the graph you want displayed, you can click on the graph itself and copy-and-paste it into whatever program you want (like Word, for example).  Enjoy.

Manipulate[Module[{plotOpts,plotOptsType,zoomScale,viewDomain,partitionPts,checkLimit,pieces,functionValues,rightLimits,leftLimits,openPts,stuff},

(* Compute domain/range based on location of center and zoom settings *)
zoomScale[factor_]=(factor^3);
viewDomain[centerpt_,var_:0,radius_:zoomScale[zoomFactor]]:=If[NumberQ[var],{-Abs[radius]+centerpt,Abs[radius]+centerpt},
{var,
-Abs[radius]+centerpt,Abs[radius]+centerpt}];

(* Plot options common to all plots *)
plotOpts ={AxesOrigin->{0,0},PlotStyle->graphColor};
If[showGrid,AppendTo[plotOpts,{GridLines->Automatic,GridLinesStyle->Directive[Dotted,Gray]}]];
plotOptsType=Options[Plot];

(*Assign the InputBox function to pieces[]*)
pieces[xTemp_]=pw/.x->xTemp;

(*Scan through the partitions to find the partition points *)
partitionPts=Select[Union[pieces[x][[1,All,2,1]],pieces[x][[1,All,2,2]]],NumberQ];

checkLimit[pt_,direct_]:=Limit[pieces[x],x->pt,Direction->direct];
SetAttributes[pieces,Listable];
SetAttributes[checkLimit,Listable];

functionValues=Select[Transpose[{partitionPts,pieces[partitionPts]}],!MemberQ[#,Indeterminate]&];

openPts=Select[Union[
rightLimits=Transpose[{partitionPts,checkLimit[partitionPts,-1]}],leftLimits=Transpose[{partitionPts,checkLimit[partitionPts,1]}]],!MemberQ[functionValues,#]&];

Show[Plot[pieces[x],Evaluate[viewDomain[viewPt[[1]],x]],PlotRange->Evaluate[viewDomain[viewPt[[2]]]],Evaluate[FilterRules[plotOpts,plotOptsType]]],ListPlot[openPts,PlotMarkers->{"○",Small},Evaluate[FilterRules[plotOpts,plotOptsType]]],ListPlot[functionValues,PlotMarkers->{"●",Small},Evaluate[FilterRules[plotOpts,plotOptsType]]]]

],
{{pw,(Piecewise[{{x+5, x≤-1}, {(x^2), -1<x<2}, {8-x, 2≤x<4}, {2, x==4}, {3Sin[x], x>4}, {Indeterminate, True}}]),"f[x]="}},{{graphColor,Red,"Color"},Red},Style["Zoom"],
{{zoomFactor,1.8,""},6,0.1,ControlType->VerticalSlider},
{{showGrid,False,"Display grid?"},{True,False}},
{{viewPt,{0,0},"Pan viewpoint"},{-20,-20},{20,20}},
ControlPlacement->{Top,Top,Left,Left,Bottom,Bottom}
]

P.S.:  The last line in the piecewise definition (Indeterminate True) should not be deleted.  It is the "Otherwise" case; if you leave any holes in your domain, the value here (Indeterminate) is assigned to those points.  If you delete the line, the default Otherwise case assigns it to 0, which is probably not what you want.  On the other hand, if you want to define a piecewise function like:

(Piecewise[{{(x^2), 0≤x≤1}, {0, Otherwise}}])

the correct way to do this would be:

(Piecewise[{{(x^2), 0≤x≤1}, {0, True}}])

(This line should be the last line in your definition always.)