# Apply solution rules to new functions as parameters [duplicate]

Update: I put an screenshot of how my code and problem look like at the end.

Say that Mathematica solved a system of equations and got two answers {{a -> 3, b -> c, d -> 0}, {a -> -3, b -> 0, c -> 1/2d}}. (These answers are completely made up and don't mean anything) I want to plug in those numbers/rules in the function f = ax + by + c*z + d and do contourPlot3D with f==0.

The thing is if I apply rules and get the substituted expression and manually paste the expression in contourPlot3D, it works fine, but if I try to do it in a more systematic way, it doesn't work.

So this doesn't work:

solution = Solve[constraints, {g, g, g, g, h, h, h, h}];
gplot[k_] := g*x + g*y + g*z + g /. solution[[k]];
hplot[k_] := h*x + h*y + h*z + h /. solution[[k]];
Manipulate[
ContourPlot3D[gplot == 0, , {x, -1, 1}, {y, -1, 1}, {z, -1, 1}],
{g, -1, 1}, {g, -2, 2}, {h, -1, 1}, {h, -2, 2}]


But this works, :

Manipulate[
ContourPlot3D[x g + y g + g == 0, , {x, -1, 1}, {y, -1, 1}, {z, -1, 1}],
{g, -1, 1}, {g, -2, 2}, {h, -1, 1}, {h, -2, 2}]


I thought gplot1 should be the same as x g1 + y g1 + g, as that's how I copy paste into the contourplot function, but then as I print out gplot1 again, it changes to -2.22222*10^-7 g1 + g. I think this is what's causing the plot to fail, but I don't understand why this happens. Why did my x,y,z variables turn into weird constants?

Or is there another way to apply solution rules to new functions as parameters and plot the equations that can avoid such problems?

Screenshot: • In your example gplot has its parameters g .. g replaced with the solution, so there are no variables for the Manipulate left to play with. – Sjoerd C. de Vries Jun 24 '15 at 12:05
• But x,y,z should remain variables. – Xilin Jun 24 '15 at 12:15
• Yes, but the problem is that g etc are not part anymore of the plot equation. So, you end up with a fixed plot (after you have removed the erroneous comma in your plot command) that doesn't change anymore when the g's and h's are manipulated. – Sjoerd C. de Vries Jun 24 '15 at 12:38
• I think that @SjoerdC has pinpointed the mechanical cause of the failure of your Manipulate, but I think that there is also some conceptual problem here. It would be helpful if you could show us what solutions looks like in your case. For instance, I am not sure what is causing that value of the gplot function. Is there any chance that you might have some lingering definitions for your variables leftover from previous execution? Have you tried in a fresh kernel (from the menus, "Evaluation -> Quit kernel -> Local")? – MarcoB Jun 24 '15 at 18:02
• @SjoerdC.deVries Oh I see what you mean. The thing is the solution does not give numerical value to every parameter, that's why I used Manipulate for g and g. solution[] is actually this: {g -> g, g -> 0, h -> 0, h -> 0}. And I think Manipulate worked, because I see a interface to change g and g, but the ContourPlot didn't recognize my equation and where there's suppose to be the graph, there is the code for ContourPlot. – Xilin Jun 25 '15 at 6:49

You are running afoul of the (beneficial) scoping that is applied inside Manipulate constructs by way of DynamicModule (or the low-level equivalent). If you "inject" the expression containing g etc. into the Manipulate before it is evaluated it should work correctly I believe:

With[{body = gplot == 0},
Manipulate[
ContourPlot3D[body, {x, -1, 1}, {y, -1, 1}, {z, -1, 1}],
{g, -1, 1}, {g, -2, 2}, {h, -1, 1}, {h, -2, 2}
]
]

• Thank you so much. It works now! I don't quite understand what you mean by "way of Dynamic Module" though. And how does using With[{body = gplot == 0} inject the expression before it's evaluated? I'm very new to Mathematica, so I should apologize first for my tons of questions. – Xilin Jun 26 '15 at 8:59
• @Xilin You're diving into the deep end when working with Dynamic expressions as thought they appear simple they are actually rather involved. (Manipulate is an abstraction for dynamic functionality.) DynamicModule is a special variation of Module. It is the scoping construct that should be used with dynamic expressions. In Mathematica this localization is "emulated" by renaming Symbols on-the-fly. Your Symbols must appear inside Manipulate before it is evaluated for the localization (renaming) to work properly. (continued) – Mr.Wizard Jun 26 '15 at 9:04
• With replaces literal appearances of a given Symbol with a given expression in the body (second argument) of the With expression before that expression is further evaluated. Consider for example x = 2; With[{x = 5}, Print[x]]; -- 5 is printed because x in Print[x] is replaced with 5 before Print is evaluated. Note that x still has the value of 2 after running this code. – Mr.Wizard Jun 26 '15 at 9:08
• @Xilin By the way I like this example best for understanding the basic Mathematica scoping constructs. – Mr.Wizard Jun 26 '15 at 9:12
• Thank you, your examples are vey helpful!!! – Xilin Jun 26 '15 at 10:47