3
$\begingroup$

I am trying to plot the intersection of a function n and a line b/2, as well as showing the line b in the same plot. For this purpose, I have tried using MeshFunctions, but it not only displays the intersection of the function and the line, but also the point on b lying directly above the intersection.

n[n0_, b_, t_, r_] := (n0*b)/(n0 + (b - n0) E^(-r*t));
Manipulate[
 Plot[{n[n0, b, t, r], b, b/2}, {t, 0, 20}, 
  PlotStyle -> {{Pink, Thick}, {Purple, Medium}, {Blue, Dashed, Thin}},
  MeshFunctions -> {n[n0, b, #, r] - (b/2) &}, Mesh -> {{0.}}, 
  MeshStyle -> {Black, PointSize[0.02]}],
 {{n0, 10}, 1, 100, 1, Appearance -> "Labeled"}, 
 {{b, 100}, 0, 100, 5, Appearance -> "Labeled"}, 
 {{r, 0.4}, 0, 5, 0.1, Appearance -> "Labeled"}
 ]

Plots

How do I get around this problem, and why does it happen? From looking at similar questions (in particular Show intersection points between a curve and a line and Understand MeshFunctions with Intercepts) I understand this might not be the best method to do this. I am relatively new to Mathematica, so any help on how to plot intersections like this is appreciated!

$\endgroup$

3 Answers 3

3
$\begingroup$

Epilog works best in such cases. Just solve for the intersection and then use Epilog:

Manipulate[
 pt = {t /. Quiet@First@NSolve[n[n0, b, t, r] == b/2, t], b/2}; 
 Plot[{n[n0, b, t, r], b, b/2}, {t, 0, 20}, 
  PlotStyle -> {{Pink, Thick}, {Purple, Medium}, {Blue, Dashed, 
     Thin}}, Epilog -> {PointSize[Medium], Point[pt]}], {{n0, 10}, 1, 
  100, 1, Appearance -> "Labeled"}, {{b, 100}, 0, 100, 5, 
  Appearance -> "Labeled"}, {{r, 0.4}, 0, 5, 0.1, 
  Appearance -> "Labeled"}]

enter image description here

$\endgroup$
3
  • $\begingroup$ Very nice solution, thank you! So there were no particular reason as to why that extra point appeared there, is it a bug? $\endgroup$ Feb 3, 2020 at 13:48
  • $\begingroup$ I am not sure that it is a bug. It could be just the way MeshFunction works. $\endgroup$
    – Lotus
    Feb 3, 2020 at 14:29
  • 1
    $\begingroup$ @Emilia it isn't a bug; Mathematica is very dutifully marking the corresponding points on all three functions you specified where your MeshFunctions is satisfied. It just so happens that you only want two of those three points. $\endgroup$ Feb 3, 2020 at 22:10
2
$\begingroup$

An alternative is to use InfiniteLine[] instead to depict your asymptote and intersecting line. This has the advantage of simplifying the expression in MeshFunctions:

Manipulate[Plot[n[n0, b, t, r], {t, 0, 20}, Mesh -> {{b/2}}, MeshFunctions -> {#2 &}, 
                MeshStyle -> Directive[Black, PointSize[0.02]], 
                PlotStyle -> Directive[Pink, Thick], 
                Prolog -> {{Directive[Purple, Thickness[Medium]],
                            InfiniteLine[{0, b}, {1, 0}]},
                           {Directive[Blue, Dashed, Thin],
                            InfiniteLine[{0, b/2}, {1, 0}]}}],
           {{n0, 10}, 1, 100, 1, Appearance -> "Labeled"},
           {{b, 100}, 0, 100, 5, Appearance -> "Labeled"},
           {{r, 0.4}, 0, 5, 0.1, Appearance -> "Labeled"}]

Manipulate object

$\endgroup$
2
$\begingroup$

A few additional work-arounds:

  1. Post-process Plot output to remove extra points: Use

Plot[...] /. Point -> Point @* First

in the first argument of Manipulate to get:

enter image description here

  1. Instead of post-processing outside Plot use the option

 DisplayFunction -> (# /. Point -> Point@*First &)

inside Plot to get the same result.

  1. Use a function for the value of the option MeshStyle and use the replacement rule above in that function:

MeshStyle -> ({Directive[Black, PointSize[0.02]], # /. Point -> Point@*First} &)

same picture

  1. Remove b and b/2 from the function list in the first argument of Plot and show them using GridLines:

Manipulate[Plot[n[n0, b, t, r], {t, 0, 20}, PlotStyle -> Directive[Thick, Pink],
   MeshFunctions -> {#2 &}, Mesh -> {{b/2}}, 
  MeshStyle -> Directive[Black, PointSize[0.02]], 
  GridLines -> {None, {{b, 
      Directive[Opacity[1], Purple, Thick]}, {b/2, 
      Directive[Blue, Dashed, Thin]}}}], {{n0, 10}, 1, 100, 1, 
  Appearance -> "Labeled"}, {{b, 100}, 0, 100, 5, 
  Appearance -> "Labeled"}, {{r, 0.4}, 0, 5, 0.1, 
  Appearance -> "Labeled"}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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