Suppose I have an expression that I want to symbolically differentiate using the Calculus package, and then create a function in Julia evaluating this expression at a given value of the variable x. For instance,
using Calculus
expression_F=“x^3”
expression_f= differentiate(expression_F,:x)
f(x)=eval(expression_f)
However this function is broken, as eval does not understand that I want to evaluate “x” to the local variable (not to a global one, see https://github.com/JuliaLang/julia/issues/2386)
Try f = eval(:(x->$expression_f)). That creates a function expression containing the expression to evaluate, so that x is bound within the expression. Then the function expression is evaluated, producing a function object you can call.
Many thanks! However, I’m not really understanding how this works. For if, I just define my expression by hand as in the following example, it doesn`t work!
julia> expression_F=“x^3”
“x^3”
julia> F(x)=eval(:(x->$expression_F))
F (generic function with 1 method)