Little useless-useful R functions – Continuous, nowhere differentiable Weierstrass function

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Coming from the simple sine function (remember of Fourier series), German

xs <- seq(-2*pi,2*pi,pi/100)
plot(xs,sin(2*xs),type="l",ylim=c(-1,1)); abline(h=0,lty=3)

mathematician Karl Weierstrass became the first to publish an example of a continuous, nowhere
differentiable function
. Weierstrass function (originally defined as a Fourier series) was the first instance in which the idea that a continuous function must be differentiable was introduced. This is an example of a fractal in a function (known as a fractal function) and also another of pathological functions (runs counter to some intuition).

The mathematical function is represented as:

Let b be a real number such that 0 < b < 1 and let a be a positive odd integer.

If ab > 1 and 2 3 > π ab−1 , then

is continuous on R and is not differentiable at any point in R.

And the R code:

weierstrass_curve <- function(x,a,b) {
  values <- 0
  for (n in 0:100) {  
    values <- values + (a**n * cos(b**n * pi * x)) }

len <- 1000
x <- seq(-2.4, 2.4, length.out=len)  
y <- weierstrass_curve(x,0.3,5)

plot(x, y, type = "l", col = "red", main = "Weierstrass curve")

As always, the complete code is available on GitHub in  Useless_R_function repository. The sample file in this repository is here (filename: Weierstrass_function.R). Check the repository for future updates.

Happy R-coding and stay healthy!

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)