I would like to know how to put another function as an argument for a function.
For example, let's say that I want to solve an integral by a given numerical integral approximation method. And I want to create a function that does this, in which the arguments of the function are:
Integral = function(a,b, fx)
Where a
and b
are the integration intervals and fx
is the function that I want to integrate.
It's still not working. See:
trapezio.integracao <- function(a, b, fn){ fn(x) }
{
x = seq(a, b, by = 0.005)
n = length(x)
integral = 0.5*sum((x[2:n] - x[1:(n-1)]) * (f(x[2:n]) + f(x[1:(n-1)]) ) )
return(integral)
}
fx <- function(x){x^2}
trapezio.integracao(0,1,fx)