# Initial Value Problem:
G <- function(x, g) {
-1000*g + sin(x)
}
g0 <- -1/1000001; x0 <- 0 # initial condition
xF <- 1 # interval endpoint
nMVT <- 1000 # number of subintervals used
# Monte Carlo solution based on one iteration:
ghat1 <- ODE.MVT(G, initvalue = g0, endpoint = xF, initpoint = x0, Niter = 1, npoints = nMVT)
# Monte Carlo solution based on five iterations:
ghat5 <- ODE.MVT(G, initvalue = g0, endpoint = xF, initpoint = x0, Niter = 5, npoints = nMVT)
gTrue <- function(x) (1000*sin(x) - cos(x))/1000001 # true solution
oldpar <- par(mfrow=c(1,3))
curve(gTrue(x)) #
lines(ghat1, col = 2, lty = 2, ylab="g(x)")
plot(abs(gTrue(ghat1$x) - ghat1$y) ~ ghat1$x, xlab = "x",
ylab = "Absolute Error", type = "l", ylim = c(0, 2e-6), main="1 Iteration")
plot(abs(gTrue(ghat5$x) - ghat5$y) ~ ghat5$x, xlab = "x",
ylab = "Absolute Error", type = "l", ylim = c(0, 2e-6), main="5 Iterations")
par(oldpar)
Run the code above in your browser using DataLab