# How to make Manipulate[] to show me the result of expression evaluation live (question updated!)?

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).

• 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? Apr 2 '20 at 21:01
• @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. Apr 2 '20 at 21:35

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"}
] 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.

• 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) Apr 2 '20 at 23:29

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[], " to ", pts[]}],
Plot[-3*x + 2, {x, from, to},
AspectRatio -> Automatic,
PlotRange -> {{-20, 20}, {-20, 20}},
Epilog -> {Red, AbsolutePointSize,
Tooltip[Point[#], #] & /@ pts}]}],
{{from, -10, "frm"}, -10, 10, 1, Appearance -> "Labeled"},
{{to, 10, "t"}, -10, 10, 1, Appearance -> "Labeled"}] f[x_] := -3*x + 2;

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