Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

extract only positive solutions

946 views
Skip to first unread message

Gabby Domingo

unread,
Feb 17, 2012, 6:24:16 AM2/17/12
to
hi all! how do you extract only positive solutions to a solver, like
NSolve?

sol={{-.532,.932},{.34,23}}

and extract {.34,.23}?

Bill Rowe

unread,
Feb 18, 2012, 6:29:22 AM2/18/12
to
Here is an example of one way

In[3]:= p = (x + 3) (x - 4) (x - 5) // Expand;

In[4]:= Cases[Solve[p == 0, x], {_?(x > 0 /. # &)}]

Out[4]= {{x->4},{x->5}}


Murray Eisenberg

unread,
Feb 18, 2012, 6:31:25 AM2/18/12
to
This sort of thing has been asked and answered many times before, and
you'll probably get lots of answers now. here's one way:

Select[pairs, Last@# > 0 &]


On 2/17/12 6:23 AM, Gabby Domingo wrote:
> hi all! how do you extract only positive solutions to a solver, like
> NSolve?
>
> sol={{-.532,.932},{.34,23}}
>
> and extract {.34,.23}?
>

--
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

Bob Hanlon

unread,
Feb 18, 2012, 6:31:56 AM2/18/12
to
sol = {{-.532, .932}, {.34, 23}};

Select[sol, And @@ Positive[#] &]

{{0.34, 23}}

Cases[sol, _?(And @@ Positive[#] &)]

{{0.34, 23}}

Cases[sol, {_?Positive ..}]

{{0.34, 23}}

DeleteCases[sol, {___, _?NonPositive, ___}]

{{0.34, 23}}

DeleteCases[sol, _?(Or @@ NonPositive[#] &)]

{{0.34, 23}}


Bob Hanlon

andres

unread,
Feb 18, 2012, 6:34:30 AM2/18/12
to
I don't fully understand, NSolve (or any solver) gives the solutions
as a set of rules, not as a list of numbers.
>From your example I guess you want to extract the pairs whose first
element is > 0. If that's the case I'd use Select:
Select[sol, #[[1]] > 0 &]
Andr=E9s

Helen Read

unread,
Feb 19, 2012, 6:35:57 AM2/19/12
to
You can restrict the domain in Solve.

For example:

f[x_]:=150 - 15 x - 50 x^2 + 2 x^3 - 6 x^4 + x^5 + 2 x^6

Solve[f[x]==0,x]

Solve[f[x]==0,x,Reals]

Solve[f[x]==0,x,Rationals]

Solve[f[x]==0,x,Integers]

Solve[f[x] == 0 && x > 0, x]


So if I understand what you are trying to do, something like this:


{x, y} /.
Solve[{y == x^2 - 2 x - 5, y == 7 - x, x > 0, y > 0}, {x, y}]


--
Helen Read
University of Vermont

0 new messages