## Solve -y'' + 0.1*y = 1 + sin(4*pi*x) on [0, 1] with y(0) = y(1) = 0.
fun <- function(x) 1 + sin(4*pi*x)
sol <- bvp(fun, 0, 1, 0, 0, 40, c(1, 0, 0.1))
# The analytic solution is
fan <- function(x) cc[1]*exp(sqrt(0.1)*x) + cc[2]*exp(-sqrt(0.1)*x) +
1/((4*pi)^2+0.1)*sin(4*pi*x) + 10
ezplot(fan, 0, 1, col="magenta", lty = 3)
points(sol$xh, sol$yh, type="p", col="blue")
grid()
Run the code above in your browser using DataLab