On X (Twitter), I saw a post by @ThePhysicMemes making fun of a bear running on a jumping ball. I used this occasion to generate periodic plots with an if-condition, demonstrating also how functions can be declared:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[
  declare function =
    {
      f(\x,\a,\b) =
        (mod(\x,\a)/\a<\b? % If 
        0.5-mod(\x,\a)^2/\b/\a: % Yes
        0.5-(\a-mod(\x,\a))^2/(\a-\b*\a));
    }
  ]
  \begin{axis}[axis lines = middle,
    ymax = 0.6, samples = 400,
    grid, domain = 0:5, no marks, thick]
    \addplot { f(x,1,0.5) };
    \addplot { 0.5 + 0.1*f(x-0.5, 1, 0.5) };
  \end{axis}
\end{tikzpicture} 
\end{document}

See also: Original Source by Stefan Kottwitz

Note: The copyright belongs to the blog author and the blog. For the license, please see the linked original source blog.