# How to plot a complex functions

I am new to mathematica. I am trying to plot a complex function, though my function is complicated one I am trying to start from a basic one.

Suppose I have a function f[z]=z^3-z^2-z-1==0 where z=x+i*y, I would like to plot the function in two different ways

1. Plot the curve for Re[f] and Im[f] in the interval defined for x and y (Example taken from this link http://mathworld.wolfram.com/Root.html), the plot looks like this

2. 2D color plot (Projection of the value of f[x+i*y] on the x-y plane). The motivation behind this plot is to search for the minimum of the function value (roots of the function)

Thank You

What you see as red and blue lines are the contours where the real- and imaginary-part of the complex expression is zero. For this, you can use ContourPlot. You have to make explicit that z is a complex variable that is x+I*y:

expr = z^3 - z^2 - z - 1;
expr1 = ReIm[expr /. {z -> x + I y}];


After that, you can tell to plot the zero contour like this (MaxRecursion is only for the beauty):

cp = ContourPlot[expr1, {x, -3, 3}, {y, -3, 3}, Contours -> {0}, MaxRecursion -> 4]


When you know that these lines are the zero lines, you instantly see that the crossings are the points where both are zero. This means you are searching the roots of your complex function.

roots = N@ReIm[z /. {ToRules@Reduce[expr == 0, z]}];
Show[cp, Graphics[{PointSize[0.03], Point[roots]}]]


Btw, if you feel the roots lines is to complicated as a beginner, just rip it apart. You need the roots of your complex function and your expression is easy enough that you can do this analytically using Reduce:

Reduce[expr == 0, z]
(* z == Root[-1 - #1 - #1^2 + #1^3 &, 1] ||
z == Root[-1 - #1 - #1^2 + #1^3 &, 2] ||
z == Root[-1 - #1 - #1^2 + #1^3 &, 3] *)


Mathematica gives you Root expressions which basically means "the result is the root of this polynomial and I can give you the numbers if you like, but this representation is more clear and I'll keep it as long as you don't tell me otherwise".

Furthermore, you see that Reduce gives a logical expression: solution 1 or solution 2 or solution 3. ToRules will convert this into replacement rules and we can use z /. to access it. Since the solutions are points in the complex plane I'm using ReIm to get their real- and imaginary-part and finally N to convert it to numbers.

## Edit

As Bob mentioned in the comments, it maybe wasn't the best move to directly jump to Reduce (whose difference to Solve is not obvious to newcomers). You will get a better representation of your roots directly when you use Solve:

Solve[expr == 0, z]
(* {
{z ->
1/3 (1 + (19 - 3 Sqrt[33])^(1/3) + (19 + 3 Sqrt[33])^(1/3))},
{z ->
1/3 - 1/6 (1 + I Sqrt[3]) (19 - 3 Sqrt[33])^(1/3) -
1/6 (1 - I Sqrt[3]) (19 + 3 Sqrt[33])^(1/3)},
{z ->
1/3 - 1/6 (1 - I Sqrt[3]) (19 - 3 Sqrt[33])^(1/3) -
1/6 (1 + I Sqrt[3]) (19 + 3 Sqrt[33])^(1/3)}
} *)

• Since the OP is new to Mathematica, radicals may be preferred to Root objects. Either Reduce[expr == 0, z] // ToRadicals // Simplify or Solve[expr == 0, z] // Simplify may be of interest. – Bob Hanlon Feb 10 '18 at 15:32
• @BobHanlon That's a good idea. I included the Solve case in the answer as it seems vastly superior for beginners. – halirutan Feb 10 '18 at 15:46
• @halirutan Thanks. Can you please suggest me with my second part of the question. What suppose if I want to get a visual picture (say find the minimum of the complex function expr==0 in the x-y plane) to locate the region of complex plane where there are roots? – T S Singh Feb 11 '18 at 4:09
• @halirutan (To the best of my knowledge :-)) I think you have found the intersection point (roots) using Reduce command and then marked those point. Can this be done by using ContourPlot only without using Reduce (As in my equation its taking a lot of time using Reduce and also before using this I am trying to search for domain where solution is available) . Any suggestion – T S Singh Feb 11 '18 at 8:40
• @TSSingh I would not do that. ContourPlot is for plotting and it can make mistakes for non-trivial functions. Furthermore, if you have a complex polynomial like in your case, you cannot talk of a minimum. For this, you would need the < to compare complex numbers. This does not make sense for complex numbers. Roots however, where both the Re and Im is zero, does make sense. There are a lot of algorithms out there for finding roots and for polynomials the situation is even more clear. The correct procedure depends on your complex function, but I would not use ContourPlot for that. – halirutan Feb 11 '18 at 15:31