# How to determine explicit derivative using variables with known derivatives

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:

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:

# 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:

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

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?

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)

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.

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?

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