# Problem with NSolve with Logarithm

I have an issue with NSolve, and I think it's because of the logarithms :

f[x_, a_, b_] = x*Log[x] + (1 - x)*Log[1 - x] - a*x^2 + b*x^4
h[x_, a_, b_] = D[f[x, a, b], x, x]
mu[x_, a_, b_] = D[f[x, a, b], x]
p[x_, a_, b_] = f[x, a, b] - x*mu[x, a, b]

a = 3
b = 0

solution = NSolve[mu[x1, a, b] == mu[1 - x1, a, b], {x1}]


And I'm just getting :

NSolve::nsmet: This system cannot be solved with the methods available to NSolve.

So I assume I miswrote something.... I noticed it does't change anything if I write ',' or '&&'.

Or it could be because of the logarithm ? I see that NSolve works for polynomials. Is there a difference ?

You can convince yourself that there are solutions, because $$x_1=0.5$$ is an obvious solution.

• NSolve is first and foremost for polynomial problems (the Details section mentions this in a somewhat off-hand manner). For general non-linear numerical equation solving, FindRoot is what you need. – Sjoerd Smit Oct 10 '19 at 10:18

## 3 Answers

You could try Reduce.

 solution=Reduce[{mu[x1,a,b]==mu[1-x1,a,b]&&0<x1<1},x1]


 N[%]


NSolve is mostly for finding all the solutions to polynomial equations. General non-linear equations should be dealt with by using FindRoot. You can get different solutions by trying different initial guesses for the root. For example, to get the 3 roots in the interval [0, 1], you can try 10 different initial guesses and then delete the duplicates:

DeleteDuplicatesBy[
Cases[
FindRoot[mu[x1, a, b] == mu[1 - x1, a, b], {x1, #}] & /@ Subdivide[0, 1, 10],
{__Rule}],
Round[#[[1, 2]], 0.00001] &
]


{{x1 -> 0.0707202}, {x1 -> 0.5}, {x1 -> 0.92928}}

Clear["Global*"]

f[x_, a_, b_] = x*Log[x] + (1 - x)*Log[1 - x] - a*x^2 + b*x^4;
h[x_, a_, b_] = D[f[x, a, b], x, x];
mu[x_, a_, b_] = D[f[x, a, b], x];
p[x_, a_, b_] = f[x, a, b] - x*mu[x, a, b];

a = 3;
b = 0;


NSolve works when you restrict the domain to Reals

solution = NSolve[mu[x1, a, b] == mu[1 - x1, a, b], x1, Reals]

{{x1 -> 0.0707202}, {x1 -> 0.5}, {x1 -> 0.92928}}


Solve will provide the exact solutions, again by restricting the domain to Reals. Two of the exact roots are expressed as Root objects.

(solutionExact =
Solve[mu[x1, a, b] == mu[1 - x1, a, b], x1, Reals]) // InputForm

(* {{x1 -> 1/2},
{x1 -> Root[
{-3 + Log[(1 - #1)/#1] +
6*#1 & , 0.07072018167994481\
8926740689090586038120.31284969019\
9485}]},
{x1 -> Root[
{-3 + Log[(1 - #1)/#1] +
6*#1 & , 0.92927981832005518\
10732460760196131134620.3011190594\
37458}]}} *)

solutionExact // N

(* {{x1 -> 0.5}, {x1 -> 0.0707202}, {x1 -> 0.92928}} *)
`