Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

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
add comment

1 Answer

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

  Column[{
    InputField[Dynamic[f], ContinuousAction -> True],
    InputField[Dynamic[g]],
    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
add comment

Your Answer

 
discard

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.