1
$\begingroup$

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?

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

Minimize + MinimalBy work on the test case:

MinimalBy[Minimize[x /. #, C[1]] & /@ solutions, First]
(*  {{π/2, {C[1] -> 0}}}  *)
$\endgroup$
  • $\begingroup$ I didn't realize ConditionalExpression works inside Minimize and friends. I should've known! (+1) $\endgroup$ – Chip Hurst Jun 19 '16 at 20:59
2
$\begingroup$

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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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