3
$\begingroup$

I want to solve the a transcendental equation, e.g.

NSolve[-(1-x)*Log[1-x] - x*Log[x] == 0.5, x]

Mathematica is not able to solve, wheres WolframAlpha -- is able to do it.

The purpose it to find the probabilities for two events in accordance to a given entropy value.

$\endgroup$
3
  • 1
    $\begingroup$ Try FindRoot instead (look it up in the help pages). And be careful that there is a space or a * between x and Log[x] in your expression. $\endgroup$
    – march
    Commented Jul 6, 2015 at 20:04
  • $\begingroup$ In particular, Chop@FindRoot[-(1 - x) Log[1 - x] - x Log[x] == 0.5, {x, 2}] gives an answer of 0.80029. FindRoot is preferable to NSolve for most transcendental equations. $\endgroup$
    – bbgodfrey
    Commented Jul 6, 2015 at 20:07
  • 2
    $\begingroup$ NSolve[-(1 - x)*Log[1 - x] - x*Log[x] == 0.5, x, Reals] ? $\endgroup$
    – Greg Hurst
    Commented Jul 6, 2015 at 20:13

1 Answer 1

6
$\begingroup$
Reduce[-(1 - x) Log[1 - x] - x*Log[x] == 1/2, x, Reals]
(*
 x == Root[{-1 - 2 Log[#1] #1 + Log[1 - #1] (-2 + 2 #1) &, 0.199709902553977194585}] || 
 x == Root[{-1 - 2 Log[#1] #1 + Log[1 - #1] (-2 + 2 #1) &, 0.80029009744602280541}]
*)
% // N
(* x == 0.19971 || x == 0.80029 *)

Plot[{1/2, -(1 - x) Log[1 - x] - x*Log[x]}, {x, 0, 1}]

Mathematica graphics

$\endgroup$
2
  • 4
    $\begingroup$ +1. Why not Solve[-(1 - x) Log[1 - x] - x*Log[x] == 1/2, x, Reals]? $\endgroup$
    – Michael E2
    Commented Jul 6, 2015 at 20:11
  • 4
    $\begingroup$ @MichaelE2 I tend to use Reduce[ ] when you may expect branching issues. You're right in this case, though. $\endgroup$ Commented Jul 6, 2015 at 20:13

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.