0
$\begingroup$

I have the following Manipulate[] working just fine.

Manipulate[
permut = Permutations[{eq1, eq2, eq3, eq4, eq5}, {3}];
eq1 = x^a - 4 y - z^b == 1;
eq2 = Sqrt[x] + y + 3 z == 4;
eq3 = x - y^2 + Sqrt[z] == 2;
eq4 = x^a - 4 y^a - z^c == 6;
eq5 = x^b + 6 y - z^c == 8;
NSolve[permut[[eqSet]], {x, y, z}, Reals],
{a, -2, 2, 1},
{b, -2, 2, 1},
{c, -1, 1, 0.5},
{eqSet, 1, Length[permut], 1}
]

I want to solve a system of 3 equations with varying parameters. With Permutations[] I like to run a different set of 3 equations for each element of index "eqSet". I can do that for each element of Permutations[], no problem. My problem here is that I cannot see the group of 3 equations solved. eqSet is an index to pick a set of 3 equations from permutations but I cannot know which set of equations have been solved.

[1] Can I get the names (such as eq1, e2, etc) of the actual set of equations being solved, for example, "eq2, eq5, eq4}"?

[2] Suppose that I solved the set of equations: "{eq2, eq5, eq4}", but now I like to replace "eq2" with equation "eq1" in this set without changing counter value "eqSet". How can I do this?

Thanks.

$\endgroup$
2
  • 1
    $\begingroup$ If I replace NSolve[permut[[eqSet]],{x,y,z},Reals] with names=Permutations[{"eq1","eq2","eq3","eq4","eq5"},{3}]; {names[[eqSet]],NSolve[permut[[eqSet]],{x,y,z},Reals]} then it displays the names of the three equations along with the solution. $\endgroup$
    – Bill
    Jul 19, 2020 at 19:05
  • $\begingroup$ @Bill: Yes, it works in the way I expected. Thanks a lot. $\endgroup$ Jul 19, 2020 at 20:09

1 Answer 1

3
$\begingroup$

You can use a RadioButtonBar to pick the 10 combinations of equations. Use Subsets[equations, {3}] instead of Permutations because this avoids all the choices where you have a repeated equation.

It's a bit clunky and I didn't know how to represent this very well with the UI elements, but I've also added a CheckboxBar so you can select the equations from the first set to replace with equations in the second set.

mysolve[a1_, b1_, c1_, d1_, eqs_] := {x, y, z} /. 
  Quiet[NSolve[eqs /. {a -> a1, b -> b1, c -> c1, d -> d1}, {x, y, z},
     Reals]]
permut = Subsets[Range[5], {3}];
equations1 = {x^a - 4 y - z^b == 1, Sqrt[x] + y + 3 z == 4, 
   x - y^2 + Sqrt[z] == 2, x^a - 4 y^a - z^c == 6, 
   x^b + 6 y - z^c == 8};
equations2 = {x^d + Sqrt[6 y] - Sqrt[z^c] == 10, x^b + z^d == 14, 
   x^c + 6 y == 12, x^a + y^d - z^c == 7, 
   x^c + 6 Sqrt[y] - Log[z^d] == 9};
pchoice = First@permut;
Labeled[RadioButtonBar[Dynamic[pchoice], 
  permut], "Choose equations.", Left]
replacementchoices = 
  DeleteCases[
   Flatten[Outer[Rule, Range[Length[equations1]], 
     Range[Length[equations2]]]], x_ -> x_];
Labeled[CheckboxBar[Dynamic[replchoice], 
  ToString /@ replacementchoices], "Choose replacements.", Left]
Manipulate[
 results1 = mysolve[a, b, c, d, equations1[[pchoice]]];
 results2 = If[ListQ[replchoice] && Length[replchoice] > 0,
   newequations = ReplacePart[equations1[[pchoice]],
     With[{choices = ToExpression[replchoice]},
      Thread[choices[[All, 1]] -> equations2[[choices[[All, 2]]]]]]];
   mysolve[a, b, c, d, newequations], {}];
 Column[{equations1[[pchoice]],Labeled[results1, "First result", Left], 
   If[ValueQ[newequations], newequations, Nothing], Labeled[results2, "After replacement", Left]}]
 , {a, -2, 2, 1}, {b, -2, 2, 1}, {c, -1, 1, 0.5}, {d, -1, 1, 0.5}]
$\endgroup$
7
  • $\begingroup$ I think my second question escaped from your attention. It was a question for "replacing an equation in the system selected with another equation from the total set of equations". $\endgroup$ Jul 19, 2020 at 20:25
  • $\begingroup$ But you've already got that from the subsets. Every possible triplet of equations is already present to choose from. $\endgroup$
    – flinty
    Jul 19, 2020 at 20:31
  • $\begingroup$ You are absolutely right that I get it from the subset. My real case is a bit different from the example I gave above. In my real case, I have two sets of equations and after getting a solution to one subset in the first set of equations, I replace one or two equations with an equation from the second set by keeping the parameters fixed. For example, if I get a solution to {eq2,eq4,eq1}, then I go to second set and pick eq5 to replace eq1. Sorry I did not make this point clear in my example just to keep the presentation simple enough. $\endgroup$ Jul 19, 2020 at 20:52
  • 1
    $\begingroup$ Can you provide the second set in your question? $\endgroup$
    – flinty
    Jul 19, 2020 at 20:56
  • 1
    $\begingroup$ I've added a way to replace the equations with those from the second set. $\endgroup$
    – flinty
    Jul 19, 2020 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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