使用addpot绘制双曲函数时,修改表达式出现报错?

发布于 2025-07-12 13:28:01

同样的方法画双曲线函数,左边的由于定义域包含渐近线的零点而报错,意外给画了一条x=b,右边的定义域同样包含却正常编译,如何修改。

编译结果
image.png

        \addplot[                % 函数f(x)=1/(x-1) + 1
            domain = 1:7,        % 定义域[1,7]
            samples = 300,       % 采样点密度(值越高曲线越平滑)
            smooth,              % 平滑曲线
            % thick,             % 粗线宽
            line width = 0.5pt,  % 设置为0.5磅线宽
            blue                 % 颜色
        ] {-1/(x-7) + 1};     

以上是左侧图像

        \addplot[                % 函数f(x)=1/(x-1) + 1
            domain = 1:7,        % 定义域[1,7]
            samples = 300,       % 采样点密度(值越高曲线越平滑)
            smooth,              % 平滑曲线
            % thick,             % 粗线宽
            line width = 0.5pt,  % 设置为0.5磅线宽
            blue                 % 颜色
        ] {1/(x-1) + 1};      

以上是右侧图像

画图.tex

完整代码如下(同上文件)

\documentclass[tikz,border=5pt]{standalone}
\usepackage[UTF8]{ctex} % 加载中文支持
\usepackage{pgfplots} % 核心绘图库
\usepackage{bm} % 或 
\usepackage{amsmath}
\usepackage{silence}
\WarningFilter{latexfont}{} % 过滤所有字体警告
\pgfplotsset{compat=1.18} % 版本兼容性设置
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}



\begin{document}

\begin{tikzpicture}[
    node distance=1cm, % 节点间距
]

% 左侧图像节点
\node (left) {
    \begin{tikzpicture}
        \begin{axis}[
            axis lines = middle,
            %——xOy
            xlabel = $x$,  
            ylabel = $y$,
            x=0.5cm, % x轴每单位长度0.8cm
            y=0.5cm, % y轴每单位长度0.8cm
            width=11 * 0.5cm, % 根据x轴范围计算宽度
            height=6 * 0.5cm, % 根据y轴范围计算高度
            %将标签定位在x轴的​​最末端​​(刻度坐标系中,1表示100%位置)
            xlabel style={at={(ticklabel* cs:1)}, anchor=north, below=1pt},
            ylabel style={at={(ticklabel* cs:1)}, anchor=east, left=1pt},
            %
            xmin = -1, xmax = 9,
            ymin = -1, ymax = 5,
            axis line style = {-{Triangle[scale=1]}},  % 修正箭头语法
            %
            %——刻度和刻度线
            xtick = {1, 7},             %x标点
            xticklabels = {$a$, $b$},   %x自定义
            ytick = \empty,             % 隐藏y轴刻度
            tick style={draw=none},     % 隐藏所有刻度线
            title={\large $\bm{f'(x) \leq 0}$},
            title style={
                at={(0.5,0)},      % 相对位置 (x,y) ∈ [0,1]
                anchor=north,      % 锚点方向
                yshift=5pt         % 垂直偏移
            }
            ]

        %图像
        \node[below left] at (axis cs:0,0) {$O$};%原点 

        \addplot[                % 函数f(x)=1/(x-1) + 1
            domain = 1:7,        % 定义域[1,7]
            samples = 300,       % 采样点密度(值越高曲线越平滑)
            smooth,              % 平滑曲线
            % thick,             % 粗线宽
            line width = 0.5pt,  % 设置为0.5磅线宽
            blue                 % 颜色
        ] {-1/(x-7) + 1};         % 函数表达式(deg()将弧度转角度)

        \draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:7,0) -- (axis cs:7,5); % 从x=a处画垂直线
        \draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:1,0) -- (axis cs:1,1/6+1); % 从x=b处画垂直线


        % x=3 处切线
        \addplot[domain=0.7:7.3, smooth, red, line width= 0.5pt]{(1/16)*x + (17/16)};
        % x=4 处切线
        \addplot[domain=0.7:7.3, smooth, red, line width=0.5pt]{(1/9)*x + 8/9}; 


        % 在右上角添加解析式
        \node[anchor=north west, align=left, xshift=-5pt, yshift=-10pt] 
            at (axis cs:3,4) % 定位
            {\large $y=f(x)$};
        \end{axis}

    \end{tikzpicture}
};

% 右侧图像节点
\node[right=of left] (right) {
    \begin{tikzpicture}
        \begin{axis}[
            axis lines = middle,
            %——xOy
            xlabel = $x$,  
            ylabel = $y$,
            x=0.5cm, % x轴每单位长度0.8cm
            y=0.5cm, % y轴每单位长度0.8cm
            width=11 * 0.5cm, % 根据x轴范围计算宽度
            height=6 * 0.5cm, % 根据y轴范围计算高度
            %将标签定位在x轴的​​最末端​​(刻度坐标系中,1表示100%位置)
            xlabel style={at={(ticklabel* cs:1)}, anchor=north, below=1pt},
            ylabel style={at={(ticklabel* cs:1)}, anchor=east, left=1pt},
            %
            xmin = -1, xmax = 9,
            ymin = -1, ymax = 5,
            axis line style = {-{Triangle[scale=1]}},  % 修正箭头语法
            %
            %——刻度和刻度线
            xtick = {1, 7},             %x标点
            xticklabels = {$a$, $b$},   %x自定义
            ytick = \empty,             % 隐藏y轴刻度
            tick style={draw=none},     % 隐藏所有刻度线
            title={$\bm{f'(x) \geq 0}$},
            title style={
                at={(0.5,0)},      % 相对位置 (x,y) ∈ [0,1]
                anchor=north,      % 锚点方向
                yshift=5pt         % 垂直偏移
            }
            ]

        %图像
        \node[below left] at (axis cs:0,0) {$O$};%原点 

        \addplot[                % 函数f(x)=1/(x-1) + 1
            domain = 1:7,        % 定义域[1,7]
            samples = 300,       % 采样点密度(值越高曲线越平滑)
            smooth,              % 平滑曲线
            % thick,             % 粗线宽
            line width = 0.5pt,  % 设置为0.5磅线宽
            blue                 % 颜色
        ] {1/(x-1) + 1};         % 函数表达式(deg()将弧度转角度)

        \draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:1,0) -- (axis cs:1,5); % 从x=a处画垂直线
        \draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:7,0) -- (axis cs:7,1/6+1); % 从x=b处画垂直线


        % x=3 处切线
        \addplot[domain=0.7:7.3, smooth, red, line width= 0.5pt]{-0.25*x + 2.25};
        % x=4 处切线
        \addplot[domain=0.7:7.3, smooth, red, line width=0.5pt]{-1/9*x + 16/9}; 


        % 在右上角添加解析式
        \node[anchor=north west, align=left, xshift=-5pt, yshift=-10pt] 
            at (axis cs:3,4) % 定位
            {\large $y=f(x)$};
        \end{axis}

    \end{tikzpicture}
   
};
% 在整体图形下方添加标题节点
\node[below=10pt of current bounding box.south, 
      anchor=north]{图 \\ 3.3};

\title{123}
\end{tikzpicture}

\end{document}

查看更多

关注者
0
被浏览
139
1 个回答
Sagittarius Rover
我要成为Typst糕手/(ㄒoㄒ)/~~

关于文件的组织逻辑

首先,不建议使用
「同一个node里」左右放置两个tikzpicture,比较难管理

更建议「分别」建立「A.tex」绘制左图和「B.tex」绘制右图,再用传统插入图片PDF与caption的方式排版主文档,这样还有一个好处,caption可以使用超链接,也不用手动编号.

这个链接提到了「two-file approach」

image.png

简化问题

本着分而治之的思想,在找问题时最好将源代码分为不出问题的和出问题的两份。

正常工作的「1 / (x-1) + 1」:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{ctex} % 加载中文支持
\usepackage{pgfplots} % 核心绘图库
\pgfplotsset{compat=1.18}
\usepackage{bm} % 或 
\usepackage{amsmath,amsfonts}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines = middle,
        xlabel = $x$,  
        ylabel = $y$,
        x=0.5cm, % x轴每单位长度0.8cm
        y=0.5cm, % y轴每单位长度0.8cm
        width=11 * 0.5cm, % 根据x轴范围计算宽度
        height=6 * 0.5cm, % 根据y轴范围计算高度
        xlabel style={at={(ticklabel* cs:1)}, anchor=north, below=1pt},
        ylabel style={at={(ticklabel* cs:1)}, anchor=east, left=1pt},
        xmin = -1, xmax = 9,
        ymin = -1, ymax = 5,
        axis line style = {-{Triangle[scale=1]}},  % 修正箭头语法
        %——刻度和刻度线
        xtick = {1, 7},             %x标点
        xticklabels = {$a$, $b$},   %x自定义
        ytick = \empty,             % 隐藏y轴刻度
        tick style={draw=none},     % 隐藏所有刻度线
        title={$\bm{f'(x) \geq 0}$},
        title style={
            at={(0.5,0)},      % 相对位置 (x,y) ∈ [0,1]
            anchor=north,      % 锚点方向
            yshift=5pt         % 垂直偏移
            }
        ]
    \node[below left] at (axis cs:0,0) {$O$};%原点 
    \addplot[                % 函数f(x)=1/(x-1) + 1
        domain = 1:7,        % 定义域[1,7]
        samples = 300,       % 采样点密度(值越高曲线越平滑)
        smooth,              % 平滑曲线
        line width = 0.5pt,  % 设置为0.5磅线宽
        blue                 % 颜色
    ] { 1 / (x-1) + 1 };         % 函数表达式(deg()将弧度转角度)
    \draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:1,0) -- (axis cs:1,5); % 从x=a处画垂直线
    \draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:7,0) -- (axis cs:7,1/6+1); % 从x=b处画垂直线
    % x=3 处切线
    \addplot[domain=0.7:7.3, smooth, red, line width= 0.5pt]{-0.25*x + 2.25};
    % x=4 处切线
    \addplot[domain=0.7:7.3, smooth, red, line width=0.5pt]{-1/9*x + 16/9}; 
    % 在右上角添加解析式
    \node[anchor=north west, align=left, xshift=-5pt, yshift=-10pt] 
        at (axis cs:3,4) {\large $y=f(x)$};
    \end{axis}
\end{tikzpicture}
\end{document}

出问题的「-1 / ( x-7 ) + 1

\documentclass[tikz,border=5pt]{standalone}
\usepackage{ctex} % 加载中文支持
\usepackage{pgfplots} % 核心绘图库
\usepackage{bm} % 或 
\pgfplotsset{compat=1.18}
\usepackage{amsmath,amsfonts}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines = middle,
        %——xOy
        xlabel = $x$,  
        ylabel = $y$,
        x=0.5cm, % x轴每单位长度0.8cm
        y=0.5cm, % y轴每单位长度0.8cm
        width=11 * 0.5cm, % 根据x轴范围计算宽度
        height=6 * 0.5cm, % 根据y轴范围计算高度
        %将标签定位在x轴的​​最末端​​(刻度坐标系中,1表示100%位置)
        xlabel style={at={(ticklabel* cs:1)}, anchor=north, below=1pt},
        ylabel style={at={(ticklabel* cs:1)}, anchor=east, left=1pt},
        %
        xmin = -1, xmax = 9,
        ymin = -1, ymax = 5,
        axis line style = {-{Triangle[scale=1]}},  % 修正箭头语法
        %
        %——刻度和刻度线
        xtick = {1, 7},             %x标点
        xticklabels = {$a$, $b$},   %x自定义
        ytick = \empty,             % 隐藏y轴刻度
        tick style={draw=none},     % 隐藏所有刻度线
        title={\large $\bm{f'(x) \leq 0}$},
        title style={
            at={(0.5,0)},      % 相对位置 (x,y) ∈ [0,1]
            anchor=north,      % 锚点方向
            yshift=5pt         % 垂直偏移
        }
        ]
    %图像
    \node[below left] at (axis cs:0,0) {$O$};%原点 
    \addplot[                % 函数f(x)=1/(x-1) + 1
        domain = 1:7,        % 定义域[1,7]
        samples = 300,       % 采样点密度(值越高曲线越平滑)
        smooth,              % 平滑曲线
        % thick,             % 粗线宽
        line width = 0.5pt,  % 设置为0.5磅线宽
        blue                 % 颜色
    ] { -1 / ( x - 7 ) + 1 }; 
    \draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:7,0) -- (axis cs:7,5);% 从x=a处画垂直线
    \draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:1,0) -- (axis cs:1,1/6+1); % 从x=b处画垂直线
    % x=3 处切线
    \addplot[domain=0.7:7.3, smooth, red, line width= 0.5pt]{(1/16)*x + (17/16)};
    % x=4 处切线
    \addplot[domain=0.7:7.3, smooth, red, line width=0.5pt]{(1/9)*x + 8/9}; 
    % 在右上角添加解析式
    \node[anchor=north west, align=left, xshift=-5pt, yshift=-10pt] 
        at (axis cs:3,4) % 定位
        {\large $y=f(x)$};
    \end{axis}
\end{tikzpicture}
\end{document}

这将报错并且得不到「有效的PDF文件」,有错误必须纠正,不可以忽略。

! Dimension too large.
<recently read> \pgf@yy

l.60     \end{axis}

如何修改

其实维数太大意味着是正无穷呗~ 观察下面这段:

\addplot[                % 函数f(x)=1/(x-1) + 1
        domain = 1:7,        % 定义域[1,7]
        samples = 300,       % 采样点密度(值越高曲线越平滑)
        smooth,              % 平滑曲线
        % thick,             % 粗线宽
        line width = 0.5pt,  % 设置为0.5磅线宽
        blue                 % 颜色
    ] { -1 / ( x - 7 ) + 1 }; 

domain中的x=7不就除零了...

我不知道你的最终目的是什么,但是改成domain = 1:6.95即可:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{ctex} % 加载中文支持
\usepackage{pgfplots} % 核心绘图库
\usepackage{bm} % 或 
\pgfplotsset{compat=1.18}
\usepackage{amsmath,amsfonts}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines = middle,
        %——xOy
        xlabel = $x$,  
        ylabel = $y$,
        x=0.5cm, % x轴每单位长度0.8cm
        y=0.5cm, % y轴每单位长度0.8cm
        width=11 * 0.5cm, % 根据x轴范围计算宽度
        height=6 * 0.5cm, % 根据y轴范围计算高度
        %将标签定位在x轴的​​最末端​​(刻度坐标系中,1表示100%位置)
        xlabel style={at={(ticklabel* cs:1)}, anchor=north, below=1pt},
        ylabel style={at={(ticklabel* cs:1)}, anchor=east, left=1pt},
        %
        xmin = -1, xmax = 9,
        ymin = -1, ymax = 5,
        axis line style = {-{Triangle[scale=1]}},  % 修正箭头语法
        %
        %——刻度和刻度线
        xtick = {1, 7},             %x标点
        xticklabels = {$a$, $b$},   %x自定义
        ytick = \empty,             % 隐藏y轴刻度
        tick style={draw=none},     % 隐藏所有刻度线
        title={\large $\bm{f'(x) \leq 0}$},
        title style={
            at={(0.5,0)},      % 相对位置 (x,y) ∈ [0,1]
            anchor=north,      % 锚点方向
            yshift=5pt         % 垂直偏移
        }
        ]
    %图像
    \node[below left] at (axis cs:0,0) {$O$};%原点 
    \addplot[                % 函数f(x)=1/(x-1) + 1
        domain = 1:6.95,        % 定义域[1,7]
        samples = 300,       % 采样点密度(值越高曲线越平滑)
        smooth,              % 平滑曲线
        % thick,             % 粗线宽
        line width = 0.5pt,  % 设置为0.5磅线宽
        blue                 % 颜色
    ] { -1 / ( x - 7 ) + 1 }; 
    \draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:7,0) -- (axis cs:7,5);% 从x=a处画垂直线
    \draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:1,0) -- (axis cs:1,1/6+1); % 从x=b处画垂直线
    % x=3 处切线
    \addplot[domain=0.7:7.3, smooth, red, line width= 0.5pt]{(1/16)*x + (17/16)};
    % x=4 处切线
    \addplot[domain=0.7:7.3, smooth, red, line width=0.5pt]{(1/9)*x + 8/9}; 
    % 在右上角添加解析式
    \node[anchor=north west, align=left, xshift=-5pt, yshift=-10pt] 
        at (axis cs:3,4) % 定位
        {\large $y=f(x)$};
    \end{axis}
\end{tikzpicture}
\end{document}

image.png

至于为什么:

意外给画了一条x=b

因为当x=7时,除零了没法计算,计算得到的y遍历(-oo,+oo),也就是维数太多,画成图就是一条x=b的直线,而且报错。

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览