# I use the function knn.reg to evaluate the numerical solution at a fixed grid of points # if( !require('FNN') ){ install.packages('FNN', dependencies=TRUE, repos='http://cran.rstudio.com/') } library(FNN) source('vector_predictor_corrector.R') t0 = 0.0 xy0 = c(1, 0) t1 = 0.4 # Where do we want to evaluate the solution: # t_grid = c( t1 ) vector_f = function(t, x, y){ f = rep(NA, 2) f[1] = x - 4*y f[2] = -x + y return(f) } # The vector predictor corrector method: # h=0.0001 res = vector_predictor_corrector(vector_f, h=h, start=t0, xy0=xy0, end=t1) pc_xhat = knn.reg( as.matrix( res$ts ), y=res$xys[1, ], test=as.matrix( t_grid ), k=1, algorithm='brute') pc_yhat = knn.reg( as.matrix( res$ts ), y=res$xys[2, ], test=as.matrix( t_grid ), k=1 , algorithm='brute') print(sprintf('x_hat(%.3f)= %f; y_hat(%.3f)= %f', t1, pc_xhat$pred, t1, pc_yhat$pred))