1

this is the code:

\documentclass{article}
\usepackage[utf8]{inputenc}    
\usepackage[margin=1in]{geometry}   
\usepackage{amsfonts, amsmath, amssymb}   
\usepackage{float}  
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{calc}

\begin{document}

%========== SLOPE FIELD GRAPH ==============

\begin{center}

\begin{tikzpicture}[scale=0.7, declare function={f(\x,\y)=(\x^3+\y^3)/(\x*\y^2);}] \def\xmax{5} \def\xmin{-5}
\def\ymax{5} \def\ymin{-5}
\def\nx{10} \def\ny{10}

%============ SLOPE MARKS ================

\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx} \pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny} \foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}{
\ifnum\i=5
\else \pgfmathsetmacro{\yprime}{f({\xmin+\i\hx},{\ymin+\j\hy})} \draw[blue,thick, shift={({\xmin+\i\hx},{\ymin+\j\hy})}]
(0,0)--($(0,0)!4mm!(.1,.1*\yprime)$);
\fi
}

%========== SOLUTION CURVE ===============

\begin{scope}
\clip (\xmin,\ymin-0.5) rectangle (\xmax,\ymax); \def\yo{0.25926}
\draw[red, thick, samples=100] plot[domain=-5:5, yrange=-5:5] (\x,{ (\x)((3ln(abs(\x))-1-3*ln(4))^(0.333)) });
\end{scope}

%========== INITIAL CONDITION ==============

\draw[red] (4,-4) circle[radius=3pt] node[fill=white, below left] {$(4,-4)$};
\fill[red] (4,-4) circle[radius=3pt]; \draw[blue] (4.20,-3.9885) circle[radius=3pt];
\fill[blue] (4.20,-3.9885) circle[radius=3pt];

%=============== LABELS =================

\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] {Slope field of $y'=\frac{x^3+y^3}{xy^2}$.}; \end{tikzpicture} \end{center}

\end{document}

the main problem is that when I recompile my code it says that it can't take the natural log of a negative number or it can't divide by 0 and I'm not sure what to change in my equation to stop that from happening

Mico
  • 506,678
  • 1
    Please, extend your code sniped to complete small document which we can compile as it is. BTW, your problem has nothing with Overleaf nor with LaTeX, but seems to be immanent mathematical. Try to make a sketch what you after. And, welcome to TeX.SE! – Zarko Nov 22 '21 at 21:14
  • Thank you for welcoming me and telling me that my error is something to do with immanent mathematical, but I'm not sure what that means. I have also changed my code so people can try to open what I was trying to do. BTW i don't understand what your last sentence was trying to mean. – Matthew Fell Nov 22 '21 at 21:36
  • 2
    When \x=0, aren't you telling TeX to evaluate ln(0)? You may know that \x*ln(\x) should be 0 in that case, but TeX doesn't. – Teepeemm Nov 22 '21 at 21:54
  • It's a good question, indicating the some difficulties: the appearance zeros in the numerator of the vector field; and the inconvenience to use analytic formula for a solution curve. Have a start here https://tex.stackexchange.com/questions/471741/draw-solution-curves-of-a-differential-equation-with-tikz and here https://tex.stackexchange.com/questions/139114/differential-equation-direction-plot-with-pgfplots/ – Black Mild Aug 21 '22 at 12:22
  • Is that differential equation just a random exercise, or has some application? what is the context or textbook of the differential equation? – Black Mild Aug 21 '22 at 14:39

2 Answers2

2

I converted it all to a less primitive form - PGFPlots. With this solution, you can move the axis to the outside, add tick marks, add scale, labels, legend, ... - if you so wish. I also made some changes to the function to take care of the singularities - here 10000 is just any high number to represent infinity at zero.

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[
declare function={
f(\x,\y)=\x==0?10000:\y==0?10000:(\x*\x*\x+\y*\y*\y)/(\x*\y*\y);
cuberoot(\x)=\x<0?-exp(ln(-\x)/3):exp(ln(\x)/3);
}] 
\begin{axis}[   
view={0}{90},
xmin=-10, xmax=10,
ymin=-10, ymax=10,
axis lines=center,
enlarge x limits=0.05,
enlarge y limits=0.05,
ticks=none,
]
\newcommand{\length}{sqrt(1+f(x,y)^2)}
\addplot3[
blue, thick,
domain=-10:10, y domain=-10:10,
samples=11,
quiver={
  u={1/\length},
  v={f(x,y)/\length},
  scale arrows=0.5,
}] {0};
\addplot[red, thick, domain=-10:10, samples=400, smooth] (x, {x*cuberoot(3*ln(abs(x)/4)-1)} ); 
\end{axis}
\end{tikzpicture}
\end{document}

Quiver plot with red curvy function

1
\documentclass[10pt]{article}
\usepackage{amsmath, amssymb, amsfonts}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

%========== SLOPE FIELD GRAPH ==============
\begin{center}
\begin{tikzpicture}[scale=0.4, declare function={f(\x,\y)=
((\x)*(\x)*(\x)+(\y)*(\y)*(\y))/((\x)*(\y)*(\y))
;}] %change scale to make the graph larger or smaller; change slope equation
\def\xmax{10.5} \def\xmin{-9.5} %change domain of grid
\def\ymax{10.5} \def\ymin{-9.5} %change range of grid
\def\nx{10} %change number of horizontal subintervals
\def\ny{10} %change number of vertical subintervals

%============ SLOPE MARKS ================
\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,thick, shift={({\xmin+\i*\hx},{\ymin+\j*\hy})}] 
(0,0)--($(0,0)!5mm!(.1,.1*\yprime)$);
} %change 4mm to shorten or lengthen tangent lines


%========== SOLUTION CURVE ===============
\begin{scope}
\clip (\xmin,\ymin-0.5) rectangle (\xmax,\ymax);
\def\yo{-5.159}
\draw[red, thick, samples=100] plot[domain=0.1:5.583, yrange=\ymin:\ymax] (\x,{(\x)*(-(abs((\yo)+3*ln(\x)))^(1/3))}); 
\draw[red, thick, samples=100] plot[domain=5.583:10, yrange=\ymin:\ymax] (\x,{(\x)*((abs((\yo)+3*ln(\x)))^(1/3))}); 
\draw[red, thick, samples=100] plot[domain=-10:-5.583, yrange=\ymin:\ymax] (\x,{(\x)*((abs((\yo)+3*ln(-(\x))))^(1/3))}); 
\draw[red, thick, samples=100] plot[domain=-5.583:-0.1, yrange=\ymin:\ymax] (\x,{(\x)*(-(abs((\yo)+3*ln(-(\x))))^(1/3))}); 
\end{scope}

%=============== LABELS =================
\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]
{Slope field of $y'=\dfrac{x^3+y^3}{xy^2}$.}; %change label of differential equation

\end{tikzpicture}
\end{center}

\end{document}

This may not be the perfect solution, but hopefully it will suffice.

  • 1
    Please show not only some code, explain what you changed ... – Mensch Nov 23 '21 at 23:13
  • Apologies! In the SLOPE FIELD GRAPH section, I changed the domain and range from -10:10 to -9.5:10.5 to avoid the division by zero error (neither x nor y will equal zero in this case). In the SLOPE MARKS section, I removed the if...else lines (no longer necessary). In the SOLUTION CURVE section, I took the absolute value of the expression inside the cube root to avoid the log of a negative number error. I split the domain at the three zeros of the function, multiplying the first and last pieces by (-1) in order to get a negative cube root. – MadMatrix Nov 25 '21 at 13:22
  • I got the idea from a comment made by @Bernard in this post – MadMatrix Nov 25 '21 at 13:28