The beauty of piecewise polynomials

It always amazes me  what beautiful objects may be created by very simple mathematical equations…piecewise

To replicate the graphs above you can use the following R code

piecewise1<- function(n) {
x_0<- matrix(c(1,1),2,1)
A<- matrix(c(1/2,1/2,-1/2,1/2),2,2)
e <- matrix(c(1,1),2,1)
bern <- rbinom(n-1,1,0.5) #bernoulli trials

x_temp <- matrix(0,2,n-1)
x_t <- cbind(x_0,x_temp)

for (i in  2:n)
{
x_t[,i] <- bern[i-1]*(A%*%x_t[,i-1]+e)+ (1-bern[i-1])*(A%*%x_t[,i-1]-e)
}
x_t
}

 

piecewise2<- function(n) {
x_0<- matrix(c(1,1),2,1)
A1<- matrix(c(0,0,0,1/4),2,2)
A2<- matrix(c(0.85,-0.04,0.04,0.85),2,2)
A3<- matrix(c(0.2,0.26,-0.26,0.22),2,2)
A4<- matrix(c(-0.15,0.26,0.28,0.24),2,2)
e1 <- matrix(c(0,0),2,1)
e2 <- matrix(c(0,1.6),2,1)
e3 <- matrix(c(0,0.8),2,1)
e4 <- matrix(c(0,1),2,1)

x_temp <- matrix(0,2,n-1)
x_t <- cbind(x_0,x_temp)

m <- sample(1:4, n-1, replace=T, prob= c(0.01,0.85,0.07,0.07))

for (i in  2:n)
{
x_t[,i] <- get(paste("A",m[i-1], sep=""))%*%x_t[,i-1]+ get(paste("e",m[i-1], sep=""))

}
x_t
}
Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s