Purpose
To work out the Legendre exercise

Checking for nu = 7

> library(RColorBrewer)
> x <- (-128:128)/128
> A <- cbind(x^0, x, x^2, x^3)
> Q <- qr.Q(qr(A))
> D <- diag(1/Q[257, ])
> Q1 <- Q %*% D

Legendre_Exercise-001.jpg

> par(mfrow = c(1, 1))
> cols <- brewer.pal(4, "Set1")
> plot(x, Q1[, 1], ylim = c(-1, 1), col = cols[1], ylab = "", pch = 19)
> par(new = T)
> plot(x, Q1[, 2], ylim = c(-1, 1), col = cols[2], ylab = "", pch = 19)
> par(new = T)
> plot(x, Q1[, 3], ylim = c(-1, 1), col = cols[3], ylab = "", pch = 19)
> par(new = T)
> plot(x, Q1[, 4], ylim = c(-1, 1), col = cols[4], ylab = "", pch = 19,
+     main = "Legendre Polynomials")
> abline(h = 0, col = "grey", lty = "dashed")
> abline(v = 0, col = "grey", lty = "dashed")
> legend("bottomright", legend = c("P0", "P1", "P2", "P3"), fill = cols)

Legendre_Exercise-002.jpg

> par(mfrow = c(2, 2))
> cols <- brewer.pal(4, "Set1")
> P0 <- 1
> P1 <- x
> P2 <- 1.5 * x^2 - 0.5
> P3 <- 2.5 * x^3 - 1.5 * x
> plot(x, Q1[, 1] - P0, col = cols[1], ylab = "", pch = 19, main = "P0")
> plot(x, Q1[, 2] - P1, col = cols[2], ylab = "", pch = 19, main = "P1")
> plot(x, Q1[, 3] - P2, col = cols[3], ylab = "", pch = 19, main = "P2")
> plot(x, Q1[, 4] - P3, col = cols[4], ylab = "", pch = 19, , main = "P3")

Legendre_Exercise-003.jpg


Checking for nu = 8

> library(RColorBrewer)
> x <- (-256:256)/256
> A <- cbind(x^0, x, x^2, x^3)
> Q <- qr.Q(qr(A))
> D <- diag(1/Q[513, ])
> Q2 <- Q %*% D

Legendre_Exercise-004.jpg

> par(mfrow = c(1, 1))
> cols <- brewer.pal(4, "Set1")
> plot(x, Q2[, 1], ylim = c(-1, 1), col = cols[1], ylab = "", pch = 19)
> par(new = T)
> plot(x, Q2[, 2], ylim = c(-1, 1), col = cols[2], ylab = "", pch = 19)
> par(new = T)
> plot(x, Q2[, 3], ylim = c(-1, 1), col = cols[3], ylab = "", pch = 19)
> par(new = T)
> plot(x, Q2[, 4], ylim = c(-1, 1), col = cols[4], ylab = "", pch = 19,
+     main = "Legendre Polynomials")
> abline(h = 0, col = "grey", lty = "dashed")
> abline(v = 0, col = "grey", lty = "dashed")
> legend("bottomright", legend = c("P0", "P1", "P2", "P3"), fill = cols)

Legendre_Exercise-005.jpg

> par(mfrow = c(2, 2))
> cols <- brewer.pal(4, "Set1")
> P0 <- 1
> P1 <- x
> P2 <- 1.5 * x^2 - 0.5
> P3 <- 2.5 * x^3 - 1.5 * x
> plot(x, Q2[, 1] - P0, col = cols[1], ylab = "", pch = 19, main = "P0")
> plot(x, Q2[, 2] - P1, col = cols[2], ylab = "", pch = 19, main = "P1")
> plot(x, Q2[, 3] - P2, col = cols[3], ylab = "", pch = 19, main = "P2")
> plot(x, Q2[, 4] - P3, col = cols[4], ylab = "", pch = 19, , main = "P3")

Legendre_Exercise-006.jpg

For smaller spacing nu, the error goes down for all the legendre polynomials