Plotting Rational Functions

Helmut Knaust, Department of Mathematical Sciences, UTEP, El Paso TX 79968
hknaust@utep.edu
10/23/2011

PlotRational[fct_,var_,yrange_,xrange_:{-1,1}]:=Module[{f,solN,solD,sol,m,M,solT,l1,l2,x},f=Function[var,fct];solN=Select[NSolve[Numerator[Together[f[x]]]==0,x][[All,1,2]],(Chop[Im[#]]==0&)];solD=Select[NSolve[Denominator[Together[f[x]]]==0,x][[All,1,2]],(Chop[Im[#]]==0&)];sol=Union[solN,solD];
Print["The function has ",Length[solN],If[Length[solN]==1," zero"," zeroes"]," and ",Length[solD], If[Length[solD]==1, " pole.", " poles."]];
m=Min[Min[sol]-1,xrange[[1]]];M=Max[Max[sol]+1,xrange[[2]]];solT=Append[Prepend[sol,m],M];l1=Map[Function[t,{t,0}],solT];l2=Table[{AbsoluteThickness[5],If[f[(l1[[i,1]]+l1[[i+1,1]])/2]>0,Red,Blue],Line[{l1[[i]],l1[[i+1]]}]},{i,1,Length[l1]-1}];
Plot[f[x],{x,m,M},Prolog->l2,BaseStyle->{14,Bold},PlotRange->{yrange[[1]],yrange[[2]]},PerformanceGoal->"Quality",PlotStyle->{Black,Thick},Exclusions->{Denominator[Together[f[x]]]==0},ExclusionsStyle->{{Gray,Dashed}},ImageSize->640,AxesLabel->{var,None}]]

g[x_]=Product[x^RandomInteger[{1,2}]-RandomReal[{-3,3}],{2}]/Product[x^RandomInteger[{1,2}]-RandomReal[{-3,3}],{2}]

PlotRational[g[x],x,{-5,5}]