Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get this piece of code to work:

 DynamicModule[{f = x, g = -x},
  xmax = 10;
   InputField[Dynamic[f], ContinuousAction -> True], 
   Dynamic[ Show[Plot[{f, g}, {x, -xmax, xmax}, AspectRatio -> 1,
                              PlotRange -> {{-xmax, xmax}, {-xmax, xmax}}]

This works and updates dynamically the graphs of f and g. Now, I would like to add the intersections, dynamically, if any (so with a IF testing), and I tried different things, without success:

solution = Dynamic[Solve[f == g, {x,y}]

gives me indeed the dynamically updated solution. But I can't use this information to plot points, nor even get Length[solution] update dynamically...

Any help with this?

share|improve this question
This is the static version of your question. Take a look there. solution definition looks strange, use solution:=... and use Dynamic at the end in the plotting procedure. p.s. isn't Show redundant? – Kuba Sep 4 '13 at 18:04
Thank you for this static version, it's very helpful! – Gabriel Sep 4 '13 at 20:05
up vote 0 down vote accepted

This solution is only for simple cases where the intersection is a point. And not adapted for cases with periodical functions when the output of the Solve contains a conditional expression (sometimes).

Panel[DynamicModule[{f = x, g = -x, xmax = 20},

    InputField[Dynamic[f], ContinuousAction -> True],
    Dynamic @ Plot[{f, g}, {x, -xmax, xmax}, AspectRatio -> 1, PlotRange -> xmax, 
                                             Epilog -> {PointSize@.02, Point[pt]}]
  , Initialization :> (pt := ({x, f} /. NSolve[f == g, x, Reals]) /. {x, f} -> {})

enter image description here

share|improve this answer
I like this answer a lot, thank you very much. One question though, what do you mean by "/. {x, f} -> {}" ? Would this be equivalent to {x, f} /. NSolve[f == g, x, Reals])[[1]] ? Thank you! – Gabriel Sep 4 '13 at 20:04
@Gabriel when there is no solution there is no replacement rules so in this case {x,f} is switching to {} because it will not cause any error in Epilog. I'm glad it is helpful. – Kuba Sep 4 '13 at 20:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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