#######################################################
#######################################################
#
### Plot of Example 3.45 - Fourier Series ######
#
#######################################################
#######################################################


fcn <- function(x) 1/2 + 2/pi*(cos(x) - 1/3*cos(3*x) + 1/5*cos(5*x) - 1/7*cos(7*x))
x <- seq(-pi, pi, length.out = 1000)
plot(x, fcn(x), 
     type = "l", lwd = 1.5, 
     col = "blue", 
     xaxt = "n", yaxt = "n", 
     xlab = "", ylab = "",
     bty = "n",
     xlim = c(-3.5, 3.5), ylim = c(-0.3, 1.2))


# Draw the axes

lab <- c(expression(-pi), 
         expression(-frac(pi,2)), 
         0, 
         expression(frac(pi,2)), 
         expression(pi))

xvalues <- c(-pi, -pi/2, 0, pi/2, pi)


arrows(x0 = -3.5, y0 = 0,
       x1 = 3.5, y1 = 0,
       length = 0.10, code = 2,
       col = "black", lwd = 2)
arrows(x0 = 0, y0 = -0.15,
       x1 = 0, y1 = 1.2,
       length = 0.10, code = 2,
       col = "black", lwd = 2)

for(i in 1:5){
  segments(xvalues[i], -0.02, 
           xvalues[i], 0.02, 
           col = "black", lwd = 2)
  
  text(xvalues[i], -0.2, lab[i])
}

segments(-0.05, 1, 0.05, 1,
         col = "black", lwd = 2)
text(0.10, 1.05, "1")



text(3.5, - 0.1, "x")
text(0.2, 1.15, "y")