PGFPlots 具有函数定义,然后通过替换常数来使用它

PGFPlots 具有函数定义,然后通过替换常数来使用它

我需要在 PGFPlots 中绘制几个函数,我绘制的每个函数实际上都是同一个主函数减去在某个点 x 处求值的函数。我想知道是否有一种方法可以定义一个函数,然后在 x 处求该函数的值,而不是对每个 x 进行硬编码。到目前为止,我拥有的代码是:

\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{amsmath}
\usepackage{mathptmx} % Use the Adobe Times Roman as the default text font together with math symbols from the Sym­bol, Chancery and Com­puter Modern fonts

\usepackage{tikz} % Required for drawing custom shapes
\usepackage{pgfplots}
\usetikzlibrary{plotmarks, calc, spy, pgfplots.polar, external}
\usepgflibrary{shapes.geometric}

\begin{document}

% add and edit tikz figure here:

\begin{tikzpicture}[scale=1]
    \begin{axis}

\addplot + [mark=none, domain=0:2, samples=70] { (x^0.65 + sin(1.5*pi*x)))/2};
\addplot + [mark=none, domain=0:4, samples=70] { (x^0.65 + sin(1.5*pi*x)) - (2^0.65 + sin(1.5*pi*2))))/2};
\addplot + [mark=none, domain=2:6, samples=70] { (x^0.65 + sin(1.5*pi*x)) - (4^0.65 + sin(1.5*pi*4))))/2};
\addplot + [mark=none, domain=4:8, samples=70] { (x^0.65 + sin(1.5*pi*x)) - (6^0.65 + sin(1.5*pi*6))))/2};
\addplot + [mark=none, domain=6:10, samples=70] { (x^0.65 + sin(1.5*pi*x)) - (8^0.65 + sin(1.5*pi*8))))/2};
\addplot + [mark=none, domain=8:12, samples=70] { (x^0.65 + sin(1.5*pi*x)) - (10^0.65 + sin(1.5*pi*10)))/2};
\addplot + [mark=none, domain=10:13, samples=70] { (x^0.65 + sin(1.5*pi*x)) - (12^0.65 + sin(1.5*pi*12))))/2};


   \end{axis}
\end{tikzpicture}
\end{document}

答案1

作为Torbjørn T.已经在问题下面的评论中提到,这可以使用来完成declare function

请注意代码中的注释!

\documentclass[bborder=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % use this `compat' level or higher to use the LUA backend for calculation
        % (--> speed improvement)
        compat=1.12,
        % declare your function here ...
        /pgf/declare function={
            f(\x,\a) = ((\x)^0.65 + sin(1.5*pi*(\x))) - ((\a)^0.65 + sin(1.5*pi*(\a))))/2;
            %                                                                         ^
            %           here is an extra closing brace and no opening one, which causes
            %                                      a different result, if you delete it
            %               --> please check what you really want and adapt accordingly
        },
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            no markers,
            samples=25,
        ]
            % ... and use it here
            \addplot+ [domain=0:2] {f(x,0)};
            \addplot+ [domain=0:4] {f(x,2)};
            \addplot+ [domain=2:6] {f(x,4)};
            \addplot+ [domain=4:8] {f(x,6)};
            \addplot+ [domain=6:10] {f(x,8)};
            \addplot+ [domain=8:12] {f(x,10)};
            \addplot+ [domain=10:13] {f(x,12)};
       \end{axis}
    \end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容