0
$\begingroup$

I am required to write a function that takes a function as a parameter.

moveTwoPoints[function_, {sliderMin_, sliderMax_}] := 
 DynamicModule[
  {sliderValue = sliderMin,
   plot},
  f[x_] := function;
  Column[
   {
(*Display slider *)
Slider[Dynamic[sliderValue], {sliderMin, sliderMax}],
(*Display updating slider value *)
Dynamic[sliderValue],
(*Display the graph with points moving*)
Dynamic[
 Show[
  Plot[f[x], {x, sliderMin, sliderMax}], 
  Graphics[
   {Red, PointSize[Large], Point[{sliderValue, f[sliderValue]}], 
    Green, PointSize[Large], Point[{0, f[sliderValue]}]
        }
       ]
      ]
     ]
    }
   ]
  ]

The problem I am running into is the fact that when I try f[sliderValue] is won't return a point just the value of function. Is there a way I can manipulate this function?

$\endgroup$

1 Answer 1

1
$\begingroup$

Do you mean this?

f[func_, {Min_, Max_}] :=Manipulate[
  Column[{a,Plot[func[x], {x, Min, Max}, 
      Epilog -> {Red, PointSize[Large], Point[{a, func[a]}], 
      Green, PointSize[Large], Point[{0, func[a]}]}]}, 
  Alignment -> Center], {a, Min, Max}]
f[Sin[#] &, {0, 2 Pi}]
$\endgroup$
8
  • $\begingroup$ Yeah that is what I meant. The requirements for the problem are to create a slider and show a point along the y axis that mirrors a red point that follows the function. Your solution is great, I don't know if it would earn me full credit tho. How did you function change the values of the y coordinate using func[a]? I feel like my solution should have worked with that. @Chenminqi $\endgroup$ Mar 13, 2014 at 16:00
  • $\begingroup$ Is there a way to do that without using manipulate? @chenminqi $\endgroup$ Mar 13, 2014 at 16:02
  • $\begingroup$ Manipulate is a good function for dynamic demo. $\endgroup$
    – Apple
    Mar 13, 2014 at 16:02
  • $\begingroup$ Okay, manipulate makes sense but after re-reading the problem it states that I must use Dynamic Module. I will try to work with that. @chenminqi $\endgroup$ Mar 13, 2014 at 16:03
  • $\begingroup$ Of course without using manipulate.But I need go to sleep ,so...tomorrow. $\endgroup$
    – Apple
    Mar 13, 2014 at 16:03

Your Answer

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

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