# How to determine explicit derivative using variables with known derivatives

**URL:** <https://discourse.julialang.org/t/how-to-determine-explicit-derivative-using-variables-with-known-derivatives/101014>\
**Category:** General Usage\
**Tags:** question, symbolics\
**Created:** [June 30, 2023, 12:11pm UTC](https://discourse.julialang.org/t/how-to-determine-explicit-derivative-using-variables-with-known-derivatives/101014 "2023-06-30T12:11:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Van\_Giel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thomas_van_giel/32/46737_2.png) [@Thomas\_Van\_Giel](https://discourse.julialang.org/u/Thomas_Van_Giel)\
**Post date:** [June 30, 2023, 12:11pm UTC](https://discourse.julialang.org/t/how-to-determine-explicit-derivative-using-variables-with-known-derivatives/101014/1 "2023-06-30T12:11:11Z")

</div>

I want to check some Lyapunov functions for a dynamical system. In order to do that, I have the variables in my system: n1(t) - n4(t), and m(t), which define a set of coupled ODEs. I declare a (wannabe) Lyapunov function V(n, m).

Using the Julia package Symbolics, I input all of this into my system:

```plaintext
using Symbolics
using LinearAlgebra

N = 4
Â = A-I
@variables t n(t)[1:N] m(t)

M = ones(Num, N, N)
M[1,2] = m

```

I also define the derivatives for all n[i] and m with respect to time, as discussed in the [Symbolics documentation](https://symbolics.juliasymbolics.org/stable/manual/derivatives/#Adding-Analytical-Derivatives):

```julia
# Define the derivatives dnᵢ(t)/dt
for i in 1:N
    Symbolics.derivative(::typeof(n[i]), args::NTuple{1, Any}, ::Val{1}) = n[i]*(1+sum(Â[i,j] * M[i,j] * n[j](args[1]) for j in 1:N))
end

Symbolics.derivative(::typeof(m), args::NTuple{1, Any}, ::Val{1}) = ω*(1)(1 - m(args[1]) + β*n[3](args[1]))

```

Afterwards I define my Lyapunov function V, and get the derivatives:

```plaintext
function V(n, m)
    M = ones(Num, N, N)
    M[1,2] = m
    return sum(log(-sum(Â[i,j] * M[i,j] * n[j] for j in 1:N)) for i in 1:N)
end

Dt = Differential(t)
DtV = Dt(V(n, m))
out = simplify(expand_derivatives(DtV))

# Print the derivative
println("dV/dt is $(out)")

```

The output from this is

```julia
The dV/dt is (Differential(t)((n(t))[2]) + Differential(t)((n(t))[3]) - Differential(t)((n(t))[1]) - Differential(t)((n(t))[4]))*(((n(t))[2] + (n(t))[3] - (n(t))[1] - (n(t))[4])^-1) + (Differential(t)((n(t))[1]) + Differential(t)((n(t))[2]) - Differential(t)((n(t))[3]) - Differential(t)((n(t))[4]))*(((n(t))[1] + (n(t))[2] - (n(t))[3] - (n(t))[4])^-1) + (Differential(t)((n(t))[1]) + Differential(t)((n(t))[3]) - Differential(t)((n(t))[4]) - m(t)*Differential(t)((n(t))[2]) - Differential(t)(m(t))*(n(t))[2])*(((n(t))[1] + (n(t))[3] - (n(t))[4] - m(t)*(n(t))[2])^-1) + (Differential(t)((n(t))[1]) + Differential(t)((n(t))[2]) + Differential(t)((n(t))[3]) + Differential(t)((n(t))[4]))*(((n(t))[1] + (n(t))[2] + (n(t))[3] + (n(t))[4])^-1)

```

This contains a lot of `Differential(t)((n(t))[i]`. I already defined what this differential should be however. How can I make it expand all those `Differential(t)((n(t))[i]` terms, so the end result is a function of my variables n and m?

---

<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:** [June 30, 2023, 12:58pm UTC](https://discourse.julialang.org/t/how-to-determine-explicit-derivative-using-variables-with-known-derivatives/101014/2 "2023-06-30T12:58:44Z")

</div>

> [@Thomas\_Van\_Giel](#):
>
> ```julia
> for i in 1:N
> Symbolics.derivative(::typeof(n[i]), args::NTuple{1, Any}, ::Val{1}) = n[i]*(1+sum(Â[i,j] * M[i,j] * n[j](args[1]) for j in 1:N))
> end
> 
> ```

`n` isn’t a function, so this doesn’t make sense. Did you mean to substitute?

(also, dispatches can’t be defined in a for loop)

---

<div class="post-metadata">

**Author:** ![Thomas\_Van\_Giel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thomas_van_giel/32/46737_2.png) [@Thomas\_Van\_Giel](https://discourse.julialang.org/u/Thomas_Van_Giel)\
**Post date:** [June 30, 2023, 1:35pm UTC](https://discourse.julialang.org/t/how-to-determine-explicit-derivative-using-variables-with-known-derivatives/101014/3 "2023-06-30T13:35:16Z")

</div>

So, the mathematical problem is:  
I have a mathematical system defined by

\dot n\_i = n\_i (1 - \sum\_{j=1}^N \hat A\_{i,j} M\_{i,j} n\_j)\\ \dot m = \omega (1- m + \beta n\_k)

with M\_{1,2} = m and M\_{i,j} = 1 elsewhere.

Now I want to check the derivative of a function V(n,m).

V(n(t), m(t)) = \sum\_i \left(log (-\sum\_j\hat A\_{i,j} M\_{i,j} n\_j)\right)

Now I want to calculate \frac{dV}{dt} = \sum\_i \frac{\partial V}{\partial n\_i} \frac{\partial n\_i}{\partial t} + \frac{\partial V}{\partial m} \frac{\partial m}{\partial t} .

The problem I have (which I’m trying to solve with the `Symbolics.derivative(...)`command is adding the \frac{\partial n\_i}{\partial t} to the system.

### So to adress your comments:

> Blockquote `n` isn’t a function, so this doesn’t make sense. Did you mean to substitute?

I guess this means that adding n as function of t in the variables doesn’t count? Is it possible to add only the derivative (which is known) of a function instead of the function itself (which is unknown).

> Blockquote (also, dispatches can’t be defined in a for loop)  
> Is the only option then to write everything in full without the loop?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 30, 2023, 2:40pm UTC](https://discourse.julialang.org/t/how-to-determine-explicit-derivative-using-variables-with-known-derivatives/101014/4 "2023-06-30T14:40:53Z")

</div>

> [@Thomas\_Van\_Giel](#):
>
> Is the only option then to write everything in full without the loop?

Yes and no. You can write a macro that uses a for loop to output all that stuff.
