0

I'm having trouble plotting a secant function. I get Dimension too large error but I can't solve it.

\documentclass{article}

    \usepackage{amsmath}
\usepackage{yhmath}
\usepackage[a4paper]{geometry}
\usepackage{enumitem}
\usepackage{amssymb}
\usepackage{multicol}
\usepackage[spanish]{babel}
\usepackage{hyperref}
\usepackage{imakeidx}
\usepackage{subfiles}
\usepackage{import}
\usepackage{cancel}
\usepackage{tikz}
\usepackage{tikz-network}
\usepackage{arydshln}
\usepackage{pgfplots}
    
 

\begin{document}

\begin{center}
            \begin{tikzpicture}
                \begin{axis}
                    [axis line style = very thick,
                    ymin=-4,ymax=4,
                    xmin=-2,xmax=5.5,
                    xtick=\empty,
                    ytick=\empty,
                    extra y ticks={-1,1},
                    extra x ticks={
                        -1.5708, %-pi/2
                        0, %0
                        1.5708, %pi/2
                        4.71239}, %3pi/2
                    extra x tick labels={
                    $-\frac{\pi}{2}$, %-pi/2
                    $0$, %0
                    $\frac{\pi}{2}$, %pi/2
                    $\frac{3\pi}{2}$}, %3pi/2
                    every extra x tick/.style={
                        xticklabel style={anchor=south west},
                        grid=major,
                        major grid style={thick,dashed,black}
                    },
                    axis lines = center,
                    xlabel=$x$,ylabel=$y$,
                    domain=-.5*pi:.5*pi,
                    samples=200,
                    ]
                    \addplot [blue,domain=-pi/2:pi/2,ultra thick] {sec(deg(x))};
                        \addplot [blue, domain=pi/2:3*pi/2,ultra thick] {sec(deg(x))};
                        %\node at (axis cs:0.2, -0.28) {$O$} ;
                        %\node at (axis cs:3.14, -0.28) {$\pi$} ;
                    \addplot[only marks, color=orange] coordinates {(0,1)};
                    \addplot[only marks, color=orange] coordinates {(0,-1)};
                \end{axis}
            \end{tikzpicture}
\end{center}
\end{document}

I have read several posts but I can not find what is the problem that makes my code fail. Changing the domain of the function, everything works, but I need to paint that domain.

The error is:

<recently read> \pgf@yy 
                        
l.59                 \end{axis}
                               
I can't work with sizes bigger than about 19 feet.
Continue and I'll use the largest value I can.
2
  • 1
    Your function has a singularity at pi/2 (plus multiples of pi, but never mind). Change the domain of your plot to avoid a small neighborhood of the singular point.
    – mickep
    Commented Aug 30, 2022 at 11:15
  • "Changing the domain of the function, everything works, but I need to paint that domain." Can you do this on paper? I'm guessing not. You have to avoid the singularity, or the plot would be, as pgf has said, too large. Just change your domain ever so slightly, like -1.5:1.5, to avoid the error. You'll get the same plot. Since you are trying to draw 200 points in this domain, the points will be virtually the same (at least in the visual sense). Commented Aug 31, 2022 at 22:19

1 Answer 1

2

You can use restrict y to domain like this:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis line style=very thick,
xmin=-2, xmax=5.5,
ymin=-4, ymax=4,
xtick={-pi/2, 0, pi/2, 3*pi/2},
ytick={-1,1},
xticklabels={$-\frac{\pi}{2}$, 0, $\frac{\pi}{2}$, $\frac{3\pi}{2}$}, 
xticklabel style={anchor=north west},
xmajorgrids=true,
major grid style={thick, dashed, black},
axis lines=center,
xlabel=$x$, ylabel=$y$,
restrict y to domain=-10:10,
]
\addplot[blue, domain=-pi/2:3*pi/2, ultra thick, samples=100, smooth] {sec(deg(x))};
\addplot[only marks, color=orange, forget plot] coordinates {(0,1) (0,-1)};
\end{axis}
\end{tikzpicture}
\end{document}

Graph of trig function sec

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .