7
$\begingroup$

How can I solve this equation (both numerically and literally) only in the positive reals $\mathbb{R}^+$?

Solve[x == (v0 - (A CD t v0^2 ρ)/(4m)) Cos[θ] t, t]

And for example, is there a way to have an output like this :

52.0756

and not like this :

{{t -> -52.3918}, {t -> 52.0756}}

?

$\endgroup$
2
  • $\begingroup$ You can add a condition like sol=Solve[{...,t>0},t]; then you can do sol[[All,1,2]]. $\endgroup$ Commented Nov 6, 2012 at 12:26
  • $\begingroup$ For the first leg of your question, there is a good tutorial in the documentation of Mathematica entitled "equations and inequalities over domains". Reduce is what you probably want to try out. As for the solutions being given as rules, the documentation of Solve has every possible way to extract those. $\endgroup$
    – gpap
    Commented Nov 6, 2012 at 12:33

2 Answers 2

10
$\begingroup$

You could use ReplaceAll (i.e. /.) and Select

Select[x /. Solve[x^2 - 1 == 0, x], Positive]

gives

{1}

It is a list (List) not a single number. You might not know how many positive solutions exist:

Select[x /. NSolve[(x - 1) (x + 3) (x - 3) == 0, x], Positive]

{1., 3.}

Edit:

Using Part or its short-hand notation [[]] you can select parts from the list:

Part[{1}, 1]

1

{1}[[1]]

1

Part[{1.,3.},2]
    3.
$\endgroup$
4
  • $\begingroup$ Isn't possible remove from the output also the braces, right? $\endgroup$
    – Aurelius
    Commented Nov 6, 2012 at 17:09
  • $\begingroup$ @FormlessCloud Yes you can if you have one result, however how would you return 2 numbers without braces ? $\endgroup$ Commented Nov 6, 2012 at 17:25
  • $\begingroup$ I have selected the positive reals appositely for have one result, but I don't know how to remove the braces from the output. I don't want the braces because I want use the result as a value for a command and if I keep the braces the result can't be taken as numerical value. $\endgroup$
    – Aurelius
    Commented Nov 6, 2012 at 21:36
  • 1
    $\begingroup$ @FormlessCloud I updated the answer to show how to get rid of the braces. $\endgroup$ Commented Nov 7, 2012 at 7:37
14
$\begingroup$

The first items of More Information in the documentation of Solve says :

  • The system expr in Solve[expr,vars] can be any logical combination of:

    lhs == rhs               equations 
    lhs != rhs               inequations 
    lhs > rhs or lhs >= rhs  inequalities 
    expr ∈ dom               domain specifications 
    ForAll[x,cond,expr]      universal quantifiers 
    Exists[x,cond,expr]      existential quantifiers 
    
  • Solve[{ expr1, expr2,...},vars] is equivalent to Solve[ expr1 && expr2 &&...,vars].

Every expri can be an equation, inequality as well as an expression tests like e.g. Positive or Negative etc., thus we can do simply e.g. Solve[-36 + 49 x^2 - 14 x^4 + x^6 == 0 && x > 0, x], but to get only the list of solutions (without Rules ) there are at least two ways:

  • using ReplaceAll (shorthand /.) (mentioned by Markus Roellig) with the condition x > 0 :

    x/.Solve[-36 + 49 x^2 - 14 x^4 + x^6 == 0 && x > 0, x]
    
    {1, 2, 3}
    
  • using Part (shorthand [[]]) with e.g. x > 0 or with an expression test like Positive, NonNegative etc.:

    Solve[-36 + 49 x^2 - 14 x^4 + x^6 == 0 && Positive[x], x][[All, 1, 2]]
    
     {1, 2, 3} 
    

The above ways can be mixed, e.g. : x /. Solve[-36 + 49 x^2 - 14 x^4 + x^6 == 0 && x > 0, x][[3]]. We needn't point out the domain Reals since the condition x > 0 implies that x is a positive and real number. The same concerns Reduce, i.e. use it like e.g.

Reduce[-36 + 49 x^2 - 14 x^4 + x^6 == 0 && x > 0, x][[All, 2]]
$\endgroup$
4
  • $\begingroup$ Not valid in Mathematica 6, I think? $\endgroup$ Commented Sep 12, 2014 at 0:04
  • $\begingroup$ @SanjayManohar What do you mean? Solve has been updated since the version 6 nevertheless all this syntax was valid, or perhaps you missed something? $\endgroup$
    – Artes
    Commented Sep 12, 2014 at 0:12
  • $\begingroup$ on Mathematica 6.0: Solve[-36 + 49 x^2 - 14 x^4 + x^6 == 0 && x > 0, x] ==> "Solve::eqf : x>0 is not a well-formed equation" $\endgroup$ Commented Sep 13, 2014 at 8:28
  • $\begingroup$ @SanjayManohar In version 6 one could do it with Reduce e.g. Reduce[-36 + 49 x^2 - 14 x^4 + x^6 == 0 && x > 0, x], Solve has worked this way since version 8, before equations had to be given in the form lhs == rhs only, although e.g. ForAll, Exists had been introduced in version 5. $\endgroup$
    – Artes
    Commented Sep 13, 2014 at 8:57

Your Answer

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

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