How to find Jacobian Matrix, df/du, df/dt, directional derivatives, etc. in julia

Hello, I have solved a system of ODE equation by using Jacobian Sparsity method. I am reading this link ODE Problems · DifferentialEquations.jl. It says that we can find df/du by using the command jac(J,u,p,t) or J=jac(u,p,t). But, I have no idea how to use it. Where to write this command in the code to get df/du. Kindly let me know. I am writing it in a new line. But, it is giving error that “jac is not defined”.

My code is like:

using DifferentialEquations

Define your ODE equation

function my_ode(du,u,p,t)
du[1] = u[2]
du[2] = -u[1] - 0.1*u[2]
end

Define the initial conditions and time span

u0 = [1.0, 0.0]
tspan = (0.0, 10.0)

p = 0

using Symbolics
du0 = copy(u0)
jac_sparsity = Symbolics.jacobian_sparsity((du,u)->my_ode(du,u,p,0.0),du0,u0)

f = ODEFunction(my_ode;jac_prototype=float.(jac_sparsity))

prob_sparse = ODEProblem(f,u0,tspan,p)

alg = Tsit5()
sol = solve(prob_sparse,alg)

answer = jac(J,u,p,t)

print(answer)

Here I used very simple ODE, but actual ODE that I am solving is very complex and its jacobian and direcational derivatives cannot found easity. So, I need to find it by using jac command.

That defines how you pass the Jacobian. So that’s not what you’re looking for.

Are you trying to get the Jacobian of the solution of the ODE? Or some Jacobian of a loss function defined on the solution?

I am trying to do something like this:

using DifferentialEquations

Define your ODE equation

function my_ode(du,u,p,t)
du[1] = u[2]
du[2] = -u[1] - 0.1*u[2]
end

Define the initial conditions and time span

u0 = [1.0, 0.0]
tspan = (0.0, 10.0)

p = 0

using Symbolics
du0 = copy(u0)
jac_sparsity = Symbolics.jacobian_sparsity((du,u)->my_ode(du,u,p,0.0),du0,u0)

f = ODEFunction(my_ode;jac_prototype=float.(jac_sparsity))

prob_sparse = ODEProblem(f,u0,tspan,p)

alg = Tsit5()
sol = solve(prob_sparse,alg)

answer = jac(J,u,p,t)

print(answer)

Here I used very simple ODE, but actual ODE that I am solving is very complex and its jacobian and direcational derivatives cannot found easity. So, I need to find it by using jac command.

prob = ODEProblem(my_ode, u0, tspan, p)
sys = modelingtoolkitize(prob)
extended_prob = ODEProblem(sys, [], tspan, jac = true)
extended_prob.f.jac(u,p,t)

would generate the symbolic solution to your Jacobian. But a better solution is likely just to use ForwardDiff.jl here. Have you tried using ForwardDiff?

I tried ForwardDiff as well. But, I failed. Not sure, how to merge ForwardDiff with my code. Kindly tell.

What did you try?

I am doing like this:

using DifferentialEquations

Define your ODE equation

function my_ode(du,u,p,t)
du[1] = u[2]
du[2] = -u[1] - 0.1*u[2]
end

Define the initial conditions and time span

u0 = [1.0, 0.0]
tspan = (0.0, 10.0)

p = 0

using Symbolics
du0 = copy(u0)
jac_sparsity = Symbolics.jacobian_sparsity((du,u)->my_ode(du,u,p,0.0),du0,u0)

f = ODEFunction(my_ode;jac_prototype=float.(jac_sparsity))

prob_sparse = ODEProblem(f,u0,tspan,p)

alg = Tsit5()
sol = solve(prob_sparse,alg)

Compute the Jacobian

u = sol(5.0)
t = 5.0
J = ForwardDiff.jacobian(sol, sol.t)

What derivative are you trying to compute here? df/du, du/dp, du/dt?

I like to find df/du.

u = sol(5.0)
t = 5.0
function diff_function(u)
  du = zero(u)
  my_ode(du,u,p,t)
  du
end
J = ForwardDiff.jacobian(diff_function, u)

Thank you very much.