Approximation of a quadratic function with neural networks I've set a simple function (y = x^2) to generate data to be used to train a neural network. The goal is to do predictions with new data. I've been trying to play around with the parameters of the train function, but I haven't been able to get good results. Should a neural network be useful in such a problem? What would be the right approach to achieve reasonably accurate predictions?
# Function
myf <- function(x){
  return(x^2)
}

# Train and test data
datasetSize <- 100
set.seed(8)
xtrain <- runif(datasetSize*0.7, -10, 10)
xtest <- runif(datasetSize*0.3, -10, 10)
plot(x=xtrain,y=myf(xtrain))
trainset <- data.frame(x = xtrain, y = myf(xtrain))
testset <- data.frame(x = xtest, y = myf(xtest))

# Neural network
library(caret)
nn <- train(y ~ ., data=trainset,
             preProcess=c("center","scale"),
             method="nnet", 
             maxit=10000, trace=F)
nn.results <- predict(nn, testset)
plot(x=xtest, y=nn.results)
testset$y/nn.results

 A: Using the neuralnet package, the R code of the solution for the approximation of the function y = x^2 is shown below. Using one single hidden layer with 10 neurons yields results within 1% of accuracy at worst in the given test set.
# Function
myf <- function(x){return(x*x)}
set.seed(8)

# Train and test data
datasetSize <- 100
xtrain <- sort(runif(datasetSize*0.7, -10, 10))
xtest <- sort(runif(datasetSize*0.3, -10, 10))
plot(x=xtrain,y=myf(xtrain))
trainset <- data.frame(x = xtrain, y = myf(xtrain))
testset <- data.frame(x = xtest, y = myf(xtest))

# NEURAL NETWORK (neuralnet)-------------------------
library(neuralnet)  
nn <- neuralnet(y~x, trainset, 
                hidden=c(10), threshold=0.001)
nn.results <- compute(nn, xtest)
tb <- cbind(xtest, testset$y,
            as.data.frame(nn.results$net.result))
colnames(tb) <- c("X","f", "NN")  
tb$ratio <- tb$f/tb$NN
print(tb)

Actually, it can be checked that works for more complex functions continuous in a given range. To achieve higher accuracy you can increase the number of hidden layers and/or increase the number of neurons in the hidden layer. For instance:
# Function
myf <- function(x){return(0.6+0.4*x*x+0.3*x*sin(15*x))}
set.seed(8)

# Train and test data
datasetSize <- 100
xtrain <- sort(runif(datasetSize*0.7, 0, 1))
xtest <- sort(runif(datasetSize*0.3, 0, 1))
plot(x=xtrain,y=myf(xtrain))
trainset <- data.frame(x = xtrain, y = myf(xtrain))
testset <- data.frame(x = xtest, y = myf(xtest))

# NEURAL NETWORK (neuralnet)-------------------------
library(neuralnet)  
nn <- neuralnet(y~x, trainset, 
                hidden=c(200), threshold=0.001)
nn.results <- compute(nn, xtest)
tb <- cbind(xtest, testset$y,
            as.data.frame(nn.results$net.result))
colnames(tb) <- c("X","f", "NN")  
tb$ratio <- tb$f/tb$NN
print(tb)

