# Plot[] fails to show all results of Solve[]

I'm interested in the intersection points of two functions: $$y=r+x$$, where $$r$$ is the constant, and $$y=ln(1+x)$$. The following code plots the two intersection points for different (negative values) of $$r$$ correctly and as expected:

Manipulate[
pts = Quiet[Solve[y == r + x && y == Log[1 + x], {x, y}]];
Graphics[Point[{x, y} /. pts], PlotRange -> {{-1, 4}, {-3, 2}}, FrameLabel -> {x, y}, Frame -> True],
{r, -2, 0}]


However, if I try to plot data corresponding only to $$x$$ coordinate (or $$y$$) as a function of constant $$r$$ using a similar code (logic) I get only one intersection point (for $$r<0$$). The code in question is the following:

xr[r_] := Quiet[Solve[y == r + x && y == Log[1 + x], {x, y}]][[1,1](*Part: flattening and selecting x only*)
Plot[Evaluate[x /. xr[r]], {r, -2, 0}, FrameLabel -> {r, x}, Frame -> True, PlotRange -> All]


Why isn't Plot[] able to access both intersection points (it did it above)? It shows only the negative $$x$$ values (positive branch is omitted).

P.S. Solve is also unable to produce the complete result by solving $$r+x-ln(1+x)=0$$ for $$x$$, that's the reason why I'm solving the system of two equations rather than this single equation.

Since Solve uses inverse functions, not all solutions get returned. You could use Reduce instead:

Reduce[r+x == Log[1+x], x, Reals]


Reduce::useq: The answer found by Reduce contains unsolved equation(s) {0==-1+r-Log[-ProductLog[-1,Times[<<2>>]]]-ProductLog[-1,-E^Plus[<<2>>]],0==-1+r-Log[-ProductLog[Times[<<2>>]]]-ProductLog[-E^Plus[<<2>>]]}. A likely reason for this is that the solution set depends on branch cuts of Wolfram Language functions.

(r == 0 && x == 0) || (0 == -1 + r - Log[-ProductLog[-1, -E^(-1 + r)]] - ProductLog[-1, -E^(-1 + r)] && r <= 0 && x == -1 - ProductLog[-1, -E^(-1 + r)]) || (0 == -1 + r - Log[-ProductLog[-E^(-1 + r)]] - ProductLog[-E^(-1 + r)] && r <= 0 && x == -1 - ProductLog[-E^(-1 + r)])

The output is messy and generates messages, but the key is the two-arg form of ProductLog. So, a function that returns both roots is:

f[r_] := -1 - ProductLog[{0, -1}, -Exp[-1 + r]]


Example:

f[-.89]


{-0.818943, 1.98289}

• Thank you Carl! Reduce[] did indeed generate the correct results. If it's not a bother, I still don't understand one thing, Solve[] seemed to generate both real roots inside the Manipulate[] function in my first code snippet. Why can't it do it in the second code example? '{x, y} /. pts' replacement seems to somehow help the situation in the first example. Nov 30, 2018 at 21:06

If r is greater than zero the two lines do not cross and the solutions are imaginary.

We can make a little module to solve for x and y given r

xy[r_] :=
Module[{},
Transpose[{x, y}] /.
Quiet[Solve[y == r + x && y == Log[1 + x], {x, y}, Reals]]]


To illustrate this, we can plot the two lines and the real solutions as we adjust R

Manipulate[
Show[
Plot[{r + x, Log[1 + x]}, {x, -10, 10}],
ListPlot[xy[r], PlotStyle -> PointSize[Large]]
], {{r, -2}, -10, 10}]


Get solutions without error messages. Since you regard real solutions, apply Exp to both sides of eqn und use Method->Reduce.

eqn = r + x == Log[1 + x];

xsol[r_] = x /. Solve[Map[Exp, eqn, 1], x, Reals, Method -> Reduce]

(*   {ConditionalExpression[-1 - ProductLog[-E^(-1 + r)], r <= 0],
ConditionalExpression[-1 - ProductLog[-1, -E^(-1 + r)], r <= 0]}   *)

xsol[-.89]
(*   {-0.818943, 1.98289}   *)

Plot[xsol[r], {r, -10, 0}]


ParametricPlot[Thread[{xsol[r], r}], {r, -10, 0}, AspectRatio -> 1,
PlotRange -> Full]