如何绘制 x^2*weierstrass?

如何绘制 x^2*weierstrass?

我使用下面的代码绘制魏尔斯特拉斯函数(来自这里)。我怎样才能改变它来绘制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}

提前致谢!

答案1

我只是从中复制了定义杰克的回答并将图x^2乘以

\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}

在此处输入图片描述

相关内容