1
$\begingroup$

I have code that works the way I need:

Manipulate[
 Plot[-3*x + 2, {x, from, to}, AspectRatio -> Automatic, 
  PlotRange -> {{-20, 20}, {-20, 20}}], {{from, -10, "frm"}, -10, 10, 
  1, Appearance -> "Labeled"}, {{to, 10, "t"}, -10, 10, 1, 
  Appearance -> "Labeled"}]

Now I need to make Manipulate to also show me the result of corresponding y values for each x values generated by current position of the sliders (so in total I need two y value fields).

$\endgroup$
  • $\begingroup$ Since you're plotting it, the expression -3*x + 2 is evaluated at many x positions and results in many y values. The way you're doing it with y = -3*x + 2 will show the most recently evaluated result. How do you want the program to decide which y value to display? Just the last one evaluated during the plotting? $\endgroup$ – MassDefect Apr 2 at 21:01
  • $\begingroup$ @MassDefect Sorry. Thank you for your comment. Just realized that I need the value of two y (corresponding to each x from sliders). I edited my question accordingly. $\endgroup$ – vasili111 Apr 2 at 21:35
1
$\begingroup$

Depending on exactly how you want it to look, there are many ways of doing this. One way would be to add text directly to the graph like this:

f[x_] := -3 x + 2
Manipulate[
  Plot[
    f[x],
    {x, from, to},
    AspectRatio -> Automatic,
    PlotRange -> {{-20, 20}, {-20, 20}},
    Epilog -> {
      Text["Left: " <> ToString[f[from]], Scaled[{0.95, 0.95}], {1, 1}],
      Text["Right: " <> ToString[f[to]], Scaled[{0.95, 0.90}], {1, 1}]
    }
  ], 
  {{from, -10, "frm"}, -10, 10, 1, Appearance -> "Labeled"}, 
  {{to, 10, "t"}, -10, 10, 1, Appearance -> "Labeled"}
]

Graph with text appended.

Separating out the function as f[x_] := -3 x + 2 gives you the option of easily changing the function being plotted and also allows you to more easily call the function with f[from] and f[to].

You can use Style to adjust how the text appears. You could also use Row, Column, or Grid to place the graph and the two y-values next to each other if you don't want the y-values to appear on the graph itself.

| improve this answer | |
$\endgroup$
  • $\begingroup$ What is the easiest way and universal way (so I can use any input function) to place text on the top of the graph (so that text is not on the graph) $\endgroup$ – vasili111 Apr 2 at 23:29
2
$\begingroup$

The Manipulate controls do not affect the expression -3*x + 2 since the control variables do not appear in this expression. The plot iterator causes that expression to be evaluated from x == from to x == to. The end points of the line specified by -3*x + 2 are {from, -3*from+2} and {to, -3*to+2}}. These end points may or may not be in the specified PlotRange.

Manipulate[
 pts = {{from, -3 from + 2}, {to, -3 to + 2}};
 Column[
  {Row[{"from ", pts[[1]], " to ", pts[[2]]}],
   Plot[-3*x + 2, {x, from, to},
    AspectRatio -> Automatic,
    PlotRange -> {{-20, 20}, {-20, 20}},
    Epilog -> {Red, AbsolutePointSize[4],
      Tooltip[Point[#], #] & /@ pts}]}],
 {{from, -10, "frm"}, -10, 10, 1, Appearance -> "Labeled"},
 {{to, 10, "t"}, -10, 10, 1, Appearance -> "Labeled"}]

enter image description here

| improve this answer | |
$\endgroup$
2
$\begingroup$
f[x_] := -3*x + 2;

Manipulate[Plot[ConditionalExpression[f[x], r[[1]] <= x <= r[[2]]], {x, -20, 20},
   AspectRatio -> Automatic,
   PlotRange -> {{-20, 20}, {-20, 20}},
   Epilog -> {PointSize[Large], Red, 
    Point /@ {{r[[1]], Min[20, f[r[[1]]]]}, {r[[2]], Max[-20, f[r[[2]]]]} }, 
    Text[f@r[[1]], Offset[{0, 10}, {r[[1]], Min[20, f[r[[1]]]]}], {0, 0}],
    Text[f@r[[2]], Offset[{0, -10}, {r[[2]], Max[-20, f[r[[2]]]]}], {0, 0}]}, 
   PlotRangeClipping -> False, ImagePadding -> 20,
   ClippingStyle -> Directive[Dashed, Red]], 
 Panel[Row[{Dynamic@Pane[{r[[1]], f@r[[1]]}, Alignment -> Right, ImageSize -> {90, 20}], 
    IntervalSlider[Dynamic[r], {-20, 20}, Method -> "Stop", 
       Appearance -> "Paired", ImageSize -> 300], 
    Dynamic@Pane[{r[[2]], f@r[[2]]}, Alignment -> Left, ImageSize -> {90, 20}]}, 
    Spacer[10]],
   Style["x range", 16], Top],
  {{r, {-15, 15}, ""}, None},
  Alignment -> Center]

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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