2

Since pgfplots doesn't have a "built in" cube root function, I adapted code from this question in order to plot a cube root function in a differential equation slope field. The trick seems to display perfectly:

enter image description here

However, although compiling and looking perfectly as desired, my Overleaf project is returning a "missing number, treated as zero" error. Here is the 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/2x^2-5)/abs(3/2x^2-5)abs(3/2x^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}

I found a similar question experienced by another person - however the solution (restricting y domain) doesn't seem to help in this situation.

Thank you very much for your thoughts.

EDIT: One comment suggested removing the \\ from line 22 of the code. Doing so removes the error, but causes the slope field to display improperly:

enter image description here

EthanAlvaree
  • 1,367
  • The error comes from the \\ in line 22 of your code. –  Mar 22 '21 at 11:43
  • Hi @user237902 thank you for your comment. I tried removing the \ \ which does stop the error, however it causes the slope field to display improperly. Please see edit to original post. – EthanAlvaree Mar 22 '21 at 15:13

1 Answers1

1

There are many issues. One of them is that you divide by zero.

\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/2x^2-5)/abs(3/2x^2-5)abs(3/2x^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}

  • This worked, thanks!

    Can you explain the \x/(\y+0.01)^2 bit? I assume we are adding 0.01 to avoid division by zero. But since y domain = -5:5, wouldn't this still result in division by zero when y= -0.01?

    – EthanAlvaree Mar 22 '21 at 19:16
  • @Mathemanic Yes, but in your case y is never 0.01 because the step size is not that small, or there not enough samples. If you had many more samples, you would need to add a different cure. –  Mar 22 '21 at 19:34