我要努力适应代码在这里复制下图。
虽然我可以绘制轴和标签,但无法绘制曲线。我不明白代码到底是干什么的。有人能帮我吗?这是我目前所得到的。
\documentclass[12pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
%draw axes and label
\draw[->] (-4.5, 0) -- (4.5, 0) node[below] {$x$};
\draw[->] (0, -2.5) -- (0, 2.5) node[left] {$y$};
\foreach \i in {-4, -3 , ..., -1, 1, 2, ..., 4}
{
\draw (\i, 0) node[above]{$\i$};
}
\foreach \m in {-2, -1 , 1,2}
{
\draw (0, \m) node[left]{$\m$};
}
\draw (-0.2, 0) node[above]{$0$};
\draw[dashed] (-2, 0) -- (-2, -1) -- (2, -1) -- (2, 0);
\draw[fill=black] (-2, -1) circle(2pt);
\draw[fill=black] (2, -1) circle(2pt);
\draw[fill=black] (-1,0) circle(2pt);
\draw[fill=black] (1,0) circle(2pt);
draw[fill=black] (0, 2) circle(2pt);
\draw[thick, smooth] plot coordinates
{
(-4, -0.1)
(-2, -1)
(-1, 0)
(0, 2)
(1, 0)
(2, -1)
(4, -0.1)
};
\end{tikzpicture}
\end{document}
我只画了轴和标签。我不知道如何处理曲线。
更新:我能够(稍微)画出曲线。它并不完美,但我现在会使用它。如果有人有更好的想法,请分享!
答案1
PSTricks 解决方案仅用于娱乐或比较目的。
\documentclass[pstricks,border=\dimexpr355pt/113\relax]{standalone}
\usepackage{pst-plot}
\begin{document}
\begin{pspicture}[algebraic,plotpoints=500](-5,-1.5)(5.5,3)
\psaxes{->}(0,0)(-5,-1.5)(5,2.5)[$x$,0][$y$,90]
\psplot[linecolor=red]{-4.5}{4.5}{-512*(x^2-1)/(27*x^6-168*x^4+560*x^2+256)}
\end{pspicture}
\end{document}
其余的琐碎装饰则故意留下来作为你的练习。
答案2
这不是一个确切的答案,而是一个如何完成的解释
简单来说,答案是——https://stackoverflow.com/questions/59880248/precise-and-smooth-curve-with-tikz?fbclid=IwAR09VVJs9oYgp96-kg-Iq1EEfa5rBnm30zHFQp_YXcZJg4sTT0rzk7X20hI-- 推入额外的坐标/点以进行精细control
在你的答案中实现同样的效果将导致
\draw[blue,line width=2pt,smooth]
{(-4,-0.1)
(-4,-0.1) ..controls(-3.6,-0.1) and (-2.9,-0.3)..
(-2.8,-0.5) ..controls(-2.5,-.9) and (-2,-1) ..
(-2,-1)
(2, -1) ..controls(2.5,-0.9) and (2.8,-0.5) ..
(2.9,-0.3) ..controls(3.5,-0.1) and (4,-0.1) ..
(4,-0.1)
};
控制代码具有固定的格式/语法
{start point ..controls() and()..
next point ..controls() and()..
end point};
推入这些额外的点会导致修改后的曲线如下
为了更清楚地说明变化,我注释掉了主要情节点,如下所示
删除不再需要的左侧和右侧点后,我们得到最终的曲线
平均能量损失
\documentclass[12pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
%draw axes and label
\draw[->] (-4.5, 0) -- (4.5, 0) node[below] {$x$};
\draw[->] (0, -2.5) -- (0, 2.5) node[left] {$y$};
\foreach \i in {-4, -3 , ..., -1, 1, 2, ..., 4}
{
\draw (\i, 0) node[above]{$\i$};
}
\foreach \m in {-2, -1 , 1,2}
{
\draw (0, \m) node[left]{$\m$};
}
\draw (-0.2, 0) node[above]{$0$};
\draw[dashed] (-2, 0) -- (-2, -1) -- (2, -1) -- (2, 0);
\draw[fill=black] (-2, -1) circle(2pt);
\draw[fill=black] (2, -1) circle(2pt);
\draw[fill=black] (-1,0) circle(2pt);
\draw[fill=black] (1,0) circle(2pt);
draw[fill=black] (0, 2) circle(2pt);
\draw[blue,line width=2pt, smooth] plot coordinates
{
% (-4, -0.1)
(-2, -1)
(-1, 0)
(0, 2)
(1, 0)
(2, -1)
% (4, -0.1)
};
\draw[blue,line width=2pt,smooth]
{(-4,-0.1)
(-4,-0.1) ..controls(-3.6,-0.1) and (-2.9,-0.3)..
(-2.8,-0.5) ..controls(-2.5,-.9) and (-2,-1) ..
(-2,-1)
(2, -1) ..controls(2.5,-0.9) and (2.8,-0.5) ..
(2.9,-0.3) ..controls(3.5,-0.1) and (4,-0.1) ..
(4,-0.1)
};
\end{tikzpicture}
\end{document}