# Help with symbolics, how to get coefficients and simple roots

**URL:** <https://discourse.julialang.org/t/help-with-symbolics-how-to-get-coefficients-and-simple-roots/68180>\
**Category:** New to Julia\
**Tags:** symbolics\
**Created:** [September 14, 2021, 11:36pm UTC](https://discourse.julialang.org/t/help-with-symbolics-how-to-get-coefficients-and-simple-roots/68180 "2021-09-14T23:36:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [September 14, 2021, 11:36pm UTC](https://discourse.julialang.org/t/help-with-symbolics-how-to-get-coefficients-and-simple-roots/68180/1 "2021-09-14T23:36:52Z")

</div>

```julia
using Symbolics

@variables x
D = Differential(x)
Z = 2.1309 + 0.0515x - 0.0009(x^2)
ZZ = expand_derivatives(D(Z))

# Derivative is 0.0515 - 0.0018x
println("Derivative is ",ZZ)

# How to get the coefficient of x from equation ZZ

# Question 1, how do I get the coefficient of x^0 which is 0.0515
#coefficient_x0 = ???

# Question 2, how do I get the coefficient of x^1 which is -0.0018
#coefficient_x1 = ???

# question 3, How do I find the root of the derivative which is 28.611
# derivative_root = ???

```

How to extract the coefficients of x from ZZ dynamically?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [September 15, 2021, 10:04pm UTC](https://discourse.julialang.org/t/help-with-symbolics-how-to-get-coefficients-and-simple-roots/68180/2 "2021-09-15T22:04:44Z")

</div>

I think someone recently asked for a coeff function but it doesn’t exist yet.

Linear rootfinding is done via `solve_for`.

---

<div class="post-metadata">

**Author:** ![lazycoder](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@lazycoder](https://discourse.julialang.org/u/lazycoder)\
**Post date:** [July 28, 2022, 11:08pm UTC](https://discourse.julialang.org/t/help-with-symbolics-how-to-get-coefficients-and-simple-roots/68180/3 "2022-07-28T23:08:46Z")

</div>

In math the solution to get the coefficients of a variable is to take the derivative with respect to the variable and set any residues of the variable to zero. Hopefully there is a better method without resorting to taking derivatives but until then that was my solution:

```julia
function mycoeffs(f,var,order)
    if order == 0
        return substitute(f,Dict(var=>0))
    end
    D=Differential(var^order)
    newf=expand_derivatives(D(f))
    return substitute(newf,Dict(var=>0))
end

```

You can test it with something like:

```julia
testfun=x*y+y^2+2*z*x
display(mycoeffs(testfun,y,0))

```
