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:
This LaTeX document shows how to use the modulo operation together with an if-then-else statement to generate periodic functions with a TikZ function declaration. We apply it to get a LaTeX diagram to visualize the parabolic movements from the quoted @ThePhysicsMemes post. https://t.co/QtzV1xZAqz pic.twitter.com/ieG5A719xf
— LaTeX.org (@TeXgallery) February 9, 2025
\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.
Leave a Reply
You must be logged in to post a comment.