0

I want to plot the roots of any given polynomial on the complex plane.

Example: Let $P(x)=x^4-x^3-1$ be given. I want to plot on the $Oxy$ complex plane all four roots of this polynomial.

I suppose Tikz might be useful tool on this case, but I don't have experience on this package.

  • 1
    Welcome to TeX.se, ofcourse tikz would be useful. However, it would be great if you can show what you have tried so far in the form of a MWE. – Raaja Mar 19 at 15:25
  • @Raaja I haven't done any effort since I'm new in TeX. I need this for some other purpose, so any help is more than welcomed. Regards :) – Emin Mar 19 at 15:31
2

When you move to more technical mathematics you should use the sagetex package as it gives you access to an open source CAS called SAGE. The documentation on CTAN is here. Here is the "quick and dirty" way to get what you want.

\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
x = polygen(QQ)
f = x^4-x^3-1
root_list = f.roots(CC) 
real_roots = []
for root in root_list:
    real_roots += [root[0].n(digits=3)]
P = list_plot(real_roots,color='red',size=25)
\end{sagesilent}
The roots of the polynomial $\sage{f}$ plotted in the complex plane
\begin{center}
\sageplot[scale=.8]{P}
\end{center}
The roots of $\sage{f}$ are $\sage{real_roots}$.
\end{document}

Here is the output: enter image description here I don't know the intricacies of the code, I just hacked together some code by referring to this and this to figure out the code. I think x = polygen(QQ) will let you find roots of polynomials with rational coefficients and f.roots(CC) tells sage to find any complex roots. Since SAGE is a CAS those numbers could be objects such as sqrt(2) and we want to force them into decimals that can be plotted. That's accomplished by for root in root_list: real_roots += [root[0].n(digits=3)]. The actual plot is stored in a variable, P, by way of P = list_plot(real_roots,color='red',size=25) where color and size refer to the points which are initially too small to be seen easily. This is all done in sagesilent mode, which is like scrap paper that doesn't get into the document. In the LaTeX code, use \sage{} to get numbers/calculations and \sageplot{} to get the plots which are done in SAGE. Doing the plots through sage helps make the code short and since a CAS is doing the math, you can change the function (just remember you need multiplication between coefficients and variables) and SAGE will crunch out the result. You can, with a bit more coding, get the plot to be a nicer looking tikz plot, you can refer to how I did that for the Zeta function here. This will take quite a few extra lines. Notice that in my code, SAGE was also able to give you the 4 zeros by \sage{real_roots}. Having a CAS do the work prevents mistakes.

SAGE is not part of the LaTeX distribution; the best way to access it is through a free Cocalc account by clicking here.

  • Thanks mate! :) – Emin Mar 19 at 18:50
5

TikZ is not a computer algebra system. Of course you can compute the roots yourself and plot them using polar coordinates. (In principle you could even let TikZ solve the equations that determine the roots numerically, but this would arguably be a bit crazy.)

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{amsmath}
\DeclareMathOperator{\re}{Re}
\DeclareMathOperator{\im}{Im}
\begin{document}
\begin{tikzpicture}[scale=4]
 \draw[-latex] (-1.5,0) -- (1.75,0) node[below left] {$\re z$};
 \draw[-latex] (0,-1.5) -- (0,1.5) node[below right] {$\im z$};
 \draw (1,0.05) -- (1,-0.05) node[below]{1};
 \draw (0.05,1) -- (-0.05,1) node[left]{i};
 \foreach \X/\Y in {-76.5/0.94,76.5/0.94,180/0.82,0/1.38}
  {\fill (\X:\Y) circle[radius=1pt];}
\end{tikzpicture}
\end{document}

enter image description here

  • This was very helpful to me. Yet, can we use any other package (polynom for example) to determine the roots, and then use them to plot with the help of TikZ? – Emin Mar 19 at 15:56
  • 2
    @Emin This is what I did here. I kindly asked Mathematica to tell me the phases and radii of the roots (zsols = z /. N[Solve[z^4 - z^3 - 1 == 0, z]]; Map[{Arg[#]*180/\[Pi], Abs[#]} &, zsols] // InputForm) and plotted them in the loop. – marmot Mar 19 at 16:00
3

Next time, you should post a minimal working example to attract more users to your post. Anyway, you are a new user, so this answer is for welcoming you to TeX.SE!


First of all, I don't think it has more than two real roots.

You can plot it quite easily with TikZ:

\documentclass[tikz]{standalone}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[>=stealth,scale=2]
\draw[->] (0,-2.5)--(0,2.5) node[left] {$y$};
\draw[->,name path=ox] (-2.5,0)--(2.5,0) node[above]{$x$};
\draw (0,0) node[below left] {$O$};
\foreach \i in {-2,-1,1,2} {
    \draw (-.05,\i)--(.05,\i);
    \draw (0,\i) node[left] {$\i$};
    \draw (\i,-.05)--(\i,.05);
    \draw (\i,0) node[below] {$\i$};
}
\draw[red,name path=pl] plot[smooth,samples=500,domain=-1.1:1.6] (\x,{\x*\x*\x*\x-\x*\x*\x-1});
\path[name intersections={of=ox and pl,by={i1,i2}}];
\fill (i1) circle (1pt) node[above right] {$A$};
\fill (i2) circle (1pt) node[below right] {$B$};
\end{tikzpicture}
\end{document}

enter image description here

Now, when you have the intersections, you can have their coordinates:

\documentclass[tikz]{standalone}
\usetikzlibrary{intersections}
\newdimen\xa 
\newdimen\xb 
\newdimen\ya 
\newdimen\yb
\makeatletter
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother
% https://tex.stackexchange.com/a/239496/156344
\begin{document}
\begin{tikzpicture}[>=stealth,scale=2]
\draw[->] (0,-2.5)--(0,2.5) node[left] {$y$};
\draw[->,name path=ox] (-2.5,0)--(2.5,0) node[above]{$x$};
\draw (0,0) node[below left] {$O$};
\foreach \i in {-2,-1,1,2} {
    \draw (-.05,\i)--(.05,\i);
    \draw (0,\i) node[left] {$\i$};
    \draw (\i,-.05)--(\i,.05);
    \draw (\i,0) node[below] {$\i$};
}
\draw[red,name path=pl] plot[smooth,samples=500,domain=-1.1:1.6] (\x,{\x*\x*\x*\x-\x*\x*\x-1});
\path[name intersections={of=ox and pl,by={i1,i2}}];
\fill (i1) circle (1pt) node[above right] {$A$};
\path (i1); \pgfgetlastxy{\xa}{\ya}
\fill (i2) circle (1pt) node[below right] {$B$};
\path (i2); \pgfgetlastxy{\xb}{\yb} 
\draw (0,-3) node[text width=10cm,align=left] {%
There are two roots:\\
$A$ at $({\convertto{cm}{\xa}*2}, 0)$ and $B$ at $({\convertto{cm}{\xb}*2}, 0)$.};
\end{tikzpicture}
\end{document}

enter image description here

Of course you can always use \xa and \xb anywhere you want ;-)


As marmot said, TikZ is not a calculator. It can only help us find the real roots using intersections. And I don't think it is easy to do so with any LaTeX tools other than finding the roots yourself.

  • Thanks a lot for the effort, but I'm asking for something else. I'm not interested to the graph of the polynomial but only the (real and complex) roots of the polynomial. I suppose first there should be a code for finding the roots of any given polynomial and then plotting those points to the complex plane. – Emin Mar 19 at 15:37
  • @Emin I edited my answer. – JouleV Mar 19 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.