2

Basically I want to use PGFplots/tikzs to plot the sum of two expressions, but the first expression - let's name it f(x) - is specified within 0<=x<=1000 and the second expression - let's name it g(x) - should only be specified within 100<=x<=200 but is unfortunately not equal zero in the regions outside of 100<=x<=200. I just want to add the two functions (f(x)+g(x)) and plot the result with a single \addplot command within the full domain 0<=x<=1000, with g(x) being zero outside of 100<=x<=200.

Do you know a flexible and easy way to achieve the desired bahavior with a single \addplot[...][domain=0:1000]{...} command, as the only solution I found (but some sort of don't like - as it forces me to plot three different functions) is plotting

1. \addplot ... f(domain: 0<=x<100)
2. \addplot ... f(domain: 200<x<=1000)
3. \addplot ... f+g(domain: 100<=x<=200)

as a workaround.

PS: I want to avoid solutions that involve gnuplot or having to store the data within files or something like that... I'd prefer to have a solution that works with the following packages/libraries within the preamble \usepackage{tikz,pgfplots,grffile,amsmath} and \usetikzlibrary{fpu} but feel free to post the most easy solution.

I hope you don't mind that I didn't include any MWE and that the syntax I provided within the description is actually not 'fully working syntax' but just some sort of symbolic excerpts that provide the key to understand my problem.

2
  • I think you actually mean 1000 in \addplot ... f(domain: 0<=x<100), don't you ?
    – marsupilam
    Jun 25 '17 at 10:32
  • No it was correct: only within 100<=x<=200 I want the function f(x)+g(x) and within the rest (0<=x<100 and 200<x<=1000) I only want f(x).
    – phw
    Jun 25 '17 at 13:12
4

You can do pgfmath if then else inside math expressions.

\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}
% Cancel f(x) until 2 then after 4 shift it with 40
\addplot+[no marks,domain=0:6,samples=301] { -x^2 + ( x<2 ? x+x^2 : ( x>4 ? 40 : 0) ) };
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

2
  • I am going to mark your answer as solution, as it is very convenient and a little easier than the answer of @marsupilam. The solution I am using looks like this: %\addplot+[no marks,domain=0:1000,samples=1000] {( x>200 ? f(x) : ( x>=100 ? f(x)+g(x) : f(x) ) )}; Is there not an easier way to get the same result, e.g. a logical AND or similar?: %\addplot+[...] {( x<=200 && x>=100 ? f(x)+g(x) : f(x) )};
    – phw
    Jun 25 '17 at 13:14
  • 1
    @phw This is in my answer ! \addplot+[...] {f(x) + (x<=200)*(x>=100)*g(x)} ; Also the && operator is documented at page 927 in the PGF-TikZ manual
    – marsupilam
    Jun 25 '17 at 17:23
4

You can truncate your functions by using boolean multipliers.

The output

enter image description here

The code

\documentclass[12pt,tikz,border=0pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\def\omeg{.1}
\def\A{1}
\begin{tikzpicture}
  [
    declare function=
    {
      f(\x) = 1/\omeg * (1.5+sin( .1 * \omeg * deg(\x)));
      g(\x) = (100<=\x) * (\x<=200) * \A/\omeg * (1+sin(\omeg*deg(\x)));
    }
  ]
  \begin{axis}
    [
      %{{{
      domain = 0:1000,
      samples = 201,
      axis lines = center,
      %}}}
    ]

    \addplot+[mark=none] {f(x)};
    \addplot+[mark=none, domain=100:200] {g(x)};
    \addplot+[mark=none] {f(x) + g(x)};

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.