11
$\begingroup$

The following code produces the graph of $f(x)=\frac{1}{x-1}$ correctly.

 Plot[1/(x - 1), {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}}, 
 PlotStyle -> {Thick}, BaseStyle -> {FontSize -> 14}, 
 Exclusions -> {x - 1 == 0}, 
 ExclusionsStyle -> Directive[Red, Dashed]]

enter image description here

However, when I try to graph $f(x)=\frac{1}{(x-1)^2}$ with essentially the same code, I lose the vertical asymptote.

Plot[1/(x - 1)^2, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}}, 
PlotStyle -> {Thick}, BaseStyle -> {FontSize -> 14}, 
Exclusions -> {(x - 1)^2 == 0}, 
ExclusionsStyle -> Directive[Red, Dashed]]

enter image description here

I have tried all sorts of permutations of writing the exclusions, but nothing seems to work! From what I can tell, whenever I try to graph a rational function that has a factor in the denominator with an even power, the vertical asymptote fails to draw.

Any help would be greatly appreciated!

$\endgroup$
  • 2
    $\begingroup$ It looks like Exclusion "connects" your singularities... So in the first case it connects two points $(1,-\infty)$ and $(1,+\infty)$, in the second case there is just one point $(1,+\infty)$, so no line is visible... $\endgroup$ – BlacKow Jun 14 '16 at 19:57
10
$\begingroup$

There is a good demonstration of how ExclusionsStyle works here. Check
Plot[Floor[x], {x, 0, 5}, ExclusionsStyle -> {Red, Blue}]

If you use the second form of ExclusionsStyle, you will see the points (boundary of exclusion region) that are being connected by your asymptote:

Plot[1/(x - 1)^3, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 25}}, 
 PlotStyle -> {Thick}, BaseStyle -> {FontSize -> 14}, 
 Exclusions -> {x - 1 == 0}, 
 ExclusionsStyle -> {Directive[Red, Dashed], 
   Directive[PointSize -> 0.05, Green]}]

plot with asymptote

When you have even powers, those two points degenerate into one point and your line becomes invisible.

no asymptote

A possible workaround would be drawing the line separately with Epilog or ListPlot

Plot[1/(x - 1)^2, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}}, 
 PlotStyle -> {Thick}, BaseStyle -> {FontSize -> 14}, 
 Epilog -> {Directive[Red, Dashed], Line[{{1, -1000}, {1, 1000}}]}]

Or

Show[Plot[1/(x - 1)^2, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}}, 
  PlotStyle -> {Thick}, BaseStyle -> {FontSize -> 14}], 
 ListPlot[{{1, -1000}, {1, 1000}}, Joined -> True, 
  PlotStyle -> Directive[Red, Dashed]]] 

Or per Kuba's comment:

Plot[1/(x - 1)^2, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}}, 
 PlotStyle -> {Thick}, BaseStyle -> {FontSize -> 14}, 
 GridLines -> {{1}, None}, GridLinesStyle -> Directive[Red, Dashed]]

alternative

In the most general case, when your asymptote isn't horizontal or vertical, you will need to plot it as a separate plot. So Exclusions is not the best option:

Plot[{4/(x^2 + 1) + x, x}, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}}, 
     PlotStyle -> {Thick, Directive[Red, Dashed]},  BaseStyle -> {FontSize -> 14}]

oblique asymptote

| improve this answer | |
$\endgroup$
  • $\begingroup$ Fantastic! I had no idea that this was how ExclusionStyle worked! I learned two things from this massive headache! $\endgroup$ – Jon Jun 14 '16 at 20:23
  • 2
    $\begingroup$ GridLines are also an option. $\endgroup$ – Kuba Jun 14 '16 at 20:49
  • $\begingroup$ @Kuba... I like GridLines idea, edited the answer. $\endgroup$ – BlacKow Jun 14 '16 at 20:55
  • $\begingroup$ @Kuba on the other hand the GridLines won't work if the asymptote isn't vertical or horizontal... $\endgroup$ – BlacKow Jun 14 '16 at 21:35
  • $\begingroup$ @BlacKow, yes, for that you can either plot the oblique asymptote as a separate function, or use InfiniteLine[] in Epilog. $\endgroup$ – J. M.'s technical difficulties Jun 14 '16 at 23:40
4
$\begingroup$

Since Mathematica 10, I prefer InfiniteLine for applications like these. It is more flexible than GridLines because it can run in any direction, and it can coexist with, say, GridLines -> Automatic.

Example:

Plot[1/(x - 1)^2, {x, -5, 5}, PlotRange -> {{-5, 5}, {-5, 5}},
 Epilog -> {Red, Dashed, 
   InfiniteLine[{1, 0} (* through this point *), {0, 1} (* in a vertical direction *)]},
 PlotTheme -> "Detailed", Axes -> True
 ]

enter image description here

| improve this answer | |
$\endgroup$
4
$\begingroup$

Just for completeness: before InfiniteLine[] came along, I often used Scaled[] in conjunction with Line[] to depict horizontal and vertical asymptotes.

Plot[1/(x - 1)^2, {x, -5, 5}, Axes -> True,
     Epilog -> {Directive[Red, Dashed],
                Line[{Scaled[{0, 1}, {1, 0}], Scaled[{0, -1}, {1, 0}]}]}, 
     PlotRange -> {{-5, 5}, {-9, 9}}, PlotTheme -> "Detailed"]

function with an even-order pole

Plot[Sec[x]^2, {x, -3 π, 3 π}, Axes -> True, 
     Epilog -> {Directive[Red, Dashed], 
                Table[Line[{Scaled[{0, 1}, {(k - 1/2) π, 0}],
                            Scaled[{0, -1}, {(k - 1/2) π, 0}]}], {k, -2, 3}]}, 
     PlotRange -> {-10, 10}, PlotTheme -> "Detailed"]

multiple poles

Plot[Erf[x], {x, -5, 5}, Axes -> True, 
     Epilog -> {Directive[Red, Dashed], 
                Line[{Scaled[{1, 0}, {0, 1}], Scaled[{-1, 0}, {0, 1}]}], 
                Line[{Scaled[{1, 0}, {0, -1}], Scaled[{-1, 0}, {0, -1}]}]}, 
     PlotRange -> {-2, 2}, PlotTheme -> "Detailed"]

error function

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ This technique is still very relevant. I just had to shade the background inbetween $(x_1, x_2)$ and this seems the best way to do it, with a Rectangle in Prolog instead of Line. $\endgroup$ – Szabolcs Jun 28 '16 at 8:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.