我拼凑了一些我找到的代码来为我的班级绘制斜率场。它做得相当好,除了以下 DE 和其他 DE,特别是在这种情况下:结果输出清楚地显示了斜率为零的场线,而事实并非如此。
有什么建议吗?我附加了代码:
\documentclass[border=5pt,tikz]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{gnuplottex}
\begin{document}
\begin{tikzpicture}
[declare function={f(\x,\y)=(3*(\x^2)+2)/(2*\y);},scale=2.0]
%limits
\def\xmax{3} \def\xmin{-3}
\def\ymax{3} \def\ymin{-3}
\def\nx{11}
\def\ny{11}
%slope field
\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
\pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny}
\foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}{
\pgfmathsetmacro{\yprime}{f({\xmin+\i*\hx},{\ymin+\j*\hy})}
\draw[blue,shift={({\xmin+\i*\hx},{\ymin+\j*\hy})}]
(0,0)--($(0,0)!1mm!(.1,.1*\yprime)$);
}
%axes
\draw[->] (\xmin-.5,0)--(\xmax+.5,0) node[below right] {{$x$}};
\draw[->] (0,\ymin-.5)--(0,\ymax+.5) node[above left] {{$y$}};
\draw (current bounding box.north) node[above]
%label
{Slope field of \quad $\displaystyle \frac{dy}{dx}=\frac{3x^2+2}{2y}$.};
\end{tikzpicture}
\end{document}
答案1
当您更改 的值时,奇异线 y=0 会导致一些错误“尺寸太大” \ny
。为了克服这个问题,我们可以裁剪该奇异区域附近的区域,并相应地局部重新定义\ymax
或 的值\ymin
。只是为了突出显示奇异区域,我magenta
在 y=0 附近放置了不同的颜色( )。
此解决方案是手动完成的,可能不适用于复杂的奇异集,例如由隐式函数给出的奇异集。这种情况可能需要 的功率Asymptote
。
\documentclass[border=5mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
[declare function={f(\x,\y)=(3*\x*\x+2)/(2*\y);}]
% global limits
\def\xmax{3} \def\xmin{-3}
\def\ymax{3} \def\ymin{-3}
\def\nx{30} \def\ny{12}
% axes
\draw[->,gray] (\xmin-.5,0)--(\xmax+.5,0) node[below right,black] {$x$};
\draw[->,gray] (0,\ymin-.5)--(0,\ymax+.5) node[above left,black] {$y$};
% the above half-plane
\begin{scope}
\clip (\xmin-.2,.2) rectangle (\xmax+.2,\ymax+.2);
\def\ymin{.2} % locally redefined ymax (inside this scope only)
\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
\pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny}
\foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}
{
\pgfmathsetmacro{\yprime}{f({\xmin+\i*\hx},{\ymin+\j*\hy})}
\draw[blue,shift={({\xmin+\i*\hx},{\ymin+\j*\hy})}]
(0,0)--($(0,0)!1.5mm!(.1,.1*\yprime)$);
}
\end{scope}
% the below half-plane
\begin{scope}
\clip (\xmin-.2,-.2) rectangle (\xmax+.2,\ymin-.2);
\def\ymax{-.2} % locally redefined ymax (inside this scope only)
\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
\pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny}
\foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}
{
\pgfmathsetmacro{\yprime}{f({\xmin+\i*\hx},{\ymin+\j*\hy})}
\draw[blue,shift={({\xmin+\i*\hx},{\ymin+\j*\hy})}]
(0,0)--($(0,0)!1.5mm!(.1,.1*\yprime)$);
}
\end{scope}
% vicinity of singular line y=0
\begin{scope}
\clip (\xmin-.2,.2) rectangle (\xmax+.2,-.2);
\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
\foreach \i in {0,...,\nx}
\draw[magenta] (\xmin+\i*\hx,0)--+(-90:1mm)--+(90:1mm);
\end{scope}
\draw (current bounding box.north) node[above]
{The slope field of \quad $\displaystyle \frac{dy}{dx}=\frac{3x^2+2}{2y}$};
\end{tikzpicture}
\end{document}