[Tex/LaTex] PGFPlots with function definition and then use it by replacing a constant

pgfplotstikz-pgf

I need to plot several functions in PGFPlots, and each function I plot is actually the same main function minus the function evaluated at some point x. Instead of hard-coding the value of the function for each x, I was wondering if there's a way to define a function, and then evaluate such function at x. So far the code I have is:

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

Best Answer

As Torbjørn T. already mentioned in his comment below the question, this can be done using declare function.

Please note the comments in the code!

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

image showing the result of above code

Related Question