\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{luacode}

\begin{luacode}
  function weierstrass(x0, x1, n, a, b, epsilon)
    local dx = (x1 - x0) / n
    local x = x0
    local out = assert(io.open("tmp.data", "w"))
    local y, k, dy

    while (x <= x1) do
      y = 0
      k = 0
      repeat
        dy = math.pow(a, k) * math.cos(math.pow(b, k) * math.pi * x)
        y = y + dy
        k = k + 1
      until (math.abs(dy) < epsilon)
      out:write(x, " ", y, "\string\n")
      x = x + dx
    end
    out:close()
  end
\end{luacode}

\begin{document}

\directlua{weierstrass(-2,2,500,0.3,5,1.e-12)}

\begin{tikzpicture}
  \begin{axis}[axis lines=middle, ymin=-1.5, ymax=1.75]
    \addplot[thick] table {tmp.data};    
  \end{axis}
\end{tikzpicture}

\end{document}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% TeX-engine: luatex
%%% End: 
