绘制幂函数,如 x^-2、x^-3 等

绘制幂函数,如 x^-2、x^-3 等

我一直在 Exchange 和网上寻找如何绘制类似 的函数1/x^3。我在维度中收到错误。我正在使用\addplottikz 下的命令。我尝试了不同的域,但无济于事。任何帮助都将不胜感激。

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{subcaption}
\usetikzlibrary{arrows.meta}
\pgfplotsset{compat=1.17}
\pgfplotsset{every x tick label/.append style={font=\footnotesize, yshift=0.6ex}}
\pgfplotsset{every y tick label/.append style={font=\footnotesize, xshift=0.5ex}}

\begin{document}

\subsubsection*{Negative Integer Powers}

The graphs of power functions that have negative integer exponents have \emph{two} distinct shapes (see Figures 3 and 4 below).

\begin{minipage}[h]{3in}
\begin{tikzpicture}
        \begin{axis}[
grid style={blue!50},
axis x line = center,
axis y line = center,
xmin = -5,   xmax = 5,
ymin = -5,  ymax = 5,
xtick = {-5,-4,...,5},
ytick = {-5,-4,...,5},
grid = both,
]
\addplot[samples=300,domain=-3:3,ultra thick,color=blue]{1/(x^2)};
\end{axis}
\end{tikzpicture}
\captionof{figure}{$y=x^n$ $\bigcup$-shaped}
\end{minipage}
\qquad
\begin{minipage}[h]{3in}
\begin{tikzpicture}
        \begin{axis}[
grid style={blue!50},
axis x line = center,
axis y line = center,
xmin = -5,   xmax = 5,
ymin = -5,  ymax = 5,
xtick = {-5,-4,...,5},
ytick = {-5,-4,...,5},
grid = both,
]
\addplot[samples=300,domain=-3:-0.1,ultra thick,color=blue]{1/(x^3)};
\addplot[samples=300,domain=0.1:3,ultra thick,color=blue]{1/(x^3)};
\end{axis}

\end{tikzpicture}
\captionof{figure}{Graph of $y=x^{-3}$ or $y=1/x^3$}
\end{minipage}

\end{document}

它起作用了,但是尺寸和数字不一致。

缺口

答案1

使用包。使用以下命令可避免pgfplots此错误:dimension too largerestrict y to domain

\documentclass[12pt]{article}
\usepackage{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage[skip=1ex, font=small, labelfont=bf]{caption}

\begin{document}
    \begin{figure}[ht]
\pgfplotsset{
    axis lines=center, 
    grid,
    grid style={very thin, blue!50},
    xmin=-5,    xmax=5,         xtick={-5,-4,...,5},
    ymin=-5,    ymax=5,         ytick={-5,-4,...,5},
    restrict y to domain=-10:10, % <-------
    ticklabel style={font=\scriptsize, fill=white, inner sep=2pt},
    domain=-4:4, samples=100,
    no marks, 
    every axis plot post/.append style={ultra thick, semitransparent, color=magenta,},
            }
\begin{minipage}[t]{0.5\linewidth}
\centering
    \begin{tikzpicture}
\begin{axis}[declare function = {f(\x)=1/((\x)^2);}] % or f(\x)=1/(\x*\x);
\addplot    {f(x)};
\end{axis}
    \end{tikzpicture}
\caption{Graph of $y=x^{-2}$ or $y=1/x^2$}
\end{minipage}\begin{minipage}[t]{0.5\linewidth}
\centering
    \begin{tikzpicture}
\begin{axis}[declare function = {f(\x)=1/((\x)^3);}] % or f(\x)=1/(\x*\x*\x);
\addplot    {f(x)};
\end{axis}
    \end{tikzpicture}
\caption{Graph of $y=x^{-3}$ or $y=1/x^3$}
\end{minipage}
    \end{figure}
\end{document}

在此处输入图片描述

答案2

Dimension too large是一个令人讨厌的错误。在大多数情况下,您可以使用 Asymptote(更准确)来避免它。下面我使用纯 TikZ 给出了一个不完整的解决方案。

在此处输入图片描述

\documentclass{article}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{tikz}
\begin{document}
\subsubsection*{Negative Integer Powers}
The graphs of power functions that have negative integer exponents have \emph{two} distinct shapes (see Figures 3 and 4 below).
\vspace*{3mm}

\noindent%
\begin{minipage}[h]{3in}
\begin{tikzpicture}[scale=.6]
\draw[blue!50] (-5,-5) grid (5,5);
\foreach \i in {-4,...,-1,1,2,...,4}
\path[nodes={scale=.8,fill=white}] 
(\i,0) node[below]{$\i$}
(0,\i) node[left]{$\i$};
\draw[-stealth] (-5,0)--(5,0);
\draw[-stealth] (0,-5)--(0,5);
\begin{scope}
\clip (-5,-5) rectangle (5,5);
\draw[magenta,smooth,ultra thick]
plot[domain=-3:-.2] (\x,{1/(\x*\x)})
plot[domain=.2:3] (\x,{1/(\x*\x)}); 
\end{scope}

\path (current bounding box.south) node[below]
{Figure 3: $y=x^n$ $\bigcup$-shaped};
\end{tikzpicture}
\end{minipage}
\hfill
\begin{minipage}[h]{3in}
\begin{tikzpicture}[scale=.6]
\draw[blue!50] (-5,-5) grid (5,5);
\foreach \i in {-4,...,-1,1,2,...,4}
\path[nodes={scale=.8,fill=white}] 
(\i,0) node[below]{$\i$}
(0,\i) node[left]{$\i$};
\draw[-stealth] (-5,0)--(5,0);
\draw[-stealth] (0,-5)--(0,5);
\begin{scope}
\clip (-5,-5) rectangle (5,5);
\draw[magenta,smooth,ultra thick]
plot[domain=-3:-.2] (\x,{1/(\x*\x*\x)})
plot[domain=.2:3] (\x,{1/(\x*\x*\x)}); 
\end{scope}
\path (current bounding box.south) node[below]
{Figure 4: Graph of $y=x^{-3}=1/x^3$};
\end{tikzpicture}
\end{minipage}
\end{document} 

相关内容