# Calculating Jacobian from two matrices

**URL:** <https://discourse.julialang.org/t/calculating-jacobian-from-two-matrices/78966>\
**Category:** Modelling & Simulations\
**Tags:** package, calculus, finitediff\
**Created:** [April 3, 2022, 10:22pm UTC](https://discourse.julialang.org/t/calculating-jacobian-from-two-matrices/78966 "2022-04-03T22:22:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iddingsite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iddingsite/32/31840_2.png) [@Iddingsite](https://discourse.julialang.org/u/Iddingsite)\
**Post date:** [April 3, 2022, 10:22pm UTC](https://discourse.julialang.org/t/calculating-jacobian-from-two-matrices/78966/1 "2022-04-03T22:22:28Z")

</div>

Hi everyone,

I would like to compute a Jacobian for one of my problem for a PDE, but I don’t have a function linking the unknown to my function. So I only have 2 matrices, 1 with the values of the function and 1 with the values of the unknowns.

From my shallow understanding of the problem, I cannot use automatic differentiation, because I would need the expression linking the function with the unknowns. Should I then go for finite differences to obtain a pure numerical jacobian?

I have this simple flux function, in 1D to simplify:

f(C, x, t) = v (C, x, t) C(x, t)

and I need to compute:

\frac{\partial f\_i}{\partial C\_i} = v\_i + C\_i \frac{\partial v\_i}{\partial C\_i}

which is a simple diagonal matrix, but I don’t know the relationship betweem v and C, so also \frac{\partial v}{\partial C}.

Is there an easy way to compute it directly without this relationship? For example with FiniteDiff.jl?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 4, 2022, 5:21am UTC](https://discourse.julialang.org/t/calculating-jacobian-from-two-matrices/78966/2 "2022-04-04T05:21:40Z")

</div>

I’m not sure I understand your problem perfectly, but it sounds like you can use automatic differentiation.  
It is not necessary to work with a “function” in the mathematical sense, that is, an explicit formula linking input to output. All you need is a “function” in the programming sense, that is, an algorithm computing the output from the input. It can use for loops, if/else statements and pretty much anything else you need.  
However, it is unclear from your question if `C` is a function, an array or both?

---

<div class="post-metadata">

**Author:** ![Iddingsite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iddingsite/32/31840_2.png) [@Iddingsite](https://discourse.julialang.org/u/Iddingsite)\
**Post date:** [April 4, 2022, 9:48am UTC](https://discourse.julialang.org/t/calculating-jacobian-from-two-matrices/78966/3 "2022-04-04T09:48:17Z")

</div>

Thx for your answer. I will try to clarify a bit my question with an example:

My problem is that I don’t know the relationship between the terms in the function I want to calculate the Jacobian from.

So, for example, for a function x \* sin(x), let’s try to calculate \frac{\partial (x \* sin(x))}{\partial x} = sin(x) + cos(x) \* x

```julia
using ForwardDiff
using LinearAlgebra
using Plots

const nx = 20
const Lx = π
const Δx = Lx / (nx-1)
x = range(0, length=nx, stop= Lx)
v = zeros(nx)

v .= sin.(x)

function flux_false(x, v=v)
    x .* v
end

function flux_true(x)
    x .* sin.(x)
end

flux_value = flux_false(x)

Jacobian_wrong = ForwardDiff.jacobian(flux_false, x)
Jacobian_true = ForwardDiff.jacobian(flux_true, x)
deriv= sin.(x) .+ cos.(x) .* x

plot(flux_value, label="value function")
plot!(diag(Jacobian_wrong),label="jacobian wrong")
plot!(diag(Jacobian_true),label="jacobian true")
plot!(deriv, label="true derivatives", linestyle=:dash)

```

with the plot:

![plot_29](https://global.discourse-cdn.com/julialang/original/3X/4/8/484e45551a093b32840b2bfb4989ff59cc22277d.png)

So here, I can’t use automatic differentiation because it doesn’t know the relationship between v and x, and it is just considering that v is a constant, so \frac{\partial (x \* v)}{\partial x} = v = sin(x). This is the case for my real case problem.

Am I missing something?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 4, 2022, 10:26am UTC](https://discourse.julialang.org/t/calculating-jacobian-from-two-matrices/78966/4 "2022-04-04T10:26:14Z")

</div>

The actual problem is that you do not include the definition of `v` in the `flux_false` function. If instead you would write

```julia
function first_step(x)
    return sin.(x)
end

function second_step(x, v)
    return x .* v
end

function flux(x)
    v = first_step(x)
    y = second_step(x, v)
    return y
end

```

then everything would work smoothly. But your current `flux_false` treats `v` as an outside constant, with respect to which it doesn’t try to differentiate.

---

<div class="post-metadata">

**Author:** ![Iddingsite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iddingsite/32/31840_2.png) [@Iddingsite](https://discourse.julialang.org/u/Iddingsite)\
**Post date:** [April 5, 2022, 10:48am UTC](https://discourse.julialang.org/t/calculating-jacobian-from-two-matrices/78966/5 "2022-04-05T10:48:09Z")

</div>

Thanks, I think I should be able to solve my problem with that idea!
