1

I have been looking all over the Exchange and online to try to find how to plot functions like 1/x^3. I get an error in the dimension. I am using the \addplot command under tikz. I have tried different domains but to no avail. Any help would be appreciated.

\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}

It worked, but the dimensions and figures don't align.

Nick

Black Mild
  • 17,569
Nick B
  • 831
  • That's probably because you can't find the image of null. Try to draw it between -3 and -0.0001 and another one from 0.0001 and 3. Actually, it's almost what you did. – SebGlav Mar 03 '21 at 20:09
  • Didn't work. Thank you though for taking time to comment – Nick B Mar 03 '21 at 23:36
  • They talk about the "dimension too large" error in this question: https://tex.stackexchange.com/questions/139883/origin-and-formation-to-the-error-dimension-too-large Given that 0.1^(-3)=1000 and that is bigger than the maximun TeX can handle (around 576), it throws an error. Try with domain=-3:-0.2 and domain=0.2:3 and it should be fine. – Luis Sibaja Mar 04 '21 at 02:22
  • Remember math at school. What is 1/x^n for x = 0? Do not blame Latex – Uwe Mar 04 '21 at 09:16

2 Answers2

4

With pgfplots package. The error dimension too large is avoided by use of restrict 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}

enter image description here

Zarko
  • 296,517
  • Wow! Thank you so much Zarko! – Nick B Mar 04 '21 at 11:53
  • @NIckBennett, you are welcome! BTW, instead to say thank you, you should vote answer (by clicking on up-triangle at top left side of answer) or even accept it, if from all received answer the best fit your expectation (by clicking on the check mark at top left side of selected answer). As I see it, so far you have not accept any from the gotten answers. Did no one meet your expectations? – Zarko Mar 04 '21 at 12:02
  • Not that. Just didn't realize I could do that. – Nick B Mar 04 '21 at 12:23
  • Sigh, can't I convince anyone to use \x\x instead of \x2? Even when it works right, it is somewhat ambiguous. – John Kormylo Mar 04 '21 at 15:26
  • @JohnKormylo, see comments in code at definition of functions f(\x) ;-) – Zarko Mar 04 '21 at 17:04
  • @Zarko - Ah, thank you for that. – John Kormylo Mar 04 '21 at 22:05
0

Dimension too large is an annoying error. In most cases, you can use Asymptote (better accuracy) to avoid it. I give below an incomplete solution using plain TikZ.

enter image description here

\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}

Black Mild
  • 17,569