我正在尝试绘制一条椭圆曲线。
曲线不知何故在点 (-2.1,0) 的近距离范围内中断。这就是为什么在此点处有一个小间隙,并且两个函数没有连接,这看起来不太好看。
是否有人已经遇到过类似的问题或知道如何解决这个问题?
这是我的代码:
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{axis}[
xmin=-3,
xmax=4.5,
ymin=-7,
ymax=7,
xlabel={$x$},
ylabel={$y$},
scale only axis,
axis lines=middle,
domain=-3:3.45,
samples=200,
smooth,
clip=false,
axis equal image=true,
]
\addplot [blue] {sqrt(x^3-3*x+3)};
\addplot [blue] {-sqrt(x^3-3*x+3)};
\end{axis}
\end{tikzpicture}
\end{figure}
答案1
这里有一个建议,可以一次性绘制出图表。代码中有说明,因为不使用 LaTeX 添加公式很麻烦。
\documentclass[fleqn]{article}
\usepackage{amsmath}
\DeclareMathOperator{\sign}{sign}
\usepackage{dsfont}
\usepackage[margin=2cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16,LaTeXNewbie/.style={xmin=-3,
xmax=4.5,
ymin=-7,
ymax=7,
xlabel={$x$},
ylabel={$y$},
scale only axis,
axis lines=middle,
samples=200,
smooth,
clip=false,
axis equal image=true,}
}
\begin{document}
In order to plot the points $(x,y)\in\mathds{R}^2$ that satisfy
\begin{equation}
y^2=x^3+a\,x+b\;,
\end{equation}
let us first find the $x$ for which $y=0$. This is a cubic equation with the
solution
\begin{equation}\label{eq:x0}% https://tex.stackexchange.com/a/533003/194703
x_0=\frac{\sqrt[3]{2} \left(\sqrt{12 a^3+81 b^2}-9
b\right)^{2/3}-2 \sqrt[3]{3} a}{6^{2/3}
\sqrt[3]{\sqrt{12 a^3+81 b^2}-9 b}}\;.
\end{equation}
So one ``brute force'' solution is to use $x_\mathrm{min}$ to define the plot
interval (see Figure~\ref{fig:elliptic}).
\begin{figure}[htb]
\centering
\begin{tikzpicture}[declare function={xnod(\a,\b)=0.001+%
(-2*pow(3,1/3)*\a + pow(2,1/3)*%
pow(abs(-9*\b + sqrt(12*pow(\a,3) + 81*pow(\b,2))),2/3))/%
(pow(6,2/3)*sign(-9*\b + sqrt(12*pow(\a,3) + 81*pow(\b,2)))*%
pow(abs(-9*\b + sqrt(12*pow(\a,3) + 81*pow(\b,2))),1/3));
ysol(\x,\a,\b)=sqrt((\x*\x*\x+\a*\x+\b));}]
\begin{axis}[LaTeXNewbie,
domain={xnod(-3,3)}:3.45,
]
\addplot [blue] {sqrt(x^3-3*x+3)};
\addplot [blue] {-sqrt(x^3-3*x+3)};
\end{axis}
\end{tikzpicture}
\caption{``Brute force'' solution.}
\label{fig:ellipticbf}
\end{figure}
\clearpage
An arguably more elegant solution goes as follows. Define
\begin{align}
x_\mathrm{tamed}(x)&=x_0+|x|\;,\\
y_\mathrm{sol}(x)&=\sign(x)\,
\sqrt{x_\mathrm{tamed}^3+a\,x_\mathrm{tamed}+b}\;.
\end{align}
with $x_0$ from \eqref{eq:x0}. Then we need only to do the parametric plot of
\begin{equation}
\gamma(t)=\bigl(x_\mathrm{tamed}(t),y_\mathrm{sol}(t)\bigr)\;,
\end{equation}
where now the plot domain determines how far the lower and upper branches,
respectively, extend right from $x_0$. That is, \texttt{domain=-5:5} in the plot
in Figure~\ref{fig:elliptic}) means both the upper and lower branch extend from
$x_0$ to $x_0+5$. In particular, we need only one \verb|\addplot| command.
\begin{figure}[htb]
\centering
\begin{tikzpicture}[declare function={xnod(\a,\b)=0.001+%
(-2*pow(3,1/3)*\a + pow(2,1/3)*%
pow(abs(-9*\b + sqrt(12*pow(\a,3) + 81*pow(\b,2))),2/3))/%
(pow(6,2/3)*sign(-9*\b + sqrt(12*pow(\a,3) + 81*pow(\b,2)))*%
pow(abs(-9*\b + sqrt(12*pow(\a,3) + 81*pow(\b,2))),1/3));
ysol(\x,\a,\b)=sign(\x)*sqrt((pow(xtamed(\x,\a,\b),3)+\a*xtamed(\x,\a,\b)+\b));
xtamed(\x,\a,\b)=xnod(\a,\b)+abs(\x);
a=-3;b=3;}]
\begin{axis}[LaTeXNewbie,
domain=-5:5,%<-different meaning than usual, see text
]
\addplot [blue] ({xtamed(x,a,b)},{ysol(x,a,b)});
\end{axis}
\end{tikzpicture}
\caption{Arguably more elegant solution.}
\label{fig:elliptic}
\end{figure}
\end{document}