Re: How to choose real positive solutions only?
- To: mathgroup at smc.vnet.net
- Subject: [mg74346] Re: How to choose real positive solutions only?
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Mon, 19 Mar 2007 02:05:54 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <etije4$irj$1@smc.vnet.net>
Dix, Manfredo A wrote: > Hello, > this question probably came up before, but anyway: > > suppose I have a system of 3 equations and 3 unknowns, > which I want to solve numerically with NSolve. > They are polynomial of degree "n" so that I get n solutions. > > Is there a way to tell Mathematica to just return the > real positive solutions, and dismiss the imaginary and negative > ones? > > thank you so much for any help, > cordially, > > Manfred > mdix at tulane.edu > > You cannot. However, you case use Mathematica pattern matching capabilities to extract whatever you want from the solutions returned by NSolve. For instance, In[1]:= eqns = {x^5 + y^2 + z == 1, 2*x + y - z == 2, x - 3*y + z == 3}; sols = NSolve[eqns, {x, y, z}] Out[2]= {{x\[Rule]-1.69511, y\[Rule]-5.04267,z\[Rule]-10.4329}, {x\[Rule]0.24097 + 1.42038 I, y\[Rule] -2.13854 + 2.13058 I, z\[Rule] -3.6566 + 4.97134 I}, {x\[Rule] 0.24097 - 1.42038 I, y\[Rule] -2.13854 - 2.13058 I, z\[Rule] -3.6566 - 4.97134 I}, {x\[Rule] 1., y\[Rule] -1., z\[Rule] -1.}, {x\[Rule] 0.213171, y\[Rule] -2.18024, z\[Rule] -3.7539}} In[3]:= (* We want the three variables negative *) Cases[sols, {x_ -> xval_Real /; xval < 0, y_ -> yval_Real /; yval < 0, z_ -> zval_Real /; zval < 0}] Out[4]= {{x\[Rule]-1.69511, y\[Rule]-5.04267, z\[Rule]-10.4329}} In[5]:= (* We want the first variable positive and the other two negative *) Cases[sols, {x_ -> xval_Real /; xval > 0, y_ -> yval_Real /; yval < 0, z_ -> zval_Real /; zval < 0}] Out[6]= {{x\[Rule]1., y\[Rule]-1., z\[Rule]-1.}, {x\[Rule]0.213171, y\[Rule]-2.18024, z\[Rule]-3.7539}} HTH, Jean-Marc