# Manipulate an equation by its variable name

Let's say I have an equation that I want to manipulate.

In:=   y=x1+ x2


When I try to manipulate it, I get:

In:=  Manipulate[y, {{x1, 1}, 0, 10}, {{x2, 1}, 0, 10}] I can get it to work by instead referring to the output of y:

In:=  y

x1+x2

  Manipulate[%3, {{x1, 1}, 0, 10}, {{x2, 1}, 0, 10}] Is there a way to refer to the equation using Manipulate without having to either insert the entire equation, or its output?

This is a simple example, but in my actual model I have a large system of equations. (So, in this case, y would be a variable in another larger equation that I'm optimizing). If I change one equation, I don't want to have to keep track of all the places I use it in the code. I've been getting around that by just using the outputs in place of the equations when using Manipulate, but that still requires going through and changing the output numbers for each variable every time I make an adjustment. Is there a simple way to do this?

Things that have not worked:

Manipulate[Evaluate[y], {{x1, 1}, 0, 10}, {{x2, 1}, 0, 10}]
Manipulate[Solve[y], {{x1, 1}, 0, 10}, {{x2, 1}, 0, 10}]

• It's more natural and provides a better symbol localization to manipulate a functions instead y[x1_, x2_] := x1 + x2; Manipulate[y[x1, x2], {{x1, 1}, 0, 10}, {{x2, 1}, 0, 10}] Dec 10 '14 at 16:43
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! Dec 10 '14 at 17:51
• I don't understand. It seems that: y = x1 + x2; Manipulate[Evaluate[y], {x1, 0, 1}, {x2, 0, 1}] works fine. Jan 9 '15 at 20:23

myEq = (y = x1 + x2);

Manipulate[Evaluate[myEq], {x1, 0, 1},  {x2, 0, 1}]

• Thanks, this is probably a better workaround than what I was doing with %output, but gets kinda messy when you have dozens of embedded equations that you have to think up and remember names for. I'll implement this for now, but ideally the solution would be something I could do to y inside Manipulate. Dec 10 '14 at 16:49
• If you accept this answer, please click on the check mark to the left of the answer. If not, please state what more you need. It is only through this way that the community knows when your question has been answered. Dec 10 '14 at 16:53
• Like I said, it's a better work around than what I have, but messy. "But ideally the solution would be something I could do to y inside Manipulate". Dec 10 '14 at 17:10
• Then I think you want belisarus' solution. Dec 10 '14 at 17:23
• Unless I'm missing something about functions in Mathematica, that would be even messier in this application since my equations are embedded within one another. If y were a function, then I would have to specify all of its arguments every time I use it as a variable. Now imagine x1 and x2 are equations too. It gets messy fast and you lose a lot of flexibility. For example, Say I add a variable to the equation in x1, then I have to go add it to everything that uses x1 as a variable and y as a variable, etc. Dec 10 '14 at 18:08

You can also use DynamicModule, e.g.:

DynamicModule[{x1 = 1, x2 = 1},
Panel[Grid[{
{"x1", Slider[Dynamic[x1], {0, 10}, Appearance -> "Labeled"]},
{"x2", Slider[Dynamic[x2], {0, 10}, Appearance -> "Labeled"]},
{SpanFromLeft, Dynamic[x1 + x2]}}], "Dynamic Module"]] 