2
$\begingroup$

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.

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Oct 10, 2019 at 10:18

3 Answers 3

4
$\begingroup$

You could try Reduce.

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

Mathematica graphics

 N[%]

Mathematica graphics

$\endgroup$
3
$\begingroup$

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}}

$\endgroup$
3
$\begingroup$
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\
89267406890905860381`20.31284969019\
9485}]}, 
 {x1 -> Root[
    {-3 + Log[(1 - #1)/#1] + 
       6*#1 & , 0.92927981832005518\
107324607601961311346`20.3011190594\
37458}]}} *)

solutionExact // N

(* {{x1 -> 0.5}, {x1 -> 0.0707202}, {x1 -> 0.92928}} *)
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.