# How to Compute Analytical Derivative and Integral of trigonometric functions using Symbolic.jl and DiffRules.jl

**URL:** <https://discourse.julialang.org/t/how-to-compute-analytical-derivative-and-integral-of-trigonometric-functions-using-symbolic-jl-and-diffrules-jl/89480>\
**Category:** General Usage\
**Tags:** package\
**Created:** [October 29, 2022, 8:50am UTC](https://discourse.julialang.org/t/how-to-compute-analytical-derivative-and-integral-of-trigonometric-functions-using-symbolic-jl-and-diffrules-jl/89480 "2022-10-29T08:50:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [October 29, 2022, 8:50am UTC](https://discourse.julialang.org/t/how-to-compute-analytical-derivative-and-integral-of-trigonometric-functions-using-symbolic-jl-and-diffrules-jl/89480/1 "2022-10-29T08:50:52Z")

</div>

Hi all,

I have this equation

y = \frac{1}{e^{\frac{t}{2}}} \left[\int\_{t\_{0}}^{t} e^{\frac{s}{2}} (10 + 5 \ sin(2s)) \ ds + c \right] \\

I want to ask how to solve this with Symbolics.jl and DiffRules.jl (even if the packages are derivatives, I know they can be used to check whether the integration is correct)  
or are there any package that can help for the integration analytically? or both if possible…

From an old 2021 thread on this discourse I try this code ([Derivative of a Symbolic Expression as a Symbol Valued Function](https://discourse.julialang.org/t/derivative-of-a-symbolic-expression-as-a-symbol-valued-function/60975)):

```julia
using Symbolics

@variables a3, a2, a1, a0, t

f0(t) = a3*t^3 + a2*t^2 + a1*t + a0

```

and it does not work:  
**┌ Info: Precompiling Symbolics [0c5d862f-8b57-4792-8d23-62f2024744c7]**  
**└ @ Base loading.jl:1423**  
**ERROR: LoadError: UndefVarError: similar\_type not defined**  
**Stacktrace:**  
\*\* [1] include\*\*  
\*\* @ ./Base.jl:418 [inlined]\*\*  
\*\* [2] include\_package\_for\_output(pkg::Base.PkgId, input::String, depot\_path::Vector{String}, dl\_load\_path::Vector{String}, load\_path::Vector{String}, concrete\_deps::Vector{Pair{Base.PkgId, UInt64}}, source::String)\*\*  
\*\* @ Base ./loading.jl:1318\*\*  
\*\* [3] top-level scope\*\*  
\*\* @ none:1\*\*  
\*\* [4] eval\*\*  
\*\* @ ./boot.jl:373 [inlined]\*\*  
\*\* [5] eval(x::Expr)\*\*  
\*\* @ Base.MainInclude ./client.jl:453\*\*  
\*\* [6] top-level scope\*\*  
\*\* @ none:1\*\*  
**in expression starting at /home/browni/.julia/packages/DiffResults/YLo25/src/DiffResults.jl:1**  
**ERROR: LoadError: Failed to precompile DiffResults [163ba53b-c6d8-5494-b064-1a9d43ac40c5] to /home/browni/.julia/compiled/v1.7/DiffResults/jl\_sNyCGj.**

I also try this:

```julia
using Symbolics

@variables x, y, z

f(x,y,z) = x^2 + sin(x+y) - z

Symbolics.derivative(::typeof(my_function), args::NTuple{N,Any}, ::Val{i})

```

from [Derivatives and Differentials · Symbolics.jl](https://docs.juliahub.com/Symbolics/eABRO/0.1.21/manual/derivatives/#Adding-Analytical-Derivatives-1)

still not working:  
**Info: Precompiling Symbolics [0c5d862f-8b57-4792-8d23-62f2024744c7]**  
**└ @ Base loading.jl:1423**  
**ERROR: LoadError: UndefVarError: similar\_type not defined**  
**Stacktrace:**  
\*\* [1] include\*\*  
\*\* @ ./Base.jl:418 [inlined]\*\*  
\*\* [2] include\_package\_for\_output(pkg::Base.PkgId, input::String, depot\_path::Vector{String}, dl\_load\_path::Vector{String}, load\_path::Vector{String}, concrete\_deps::Vector{Pair{Base.PkgId, UInt64}}, source::String)\*\*  
\*\* @ Base ./loading.jl:1318\*\*  
\*\* [3] top-level scope\*\*  
\*\* @ none:1\*\*  
\*\* [4] eval\*\*  
\*\* @ ./boot.jl:373 [inlined]\*\*  
\*\* [5] eval(x::Expr)\*\*  
\*\* @ Base.MainInclude ./client.jl:453\*\*  
\*\* [6] top-level scope\*\*  
\*\* @ none:1\*\*  
**in expression starting at /home/browni/.julia/packages/DiffResults/YLo25/src/DiffResults.jl:1**  
**ERROR: LoadError: Failed to precompile DiffResults [163ba53b-c6d8-5494-b064-1a9d43ac40c5] to /home/browni/.julia/compiled/v1.7/DiffResults/jl\_ixnUlq.**  
**Stacktrace:**

Thanks

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 29, 2022, 2:14pm UTC](https://discourse.julialang.org/t/how-to-compute-analytical-derivative-and-integral-of-trigonometric-functions-using-symbolic-jl-and-diffrules-jl/89480/2 "2022-10-29T14:14:49Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> and it does not work:  
> **┌ Info: Precompiling Symbolics [0c5d862f-8b57-4792-8d23-62f2024744c7]**  
> **└ @ Base loading.jl:1423**  
> **ERROR: LoadError: UndefVarError: similar\_type not defined**

This has nothing to do with your own code, you have something broken in your package installation. What version of Julia are you using? What version of Symbolics (type `status` at the `pkg>` prompt).

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [October 29, 2022, 3:23pm UTC](https://discourse.julialang.org/t/how-to-compute-analytical-derivative-and-integral-of-trigonometric-functions-using-symbolic-jl-and-diffrules-jl/89480/3 "2022-10-29T15:23:22Z")

</div>

I am using IJulia with kernel Julia 1.7.3

This is the status of packages in my environment:

```julia
      Status `~/LasthrimProjection/JupyterLab/DynamicalSystems/Project.toml`
  [91a5bcdd] Plots v1.35.5
  [24249f21] SymPy v1.1.7
  [0c5d862f] Symbolics v4.13.0

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 29, 2022, 5:56pm UTC](https://discourse.julialang.org/t/how-to-compute-analytical-derivative-and-integral-of-trigonometric-functions-using-symbolic-jl-and-diffrules-jl/89480/4 "2022-10-29T17:56:55Z")

</div>

Works fine for me with Julia 1.8.2 and Symbolics 4.13.0.
