# I use the function knn.reg for simple comparisons below: # if( !require('FNN') ){ install.packages("FNN", dependencies=TRUE, repos='http://cran.rstudio.com/') } library(FNN) source('euler.R') # Problem 11: # x_grid = seq( 0.0, 3.0, by=0.5 ) # where do we want to evaluate our solution dy.dx = function(x, y){ 5 - 3*sqrt(y) } h=0.1 res = euler(dy.dx, h=h, start=0, y0=2, end=3.0) yhat_h1 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) h=0.05 res = euler(dy.dx, h=h, start=0, y0=2, end=3.0) yhat_h2 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) h=0.025 res = euler(dy.dx, h=h, start=0, y0=2, end=3.0) yhat_h3 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) h=0.01 res = euler(dy.dx, h=h, start=0, y0=2, end=3.0) yhat_h4 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) A = rbind( yhat_h1\$pred, yhat_h2\$pred, yhat_h3\$pred, yhat_h4\$pred ) rownames(A) = NULL R = cbind( c( 0.1, 0.05, 0.025, 0.01 ), A ) colnames(R) = c('h', 'y(0)', 'y(0.5)', 'y(1.0)', 'y(1.5)', 'y(2.0)', 'y(2.5)', 'y(3.0)') print('Problem 11:') print(R) # Problem 12: # x_grid = seq( 0.0, 3.0, by=0.5 ) # where do we want to evaluate our solution dy.dx = function(x, y){ y*(3-x*y) } h=0.1 res = euler(dy.dx, h=h, start=0, y0=0.5, end=3.0) yhat_h1 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) h=0.05 res = euler(dy.dx, h=h, start=0, y0=0.5, end=3.0) yhat_h2 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) h=0.025 res = euler(dy.dx, h=h, start=0, y0=0.5, end=3.0) yhat_h3 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) h=0.01 res = euler(dy.dx, h=h, start=0, y0=0.5, end=3.0) yhat_h4 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) A = rbind( yhat_h1\$pred, yhat_h2\$pred, yhat_h3\$pred, yhat_h4\$pred ) rownames(A) = NULL R = cbind( c( 0.1, 0.05, 0.025, 0.01 ), A ) colnames(R) = c('h', 'y(0)', 'y(0.5)', 'y(1.0)', 'y(1.5)', 'y(2.0)', 'y(2.5)', 'y(3.0)') print('Problem 12:') print(R) # Problem 13: # x_grid = seq( 0.0, 3.0, by=0.5 ) # where do we want to evaluate our solution dy.dx = function(x, y){ (4-x*y)/(1+y^2) } h=0.1 res = euler(dy.dx, h=h, start=0, y0=-2, end=3.0) yhat_h1 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) h=0.05 res = euler(dy.dx, h=h, start=0, y0=-2, end=3.0) yhat_h2 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) h=0.025 res = euler(dy.dx, h=h, start=0, y0=-2, end=3.0) yhat_h3 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) h=0.01 res = euler(dy.dx, h=h, start=0, y0=-2, end=3.0) yhat_h4 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) A = rbind( yhat_h1\$pred, yhat_h2\$pred, yhat_h3\$pred, yhat_h4\$pred ) rownames(A) = NULL R = cbind( c( 0.1, 0.05, 0.025, 0.01 ), A ) colnames(R) = c('h', 'y(0)', 'y(0.5)', 'y(1.0)', 'y(1.5)', 'y(2.0)', 'y(2.5)', 'y(3.0)') print('Problem 13:') print(R) # Problem 14: # x_grid = seq( 0.0, 3.0, by=0.5 ) # where do we want to evaluate our solution dy.dx = function(x, y){ -x*y + 0.1*y^3 } h=0.1 res = euler(dy.dx, h=h, start=0, y0=1, end=3.0) yhat_h1 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) h=0.05 res = euler(dy.dx, h=h, start=0, y0=1, end=3.0) yhat_h2 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) h=0.025 res = euler(dy.dx, h=h, start=0, y0=1, end=3.0) yhat_h3 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) h=0.01 res = euler(dy.dx, h=h, start=0, y0=1, end=3.0) yhat_h4 = knn.reg( as.matrix( res\$xs ), y=res\$ys, test=as.matrix( x_grid ), k=1, algorithm='brute' ) A = rbind( yhat_h1\$pred, yhat_h2\$pred, yhat_h3\$pred, yhat_h4\$pred ) rownames(A) = NULL R = cbind( c( 0.1, 0.05, 0.025, 0.01 ), A ) colnames(R) = c('h', 'y(0)', 'y(0.5)', 'y(1.0)', 'y(1.5)', 'y(2.0)', 'y(2.5)', 'y(3.0)') print('Problem 14:') print(R)