0
$\begingroup$

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

Panel[
 DynamicModule[{f = x, g = -x},
  xmax = 10;
  Column[{
   InputField[Dynamic[f], ContinuousAction -> True], 
   InputField[Dynamic[g]], 
   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?

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – Kuba
    Sep 4, 2013 at 18:04
  • $\begingroup$ Thank you for this static version, it's very helpful! $\endgroup$
    – Gabriel
    Sep 4, 2013 at 20:05

1 Answer 1

0
$\begingroup$

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},

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

enter image description here

$\endgroup$
2
  • $\begingroup$ 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! $\endgroup$
    – Gabriel
    Sep 4, 2013 at 20:04
  • $\begingroup$ @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. $\endgroup$
    – Kuba
    Sep 4, 2013 at 20:10

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.