Graphics, Figures & TablesPlotting 1/x^2 or 1/x^3 tikz

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
coachbennett1981
Posts: 274
Joined: Fri Feb 05, 2010 10:15 pm

Plotting 1/x^2 or 1/x^3 tikz

Post by coachbennett1981 »

I am having a terrible time of trying to get this right. The figures don't align and I have been looking all over to try to get this to work. I tried a couple of

Code: Select all

\addplot
commands, but the graphs don't appear to be correct even though I adjusted the domain. If anyone could help that would be appreciated.

Code: Select all

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{caption}
\usepackage{tikz}
\usepackage{pgfplots}
\\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}[ht]{2.75in}
\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^2)};
\addplot[samples=300,domain=0.1:3,ultra thick,color=blue]{1/(x^2)};
\qquad
\end{axis}
\end{tikzpicture}
\captionof{figure}{$y=x^n$ $\bigcup$-shaped}
\end{minipage}
\begin{minipage}[ht]{2.75in}
\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=red]{1/(x^3)};
\addplot[samples=300,domain=0.1:3,ultra thick,color=red]{1/(x^3)};
\end{axis}
\end{tikzpicture}
\captionof{figure}{Graph of $y=x^{-3}$ or $y=1/x^3$}
\end{minipage}

\end{document}
the graphs are not aligned anymore as well.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Plotting 1/x^2 or 1/x^3 tikz

Post by Bartman »

Changing the limit value for the red graph from 0.1 or -0.1 to at least 0.2 or -0.2 avoids the "dimension too large" error message and shifting the diagram.

Code: Select all

\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage{mathtools}% loads amsmath
\usepackage{amssymb}% loads amsfonts
\usepackage{caption}
\usepackage{pgfplots}% loads tikz
\usepackage{showframe}
\usepackage[noabbrev,capitalise]{cleveref}% added
    
\usetikzlibrary{arrows.meta}
\pgfplotsset{
    compat=1.17,
    every x tick label/.append style={font=\footnotesize, yshift=0.6ex},
    every y tick label/.append style={font=\footnotesize, xshift=0.5ex}
}
     
\setcounter{figure}{2}
     
\begin{document}
\subsubsection*{Negative Integer Powers}
     
The graphs of power functions that have negative integer exponents have \emph{two} 
distinct shapes (see \cref{fig:bluegraphs,fig:redgraphs} below).

\begin{center}
\pgfplotsset{
    axis lines = center,
    grid style={blue!50},
    grid = both,
    xmin = -5, xmax = 5,
    ymin = -5, ymax = 5,
    xtick = {-5,-4,...,5},
    ytick = {-5,-4,...,5},
    every axis plot/.append style={
        samples=300,
        ultra thick
    }
}
\begin{minipage}[t]{.5\linewidth}
    \centering
    \begin{tikzpicture}[scale=.9]% scaled
        \begin{axis}[
            every axis plot/.append style={blue}
        ]
        \addplot[domain=-3:-0.1]{1/(x^2)};
        \addplot[domain=0.1:3]{1/(x^2)};
        \end{axis}
    \end{tikzpicture}
    \captionof{figure}{$y=x^n$ $\bigcup$-shaped}
    \label{fig:bluegraphs}
\end{minipage}%
\begin{minipage}[t]{.5\linewidth}
    \centering
    \begin{tikzpicture}[scale=.9]% scaled
        \begin{axis}[
            every axis plot/.append style={red}
        ]
        \addplot[domain=-3:-0.5]{1/(x^3)};% domain changed
        \addplot[domain=0.5:3]{1/(x^3)};% domain changed
        \end{axis}
    \end{tikzpicture}
    \captionof{figure}{Graph of $y=x^{-3}$ or $y=1/x^3$}
    \label{fig:redgraphs}
\end{minipage}
\end{center}
\end{document}
coachbennett1981
Posts: 274
Joined: Fri Feb 05, 2010 10:15 pm

Plotting 1/x^2 or 1/x^3 tikz

Post by coachbennett1981 »

Here is a nice solution I was given:

Code: Select all

\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);}]
\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);}]
\addplot    {f(x)};
\end{axis}
    \end{tikzpicture}
\caption{Graph of $y=x^{-3}$ or $y=1/x^3$}
\end{minipage}
    \end{figure}
\end{document}
Post Reply