# FindRoot does not work with Piecewise including complex-valued function

Suppose we have the following Piecewise function:

gg[x_?NumericQ] :=
Piecewise[{{x^4 - 6 x^3 + 5, x <= 1}, {8 x^2 - x^7, x > 1}}]


We can find the root of the function easily by:

FindRoot[gg[x], {x, 10}]
(* {x -> 1.51572} *)


The problem emerges when we make for example the second function complex:

gcom[x_?NumericQ] :=
Piecewise[{{x^4 - 6 x^3 + 5, x <= 1}, {8 x^2 - x^7 I, x > 1}}]


In this case FindRoot does not work:

FindRoot[gcom[x], {x, 10}]

LessEqual::nord: Invalid comparison with 1.24121 -0.147854 I attempted.

Greater::nord: Invalid comparison with 1.24121 -0.147854 I attempted.

LessEqual::nord: Invalid comparison with 1.24121 -0.147854 I attempted.

Greater::nord: Invalid comparison with 1.24121 -0.147854 I attempted.

FindRoot::nlnum: The function value {\[Piecewise]   -3.813+2.96586 I    1.24121 -0.147854 I<=1
8.6317 -6.15431 I   1.24121 -0.147854 I>1
0   True

} is not a list of numbers with dimensions {1} at {x} = {1.24121 -0.147854 I}.


Note that outside the Piecewise function Mathematica can find the complex root:

FindRoot[8 x^2 - x^7 I, {x, 10}]

(* {x -> 1.44153 - 0.468382 I} *)


So my question is how can I find the complex roots of a complex-valued function in a Piecewise?

• The solution lies on a jump discontinuity of the function, right? How do you expect FindRoot to find that? Moreover, you equation is overdetermined: two real quations for one real parameter. – Henrik Schumacher Jun 14 '18 at 16:17
• @Schumacher: I have a piecewise function with two functions for x greater and smaller than 1. It is my question: FindRoot can find the complex root of the function, but not inside a piecewise. – Soodeh Z. Jun 14 '18 at 16:23
• The problem is that your solution is complex, and your function gcom doesn't work with complex input, e.g., gcom[1+I] doesn't work. You need to change the conditions in your piecewise so that they work with complex input. – Carl Woll Jun 14 '18 at 16:24
• I don't think that Piecewise has meaning for a complex function. FindRoot must enter a complex number for your argument. How will Piecewise deal with this. Perhaps you need to define branch cuts. – Hugh Jun 14 '18 at 16:25
• Maybe you are looking for gcom[x_?NumericQ] := Piecewise[{{x^4 - 6 x^3 + 5, Re[x] <= 1}, {8 x^2 - x^7 I, Re[x] > 1}}]; FindRoot[gcom[x], {x, 10}]? – Henrik Schumacher Jun 14 '18 at 16:26

As I said in the comments, the solution to your FindRoot is a complex number. On the other hand, your function gcom only works when the input is a real number. This is what the error message is telling you. One possibility is to include a real check in your conditions (this is just one possibility). For example:

gcom[x_?NumericQ] := Piecewise[
{{x^4 - 6 x^3 + 5, Element[x, Reals] && x <= 1}},
8 x^2 - x^7 I (* otherwise *)
]


(another possibility as suggested by @Henrik in the comments is Re[x] <= 1)

Then:

FindRoot[gcom[x], {x,10}]


{x -> 1.44153 - 0.468382 I}

• @Woll: This works :). Thanks a lot. – Soodeh Z. Jun 14 '18 at 16:34