2

I am adapting the solution here to plot the slope field for a differential equation. The equation I would like to use is dy/dt = -sqrt(1-y^2). I can get it working for "simple" functions (see MWE) but not for this function that I want. You can see the various functions I have tried (commented out) in the MWE. For those cases that fail, I see

! Missing number, treated as zero.
<to be read again> 
                   n
l.42   \end{axis}

I suspect the answer here regarding curly braces is related, but I have not managed to get this working for my problem.

MWE:

\documentclass{article}                                                                                                                                                                               
\usepackage{pgfplots}                                                                                                                                                                                 
\pgfplotsset{compat=1.8}                                                                                                                                                                              
\pgfplotsset{                                                                                                                                                                                         
  SlopeFieldAxis/.style={                                                                                                                                                                             
    axis equal image,
    view={0}{90},                                                                                    
    samples=11,
  }                                                                                                                                                                                                   
}   
\begin{document}                                                                                                                                                                                  
\begin{tikzpicture}[                                                                                                                                                                              
    declare function={                                                                                                                                                                            
      f1(\x,\y) = 1.0 - \y*\y;                                                                                                                                                                    
      f2(\x,\y) = sqrt(\y);                                                                                                                                                                       
      f3(\x,\y) = -sqrt(f1(\x,\y));                                                                                                                                                                
      f4(\x,\y) = -sqrt(1.0 - \y*\y);                                                                                                                                                             
    }                                                                                                                                                                                             
  ]                                                                                                                                                                                               
  \begin{axis}[                                                                                                                                                                                   
      SlopeFieldAxis,                                                                                                                                                                             
      width=0.95\textwidth,                                                                                                                                                                       
      xmin=0, xmax=3.14159,                                                                                                                                                                       
      ymin=0, ymax=2.0,                                                                                                                                                                           
      zmin=0, zmax=1.0,                                                                                                                                                                           
      domain=0:3.14159, y domain=0:2,                                                                                                                                                             
    ]                                                                                                                                                                                             
    \addplot3[                                                                                                                                                                                    
      blue,                                                                                                                                                                                       
      quiver={                                                                                                                                                                                    
        u={1},                                                                                                                                                                                    
        % v={f1(x,y)}, % works                                                                                                                                                                      
        % v={f2(x,y)}, % works                                                                                                                                                                    
        v={f3(x,y)}, % error                                                                                                                                                                    
        % v={f4(x,y)}, % error                                                                                                                                                                    
        scale arrows=0.075,                                                                                                                                                                       
        every arrow/.append style={-latex},                                                                                                                                                       
      }                                                                                                                                                                                           
    ] (x,y,0);                                                                                                                                                                                    
  \end{axis}                                                                                                                                                                                      
\end{tikzpicture}

\end{document}

Clay
  • 131
  • What should do function f3(\x,\y) = -sqrt(f(\x,\y));. Itis not defined ... – Zarko Oct 09 '20 at 05:16
  • Specifically, the function f used in f3 is not defined. – Torbjørn T. Oct 09 '20 at 11:53
  • Sorry! I must have copied the MWE before I corrected that typo. Should make more sense now. – Clay Oct 09 '20 at 14:31
  • Well, square roots of negative numbers is a bit too complex it seems, try with y domain=0:0.5. – Torbjørn T. Oct 09 '20 at 14:38
  • @TorbjørnT. Wow. I am embarrassed for myself. I was so convinced that this was a tikz-pgf issue that I did not bother to check for the obvious. Thank you for your patience! – Clay Oct 09 '20 at 14:52

0 Answers0