# Contour plot of a scalar function over the complex domain in MATLAB

I'm trying to make a 2D contour plot of a function with a complex input and a real output, namely: $f(z=x+iy)= |z^2-3z|$, on MATLAB. I want to make a contour plot of f, such that the axes are $(x,y)$, i.e. $(\Re(z),\Im(z))$.

How is this done in MATLAB? The problem I'm having is the fact the input is complex.

• Possible duplicate of Nice plot of f(x) Nov 15, 2016 at 20:22
• Please have a look at this answer if it doesn't have to be in matlab perse scicomp.stackexchange.com/questions/24144/nice-plot-of-fx Nov 15, 2016 at 20:39
• [x,y] = meshgrid(-10:10); z = x+1i*y; contour(x,y,abs(z.^2-3*z)); Nov 15, 2016 at 21:22
• Cross-posted on Math.SEplease don't cross-post. Nov 15, 2016 at 22:30

You need to define a mesh over which your function will be evaluated

[mfRe, mfIm] = ndgrid(-1, 1, 100);


fh = @(z)abs(z.^2 - 3 .* z);
mfF = fh(complex(mfRe, mfIm));


Now make the contour plot and beautify

contour(mfRe, mfIm, mfF);
axis equal;
xlabel('Re(z)');
ylabel('Im(z)'); 