# How shoudl I evaluate a symbolic expression for a given value of a local variable?

**URL:** https://discourse.julialang.org/t/how-shoudl-i-evaluate-a-symbolic-expression-for-a-given-value-of-a-local-variable/16127
**Category:** New to Julia
**Created:** [October 10, 2018, 6:17pm UTC](https://discourse.julialang.org/t/how-shoudl-i-evaluate-a-symbolic-expression-for-a-given-value-of-a-local-variable/16127 "2018-10-10T18:17:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![pdenapo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdenapo/32/5686_2.png) [@pdenapo](https://discourse.julialang.org/u/pdenapo)
#### Post date: [October 10, 2018, 6:17pm UTC](https://discourse.julialang.org/t/how-shoudl-i-evaluate-a-symbolic-expression-for-a-given-value-of-a-local-variable/16127/1 "2018-10-10T18:17:51Z")

</div>

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](https://github.com/JuliaLang/julia/issues/2386))

julia\> f(8)  
ERROR: UndefVarError: x not defined

Many thanks if you could help me!

---

<div class="post-metadata">

### Author: ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)
#### Post date: [October 10, 2018, 6:24pm UTC](https://discourse.julialang.org/t/how-shoudl-i-evaluate-a-symbolic-expression-for-a-given-value-of-a-local-variable/16127/2 "2018-10-10T18:24:01Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![pdenapo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdenapo/32/5686_2.png) [@pdenapo](https://discourse.julialang.org/u/pdenapo)
#### Post date: [October 10, 2018, 6:34pm UTC](https://discourse.julialang.org/t/how-shoudl-i-evaluate-a-symbolic-expression-for-a-given-value-of-a-local-variable/16127/3 "2018-10-10T18:34:14Z")

</div>

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)

julia\> F(2)  
#11 (generic function with 1 method)

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 10, 2018, 6:49pm UTC](https://discourse.julialang.org/t/how-shoudl-i-evaluate-a-symbolic-expression-for-a-given-value-of-a-local-variable/16127/4 "2018-10-10T18:49:14Z")

</div>

> [@pdenapo](#):
>
> expression\_F=“x^3”

Note that this is a string and not an expression.

```julia
julia> F(x)=eval(:(x->$expression_F))

```

As with any function, this means that when we execute `F(x)` we get the result of computing the function body. The function body is

```julia
eval(:(x->$expression_F))

```

which is here evaluating the expression `x -> "x^3"`. This expression gives an anonymous function that takes one argument and returns the string “x^3”.

Ok, so in the end we get

```julia
julia> f = F("whatevs") # the argument is not used
#9 (generic function with 1 method)

julia> f("something else") # the argument is not used
"x^3"

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 10, 2018, 6:49pm UTC](https://discourse.julialang.org/t/how-shoudl-i-evaluate-a-symbolic-expression-for-a-given-value-of-a-local-variable/16127/5 "2018-10-10T18:49:41Z")

</div>

Also, please see [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530).
