8

I am trying to plot the elliptic curve secp256k1 y^2=x^3+7 in my latex-document.

\begin{center}
\begin{tikzpicture}[domain=-4:4, samples at ={-1.769292354238631, -1.76, -1.74, ..., 2.26, 2.35, 2.7, 2.9}]
    \draw[->] (-2.2,0) -- (3.2,0) node[right] {$x$};
    \draw[->] (0,-2.2) -- (0,4.2) node[above] {$y$};
    \draw[->, color=red] plot (\x,{sqrt(\x^3+7)}) node[right] {$y^2=x^3-2x+2$};
    \draw[->, color=red] plot (\x,{-sqrt(\x^3+7)}) node[right] {}
\end{tikzpicture}
\end{center}

but this gives me a curve that is interrupted at the left. And to be honest, I don't really know what all of these comments mean.

I would be happy if someone could help me, or has a good tutorial that explains how the plotting in Latex works!

Thanks in advance! And all the best.

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283

3 Answers3

10

Here is a solution that really uses PGFPlots. There you would get a very similar result as you did, when you wouldn't set the minimum x value (of the domain) manually.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.12,
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=-2,
            xmax=4,
            ymin=-7,
            ymax=7,
            xlabel={$x$},
            ylabel={$y$},
            scale only axis,
            axis lines=middle,
            % set the minimum value to the minimum x value
            % which in this case is $-\sqrt[3]{7}$
            domain=-1.912931:3,      % <-- works for pdfLaTeX and LuaLaTeX
%            domain=-1.91293118:3,   % <-- would also work for LuaLaTeX
            samples=200,
            smooth,
            % to avoid that the "plot node" is clipped (partially)
            clip=false,
            % use same unit vectors on the axis
            axis equal image=true,
        ]
            \addplot [red] {sqrt(x^3+7)}
                node[right] {$y^2=x^3+7$};
            \addplot [red] {-sqrt(x^3+7)};
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
  • 1
    I'm not sure the picture is right. – egreg Nov 26 '16 at 22:28
  • @egreg, I think you skepticism came from the scaling of the axis. I edited my answer to use the same unit vector on both axis. – Stefan Pinnow Nov 27 '16 at 07:16
  • It wasn't the plot of y^2 = x because it was clipped. – percusse Nov 27 '16 at 09:20
  • @StefanPinnow The equation of the curve was wrongly printed – egreg Nov 27 '16 at 09:21
  • @egreg, sorry, I think I don't understand your point. When you agree that the actual image is right, then also the "old" one was right, because the only (important) thing I changed was adding the option axis equal image=true. So when you comment that line or set the value to false you will get the "old" image (again) ... – Stefan Pinnow Nov 27 '16 at 09:27
  • The previous picture had just y^2=x, not y^2=x^3+7 – egreg Nov 27 '16 at 09:35
  • @egreg, oh sorry. Seems I didn't get the sentence right. Of course just the node was clipped partially ... Sorry again for the confusion. – Stefan Pinnow Nov 27 '16 at 09:38
  • when I try to use this I still get a weird curve that is open at the left side... the documentclass "standalone" did not work for me. Could that be the reason? (All the rest I copied from the above). – Luca Nov 27 '16 at 18:06
  • @Luca, I think this is a (math) precision issue. I assume you have compiled using pdfLaTeX, right? (I used LuaLaTeX.) If so, delete some figures from the lower bound value of domain until the curve is "closed" again. – Stefan Pinnow Nov 27 '16 at 18:17
  • Thank you @StefanPinnow ! But what does this mean? What does domain do? – Luca Nov 29 '16 at 00:49
  • From the PGFPlots manual you will see that domain specifies the x range in which the given expression/formula is evaluated (using the number of samples given). That is, when you state \addplot [mark=*,domain=0:2,samples=3] {1}; you will get points at x values 0, 1 and 2. – Stefan Pinnow Nov 29 '16 at 07:42
  • It does not work well for $y^2 = x^3 - 3x + 1$, for instance. – Watson Feb 07 '23 at 10:06
4

If you have no objection using pstricks, it's very simple to plot:

\documentclass[11pt,x11names, border=3pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}

\usepackage{pst-plot}
\usepackage{auto-pst-pdf}

\def\f{sqrt(x^3 + 7)}

\begin{document}

\psset{plotpoints=200, plotstyle=curve, algebraic, arrowinset=0.12}%
\begin{pspicture*}(-5.8,-6.5)(6,7)
    \psaxes[linecolor=LightSteelBlue3, tickcolor=LightSteelBlue3, ticksize=-2pt 2pt, labels =none, arrows=->, ](0,0)(-5.8,-6.5)(6,7)[$x$, -120][$y$, -135]
    \uput[dl](0,0){$ O $}\uput[dl](-1.913,0){ $ -\sqrt[3]{7} $}
    \uput[dl](0,-2.65 ){$ -\sqrt{7}$} \uput[ul](0,2.65 ){$ \sqrt{7}$}
    \psset{linewidth=1.5pt, linecolor=IndianRed3}
    \psplot{-1.91293}{5}{\f}
    \psplot{-1.91293}{5}{-\f}
\end{pspicture*}

\end{document}]

enter image description here

Note: This code is compilable with pdflatex if you launch it with the -shell-escape switch (under TeX Live or MacTeX), or --enable-write18 (MiKTeX). Alternatively, remove the auto-pst-pdf package, and compile with xelatex.

Bernard
  • 271,350
1

Plotted as y^2-x^3-7=0. Run with xelatex

\documentclass[11pt,x11names, border=3pt]{standalone}
\usepackage{pst-func}
\begin{document}

\begin{pspicture*}(-3,-6.5)(6,7)
\psaxes[labels=none,arrows=->,linecolor=black!50](0,0)(-3,-6.5)(6,7)[$x$, -120][$y$,-135]
\uput[dl](0,0){$ O $}\uput[dl](-1.913,0){ $ -\sqrt[3]{7} $}
\uput[dl](0,-2.65 ){$ -\sqrt{7}$} \uput[ul](0,2.65 ){$ \sqrt{7}$}
\psplotImp[linewidth=2pt,linecolor=red!60,algebraic](-4,-8)(6,8){y^2 - x^3 - 7}
\end{pspicture*}

\end{document}

enter image description here