# Dynamically show intersection of two functions inside Manipulate

I want to add some kind of arrows (or some kind of function) on the x-axis (as depicted). if sin(x) is higher than bx, then arrow should point to the right, in the interval between its two intersection. A left arrow, if bx is higher than sin(x). Arrows should vary with change of paramters.

func[x_, b_] := b*x;
Manipulate[
Plot[{a*Sin[x], func[x, b]}, {x, 0, 2 Pi}],
{a, 1, 10}, {b, 0.5, 10}]

• Can you be more clear as to what you mean when Sin[x] greater than bx? For a=1 and b=0.5, at $x=1$ Sin[x]>bx at $x=4$ Sin[x]<bx. At the intersection Sin[x]==bx. At which point of x are you evaluating at? – I should change my Username Jul 7 '17 at 9:12
• if i lower b or raise a, then the interval where sin[x] is greater than bx gets larger. Then the left arrow pointing to the right should get longer aswell (until sin[x] is lower than bx). There is no specific point of x where i am evaluating at. The arrow should vary with the parameters a and b. – Manuel Fülling Jul 7 '17 at 9:44

Update 2: Dealing with question in the comments:

My original code is much longer and with multiple functions. I don't use the command Show in it. So if i add /. Line->Arrow all functions get an arrow. Is there a possibility to use /. Line ->Arrow separately on functions?

You can put the /. Line->Arrow piece after Plot[...], and specify Arrowheads[0] for the lines that you want to be rendered as Lines as part of the PlotStyle setting. For example:

Manipulate[Plot[{a*Sin[x], func[x, b],
ConditionalExpression[0, func[x, b] <= a Sin[x]],
ConditionalExpression[0, func[x, b] >= a Sin[x]]}, {x, 0, 2 Pi},
PlotStyle -> {Directive[Thick, Arrowheads[0], ColorData[54, "ColorList"][[1]]],
Directive[Blue, Thick, Arrowheads[{-.05, 0}]]}] /. Line -> Arrow,
{a, 1, 10}, {b, 0.5, 10}]


Note: In version 9, you don't have to use /. Line->Arrow trick. You can use functions to specify the PlotStyle and use ({Directive[Blue, Thick, Arrowheads[{-.05, 0}], Arrow@@#}&) in place ofDirective[Blue, Thick, Arrowheads[{-.05, 0}]. But this method no longer works in newer versions.

Update: If you have Version 10 or newer versions, you can also use NumberLinePlot as Epilog:

Manipulate[Plot[{a*Sin[x], func[x, b]}, {x, 0, 2 Pi},
Epilog -> First[NumberLinePlot[{a Sin[x] <= func[x, b],
a Sin[x] > func[x, b]}, {x, 0, 2 Pi},
PlotStyle -> {Directive[Red, Thick, Arrowheads[{-.05, 0}]],
Spacings -> 0] /. Line -> Arrow]],
{a, 1, 10}, {b, 0.5, 10}]


Manipulate[Show[Plot[{a*Sin[x], func[x, b]}, {x, 0, 2 Pi}],
Plot[{ConditionalExpression[0, func[x, b] <= a Sin[x]],
ConditionalExpression[0, func[x, b] >= a Sin[x]]}, {x, 0, 2 Pi},
PlotStyle -> {Directive[Red, Thick, Arrowheads[{0, .05}]],
Directive[Blue, Thick, Arrowheads[{-.05, 0}]]}] /. Line -> Arrow],
{a, 1, 10}, {b, 0.5, 10}]


You can also use a combination of Epilog and ParametricPlot with Mesh options:

Manipulate[Plot[{a*Sin[x], func[x, b]}, {x, 0, 2 Pi},
Epilog -> First[ParametricPlot[{x, 0}, {x, 0, 2 Pi},
MeshFunctions -> {a Sin[#] - func[#, b] &}, Mesh -> {{0}},
Directive[Blue, Thick, Arrowheads[{0, .05}]]}] /. Line -> Arrow]],
{a, 1, 10}, {b, 0.5, 10}]

• Can you explain a bit of how the ConditionalExpression[] is working? It returns 0 when func[x,b]<=a Sin[x] and 0 when func[x,b]>=a Sin[x]. Since the 2nd argument to ConditionalExpression[] is the cond., I can't seem to be able to wrap my head around how you are getting the coordinates. – I should change my Username Jul 7 '17 at 10:49
• @IshouldchangemyUsername, ConditionalExpression[0,condition] gives 0 if the condition is satisfied and Nothing otherwise. The Nothing portion is not plotted so we get, in the current case, a line from {0,0} to {0,x*} where x* is the maximum point where condition is satisfied. BTW, ConditionalExpression[0,condition] gives the same result as Piecewise[{{0,condition}}, Nothing] (which may be more intuitive). Hope this helps. – kglr Jul 7 '17 at 10:57
• ConditionalExpression[0, func[x, b] <= a Sin[x] is a condition where i only compare two functions with each other. What if i need to compare func[x,b] with two functions (for example with asin(x) and acos(x)? ConditionalExpression[0, func[x, b] <= {a Sin[x], a Cos[x]} does not work – Manuel Fülling Jul 7 '17 at 11:37
• @Manuel, condition in ConditionalExpression[0,condition] should be a Boolean function that evaluates to True or False. So, you can use And @@ Thread[func[x, b] <= {a Sin[x], a Cos[x]}] or Or @@ Thread[func[x, b] <= {a Sin[x], a Cos[x]}] as condition in ConditionalExpression[0,condition] – kglr Jul 7 '17 at 11:59
• if i add /. Line -> Arrow after my closing Plot Bracket ] , all my function get an arrow. Where do i add /. Line -> Arrow and do i need the command Show[] for it? – Manuel Fülling Jul 7 '17 at 14:17

Another solution using NSolve to find the intersection point and Graphics to draw the arrows:

func[x_, b_] := b*x;
Manipulate[
With[{xIntersection =
NSolve[{a*Sin[x] == func[x, b], x > 0}, x, Reals][[1, 1, 2]]},
Show[
Plot[{a*Sin[x], func[x, b]},
{x, 0, 2 Pi},
PlotRange -> {{0, 2 Pi}, {-6, 6}}
],
Graphics[{
Point@{xIntersection, func[xIntersection, b]},
`