# time points
t <- seq(from = 0, to = 1, length = 100)
# basis matrix evaluated at the time points t
# an intercept column is included
Phi <- cbind(legendre.polynomials(t, degree = 0), legendre.polynomials(t, degree = 1),
legendre.polynomials(t, degree = 2), legendre.polynomials(t, degree = 3))
# check the orthogonality
crossprod(Phi) # is equal to I_4 up to rounding errors
# plot the basis system excluding the constant column
matplot(t, Phi[,2:4], type = "l", lwd = 2, lty = 1:3,
ylab = "basis function", main = "Legendre Polynomials (normalized)")
legend("top", c("linear", "quadratic", "cubic"), col = 1:3, lwd = 2, lty = 1:3)
Run the code above in your browser using DataLab