Message Boards Message Boards

0
|
3185 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Get solutions from NSolve in a consistent manner

I am trying to solve a system of three equations with Mathematica's NSolve, for some functions which depend on six parameters, however, three of them are fixed so only the other three will be my solutions. The program seems to work, and I get the three solutions for some values of the fixed parameters, but, the solutions are printed in a different order than the one written in NSolve function. More precisely: ```

NSolve[EnergyBand[level1, SpinBand[level1][[state1]], x, y, z, [Theta]] == ExperimentalBand[level1][[state1]] && EnergyBand[level2, SpinBand[level2][[state2]], x, y, z, [Theta]] ==ExperimentalBand[level2][[state2]] && EnergyBand[level3, SpinBand[level3][[state3]], x, y, z, [Theta]]== ExperimentalBand[level3][[state3]], {x, y, z}, Reals];

` The functionsEnergyBandandExperimentalBandare not relevant here since they just return numbers, andlevel1,2,3withstate1,2,3` are known integers. If i print the solutions for some values of \Theta (the number starting from -147, this is what Mathematica shows me: ```

-147
{{z->1.80464,y->0.0250839,x->0.0540758}}
-146
{{z->1.41809,y->0.0239437,x->0.0518834}}
-145
{{z->1.0827,y->0.0228379,x->0.0497059},{z->1.0827,y->0.0228379,x->0.0497059}}
-144
{{z->0.797314,y->0.0217725,x->0.0475389},{z->0.797314,y->0.0217725,x->0.0475389}}
-143
{{z->0.560211,y->0.0207584,x->0.045374}}
```

Basically the solutions are printed as {z,y,x} instead of {x,y,z} It is important for me that the values should always have the same order as {x,y,z}. Can I somehow change that in the NSolve procedure? The reason why I need a consistent order of the solutions is that at some point in my code I have to access them (like Values@NSolve[...][[k,1]] or Values@NSolve[...][[k,2]] and I need to be 100% sure that those values are x or y respectively.

So, my question is how can I impose a specific order on the solutions the program spits out? Or how can I know which will be the order of the solutions at runtime?

Thanks in advance!

POSTED BY: Robert Poenaru
2 Replies
Posted 4 years ago

Hi Robert,

I would not rely on the position being consistent. Much better to use replacement to get the order you want.

sol = {{z -> 1.80464, y -> 0.0250839, x -> 0.0540758}};

{x, y, z} /. sol
(* {{0.0540758, 0.0250839, 1.80464}} *)

{y, x, z} /. sol
(* {{0.0250839, 0.0540758, 1.80464}} *)
POSTED BY: Rohit Namjoshi

Ooops...I see that there is no horizontal scroll here?! So my code doesn't show up properly on the page?

POSTED BY: Robert Poenaru
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract