具有立方根的微分方程的斜率场:“缺失数字,视为零”

具有立方根的微分方程的斜率场:“缺失数字,视为零”

自从pgfplots没有“内置”立方根函数,我改编了代码这个问题为了在微分方程斜率场中绘制立方根函数。这个技巧似乎完美地展现了出来:

在此处输入图片描述

但是,尽管编译和外观都符合预期,我的 Overleaf 项目返回“缺失数字,视为零”错误。以下是 MWE:

\documentclass{exam}
\printanswers
\usepackage{pgf,tikz}
\usepackage{pgfplots}
\usepackage{physics}
%Slope fields, from https://tex.stackexchange.com/questions/139064/how-to-draw-slope-fields-with-all-the-possible-solution-curves-in-latex
\pgfplotsset{ODE/.style={
    axis x line = middle,
    axis y line = middle,
    xlabel={$x$}, ylabel={$y$},
    axis equal image, % Unit vectors for both axes have the same length
    view={0}{90}, % We need to use "3D" plots, but we set the view so we look at them from straight up
    samples=21, % How many arrows?
    cycle list={    % Plot styles
            blue,
            quiver={
                u={1/\length}, v={f(x)/\length}, % End points of the arrows
                    scale arrows=0.3,
                    every arrow/.append style={
%                        -latex % Arrow tip, uncomment to get "arrows" instead of line segments
                },
            }\\
        }
    }
}

\begin{document}
\begin{questions}
\question[10]
Consider the differential equation
\begin{equation*}
\dv{y}{x}(x,y)=\frac{x}{y^2},
\end{equation*}

\begin{parts}
\part Sketch the particular solution to the differential equation satisfying the initial condition $y(2)=1$.

\begin{center}
\def\length{sqrt(1+(x/y^2)^2)}
\begin{tikzpicture}[declare function={f(\x) = \x/\y^2;}]% Define which function we're using
\begin{axis}[ODE,
width=0.5\textwidth,
xmin=-5, xmax=5,
ymin=-5, ymax=5,
domain=-5:5, y domain=-5:5,
xtick={-5,-4,...,5}, ytick={-5,-4,...,5}
]
\addplot3 (x,y,0);
\addplot[red, samples=50, smooth, thick, no markers, domain=-5:5] {(3/2*x^2-5)/abs(3/2*x^2-5)*abs(3/2*x^2-5)^(1/3)}; %https://tex.stackexchange.com/questions/69411/pgfplots-cant-plot-some-usual-mathematical-functions
\end{axis}
\end{tikzpicture}
\end{center}
\vspace*{\stretch{1}}

\part Solve for the particular solution to the differential equation satisfying the initial condition $y(2)=1$.
\begin{solution}
Separating variables,
\begin{align*}
\dv{y}{x}&=\frac{x}{y^2} \\
\int y^2 \dd{y} &= \int x\dd{x} \\
\frac{1}{3}y^3 &= \frac{1}{2} x^2 + C
\end{align*}
Substituting our initial condition,
\begin{align*}
\frac{1}{3} &= 2 + C
\end{align*}
so $C=-\frac{5}{3}$, which implies
\begin{align*}
\frac{1}{3}y^3 &= \frac{1}{2} x^2 - \frac{5}{3} \\
y^3 &= \frac{3}{2} x^2 -5 \\
y &= \sqrt[3]{\frac{3}{2} x^2 -5}
\end{align*}
\end{solution}
\vspace*{\stretch{1}}
\end{parts}
\end{questions}
\end{document}

我发现了一个类似问题另一个人经历过 - 然而解决方案(限制 y 域)似乎在这种情况下没有帮助。

非常感谢您的想法。

编辑:一条评论建议\\从代码的第 22 行删除。这样做可以消除错误,但会导致斜率字段显示不正确:

在此处输入图片描述

答案1

有很多问题。其中之一就是除以零。

\documentclass{exam}
\printanswers
\usepackage{pgf,tikz}
\usepackage{pgfplots}
\usepackage{physics}
%Slope fields, from https://tex.stackexchange.com/questions/139064/how-to-draw-slope-fields-with-all-the-possible-solution-curves-in-latex
\pgfplotsset{ODE/.style={
    axis x line = middle,
    axis y line = middle,
    xlabel={$x$}, ylabel={$y$},
    axis equal image, % Unit vectors for both axes have the same length
    view={0}{90}, % We need to use "3D" plots, but we set the view so we look at them from straight up
    samples=21, % How many arrows?
    cycle list={    % Plot styles
            blue,
            quiver={
                u={1/\length}, v={f(x,y)/\length}, % End points of the arrows
                    scale arrows=0.3,
                    every arrow/.append style={
%                        -latex % Arrow tip, uncomment to get "arrows" instead of line segments
                },
            }\\
        }
    }
}

\begin{document}
\begin{questions}
\question[10]
Consider the differential equation
\begin{equation*}
\dv{y}{x}(x,y)=\frac{x}{y^2},
\end{equation*}

\begin{parts}
\part Sketch the particular solution to the differential equation satisfying the initial condition $y(2)=1$.

\begin{center}
\def\length{sqrt(1+(x/y^2)^2)}
\begin{tikzpicture}[declare function={f(\x,\y) = \x/(\y+0.01)^2;}]% Define which function we're using
\begin{axis}[ODE,
width=0.5\textwidth,
xmin=-5, xmax=5,
ymin=-5, ymax=5,
domain=-5:5, y domain=-5:5,
xtick={-5,-4,...,5}, ytick={-5,-4,...,5}
]
\addplot3 (x,y,0);
\addplot[red, samples=50, smooth, thick, no markers, domain=-5:5] 
    {(3/2*x^2-5)/abs(3/2*x^2-5)*abs(3/2*x^2-5)^(1/3)}; %https://tex.stackexchange.com/questions/69411/pgfplots-cant-plot-some-usual-mathematical-functions
\end{axis}
\end{tikzpicture}
\end{center}
\vspace*{\stretch{1}}

\part Solve for the particular solution to the differential equation satisfying the initial condition $y(2)=1$.
\begin{solution}
Separating variables,
\begin{align*}
\dv{y}{x}&=\frac{x}{y^2} \\
\int y^2 \dd{y} &= \int x\dd{x} \\
\frac{1}{3}y^3 &= \frac{1}{2} x^2 + C
\end{align*}
Substituting our initial condition,
\begin{align*}
\frac{1}{3} &= 2 + C
\end{align*}
so $C=-\frac{5}{3}$, which implies
\begin{align*}
\frac{1}{3}y^3 &= \frac{1}{2} x^2 - \frac{5}{3} \\
y^3 &= \frac{3}{2} x^2 -5 \\
y &= \sqrt[3]{\frac{3}{2} x^2 -5}
\end{align*}
\end{solution}
\vspace*{\stretch{1}}
\end{parts}
\end{questions}
\end{document}

相关内容