0
$\begingroup$

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

In[1]:=   y=x1+ x2

When I try to manipulate it, I get:

In[2]:=  Manipulate[y, {{x1, 1}, 0, 10}, {{x2, 1}, 0, 10}]

1

I can get it to work by instead referring to the output of y:

In[3]:=  y
x1+x2
  Manipulate[%3, {{x1, 1}, 0, 10}, {{x2, 1}, 0, 10}]

2

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}]
$\endgroup$
  • 2
    $\begingroup$ 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}] $\endgroup$ – Dr. belisarius Dec 10 '14 at 16:43
  • $\begingroup$ 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! $\endgroup$ – Dr. belisarius Dec 10 '14 at 17:51
  • $\begingroup$ I don't understand. It seems that: y = x1 + x2; Manipulate[Evaluate[y], {x1, 0, 1}, {x2, 0, 1}] works fine. $\endgroup$ – bill s Jan 9 '15 at 20:23
2
$\begingroup$
myEq = (y = x1 + x2);

Manipulate[Evaluate[myEq], {x1, 0, 1},  {x2, 0, 1}]
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – user3096228 Dec 10 '14 at 16:49
  • 1
    $\begingroup$ 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. $\endgroup$ – David G. Stork Dec 10 '14 at 16:53
  • $\begingroup$ 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". $\endgroup$ – user3096228 Dec 10 '14 at 17:10
  • 1
    $\begingroup$ Then I think you want belisarus' solution. $\endgroup$ – David G. Stork Dec 10 '14 at 17:23
  • $\begingroup$ 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. $\endgroup$ – user3096228 Dec 10 '14 at 18:08
0
$\begingroup$

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

enter image description here

$\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.