这是代码:
\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)*((3*ln(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}
主要问题是,当我重新编译代码时,它说不能取负数的自然对数,也不能除以 0,我不知道该如何改变我的等式来阻止这种情况的发生
答案1
我将其全部转换为不太原始的形式 - PGFPlots。使用此解决方案,您可以将轴移到外部,添加刻度标记,添加刻度、标签、图例等 - 如果您愿意的话。我还对函数进行了一些更改以处理奇点 - 这里10000
只是任何高数字来表示零处的无穷大。
\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}
答案2
\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}
这可能不是完美的解决方案,但希望它足够了。