# How to plot x^2*weierstrass?

I use the code below for drawing the Weierstrass function (from here). How can I change it to draw x²·weierstrass(x)?

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{fpu}
\def\x{\noexpand\x}
\edef\weierstrass{0}
\edef\currentbn{1}
\foreach \i in {1,...,19} {
% \global makes these definitions last beyond the current iteration
\global\edef\currentbn{2*\currentbn}    % b_n = 2 * b_n;
\global\edef\weierstrass{\weierstrass + (1/(\currentbn)*cos((\currentbn*\x) r))}    % weierstrass = weierstrass + (1/b_n) cos(b_n*\x radians);
}
\begin{document}
\begin{tikzpicture}
\draw[thick, color=lightgray,step=0.25cm,solid] (-2,-0.75) grid (2,1.0);
\draw[<->] (-2.1,0) -- (2.1,0) node[below right] {$x$};
\draw[<->] (0,-0.9) -- (0,1.1) node[left] {$y$};
\draw[color=blue, thick, domain=-2:2, samples=501, /pgf/fpu, /pgf/fpu/output format=fixed]
plot (\x, {\weierstrass});
\end{tikzpicture}
\end{document}


Thanks in advance!

## 1 Answer

I just copied the definition from Jake's answer and multiplied the plot by x^2 to get

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\makeatletter
\pgfmathdeclarefunction{weierstrass}{4}{%
\pgfmathfloattofixed@{#4}%
\afterassignment\pgfmath@x%
\expandafter\c@pgfmath@counta\pgfmathresult pt\relax%
\pgfmathfloatcreate{1}{0.0}{0}%
\let\pgfmathfloat@loc@TMPr=\pgfmathresult
\pgfmathfloatpi@%
\let\pgfmathfloat@loc@TMPp=\pgfmathresult%
\edef\pgfmathfloat@loc@TMPx{#1}%
\edef\pgfmathfloat@loc@TMPa{#2}%
\edef\pgfmathfloat@loc@TMPb{#3}%
\pgfmathloop
\ifnum\c@pgfmath@counta>-1\relax%
\pgfmathfloatparsenumber{\the\c@pgfmath@counta}%
\let\pgfmathfloat@loc@TMPn=\pgfmathresult%
\pgfmathpow{\pgfmathfloat@loc@TMPa}{\pgfmathfloat@loc@TMPn}%
\let\pgfmathfloat@loc@TMPe=\pgfmathresult%
\pgfmathpow{\pgfmathfloat@loc@TMPb}{\pgfmathfloat@loc@TMPn}%
\pgfmathmultiply{\pgfmathresult}{\pgfmathfloat@loc@TMPp}%
\pgfmathmultiply{\pgfmathresult}{\pgfmathfloat@loc@TMPx}%
\pgfmathdeg{\pgfmathresult}%
\pgfmathcos{\pgfmathresult}%
\pgfmathmultiply{\pgfmathresult}{\pgfmathfloat@loc@TMPe}%
\pgfmathadd{\pgfmathresult}{\pgfmathfloat@loc@TMPr}%
\let\pgfmathfloat@loc@TMPr=\pgfmathresult
\advance\c@pgfmath@counta by-1\relax%
\repeatpgfmathloop%
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle, axis equal image, enlarge y
limits=true,width=12cm]
\addplot [thick, black, samples=301, line join=round, domain=-2:2]
{x^2*weierstrass(x,0.5,3,10)};
\end{axis}
\end{tikzpicture}
\end{document}


• Please avoid x^2 and use x*x instead. Even when it works, it takes much longer to compute. – John Kormylo Oct 1 '19 at 13:44
• @JohnKormylo It is true that it makes it a bit faster when one uses x*x, but on my machine this is a ~3% effect for PDFLaTeX and ~5% for lualatex (which ironically takes longer here). – Schrödinger's cat Oct 1 '19 at 14:15
• OTOH, it is just good applied math. Just as quadratic polynomials should be written as (a*x+b)*x+c. – John Kormylo Oct 1 '19 at 14:17
• @Schrödinger'scat , thank's a lot!!! problem solved! – Kώστας Κούδας Oct 1 '19 at 18:13
• @JohnKormylo , thank's for your advise! – Kώστας Κούδας Oct 1 '19 at 18:14