I am trying to plot the Weierstrass function described in baby Rudin. I'm not trying to graph the entire series–even if I could graph its fourth or fifth partial sum nicely I would be relatively satisfied. For those not familiar with Rudin's construction, it goes like this:
Define $g(x)=2|\frac{x}{2}-\lfloor\frac{x+1}{2}\rfloor|$ and $g_n(x)=(\frac{3}{4})^n g(4^n x)$. Then the Weierstrass function is $\sum_{n=0}^\infty g_n(x)$.
I have managed to graph the fourth partial sum, but I am not satisfied with its resolution. My function is not identical to the Weierstrass functions shown in this old TeX SE question, but I would love for mine to look more like that, if possible. (I believe that much of the difference in appearance is simply because Rudin's construction is rougher than the classic Weierstrass function definition.) The graph I've created of the fourth partial sum is quite jagged in appearance, and looks like this:
I'm hopeful that there's still something I could do in pgfplots to improve its appearance. (I've already pushed the number of samples in the plot to a really large value, but I'm not sure if that's actually helping much or not.) Here's my code:
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
axis line style=ultra thick,
axis x line=center,
axis y line=center,
grid=none,
unit vector ratio=1 1,
width=3.5in,
xmin=-1.25, xmax=2.25,
ymin=-.25, ymax=2.5,
xtick={-1,0,...,2},
ytick={-2,-1,...,2},
]
\addplot[black, domain=-1.3:2.3, samples=30000] {2*abs(x/2-floor(x/2+.5))+3/4*2*abs(4*x/2-floor(4*x/2+.5))+9/16*2*abs(16*x/2-floor(16*x/2+.5))+27/64*2*abs(64*x/2-floor(64*x/2+.5))+81/256*2*abs(256*x/2-floor(256*x/2+.5))};
\end{axis}
\end{tikzpicture}
\end{document}




\addplot.[...samples=200]gnuplot{....}and compile with the-shell-escapeflag. – gigiair Jun 09 '22 at 14:19