# Find smallest solution in Solve's solution set

When an equation has multiple solutions, how do you find the first solution?

Example:

Say we're trying to find the first positive x-intercept of the cosine function:

$cos(x)=0 \land x \ge 0$

Running Solve, we obtain the set of solutions:

solutions = Solve[Cos[x] == 0 && x >= 0, x]


$\left\{\left\{x\to \text{ConditionalExpression}\left[\frac{1}{2} \left(4 \pi c_1-\pi \right),c_1\in \mathbb{Z}\land c_1\geq 1\right]\right\},\left\{x\to \text{ConditionalExpression}\left[\frac{1}{2} \left(4 \pi c_1+\pi \right),c_1\in \mathbb{Z}\land c_1\geq 0\right]\right\}\right\}$

To proceed, we need to find the C[1] that minimizes the substitution for x. How do we do this?

• You could just provide explicit brackets, guessing the upper limit after looking at a plot: Solve[{Cos[x] == 0, 0 <= x <= 2}, x] – J. M.'s ennui Jun 19 '16 at 17:28
• A numeric approach: Needs["NumericalCalculus"]; NLimit[ ArgMin[{eps*x^2 + Cos[x]^2, x > 0}, x], eps -> 0]. – Daniel Lichtblau Jun 19 '16 at 22:05

Minimize + MinimalBy work on the test case:

MinimalBy[Minimize[x /. #, C[1]] & /@ solutions, First]
(*  {{π/2, {C[1] -> 0}}}  *)

• I didn't realize ConditionalExpression works inside Minimize and friends. I should've known! (+1) – Chip Hurst Jun 19 '16 at 20:59

Maybe something like

MinimalSolveSolution[expr_, x_, o___] :=
MinimalSolveSolution[Solve[expr, x, o]]

MinimalSolveSolution[sol:{{x_ -> _}..}] :=
iMinimalSolveSolution[x /. sol]

MinimalSolveSolution[__] = \$Failed;

iMinimalSolveSolution[l_List] := Min[iMinimalSolveSolution /@ l]

iMinimalSolveSolution[ConditionalExpression[val_, cond_]] :=
MinValue[{val, cond}, Union[Cases[val, _C, {0, ∞}]]]

iMinimalSolveSolution[v_] := v


I didn't really stress test this solution, but it works for your given input:

MinimalSolveSolution[Cos[x] == 0 && x >= 0, x]

π/2
`