0
$\begingroup$

Consider a piecewise function such as

f = Piecewise[{{x, 0 <= x <= x1}, {Sin[\[Pi] x], x1 < x <= x2}}]

I want to plot f using Manipulate and add a point on the plot at the location x = x1.

Manipulate[
 Plot[f /. {x1 -> xx1, x2 -> xx2}, {x, 0, 50}, PlotRange -> All,
  Epilog -> {PointSize[Large], Red, Point[{xx1, f /. x -> xx1 }]}
  ], {xx1, 1, 10}, {xx2, xx1 + 1, 50}]

This code fails because the Point command does not know what f is. In this case it is trivial to just write in Point[{xx1, xx1}], in my real problem f is defined after a number of other calculations and dependent on a number of variables both set separately and as part of the manipulate, and so cannot be defined explicitly here.

I tried wrapping the y coordinate in an Evaluate unsuccessfully. The underlying Plot command does not work even without the Manipulate

$\endgroup$

1 Answer 1

1
$\begingroup$

Your problem is the definition of "Point". You need to replace not only x, but also x1 and x2:

f = Piecewise[{{x, 0 <= x <= x1}, {Sin[\[Pi] x], x1 < x <= x2}}];
Manipulate[
 Plot[f /. {x1 -> xx1, x2 -> xx2}, {x, 0, 50}, PlotRange -> All, 
  Epilog -> {PointSize[Large], Red, 
    Point[{xx1, f /. { x -> xx1, x1 -> xx1, x2 -> xx2}}]}], {xx1, 1, 
  10}, {xx2, xx1 + 1, 50}]

enter image description here

$\endgroup$

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.