# Question on ufl.diff()

**URL:** <https://fenicsproject.discourse.group/t/question-on-ufl-diff/2388>\
**Category:** variational formulation\
**Created:** [February 7, 2020, 2:06pm UTC](https://fenicsproject.discourse.group/t/question-on-ufl-diff/2388 "2020-02-07T14:06:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dajuno](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@dajuno](https://fenicsproject.discourse.group/u/dajuno)\
**Post date:** [February 7, 2020, 2:06pm UTC](https://fenicsproject.discourse.group/t/question-on-ufl-diff/2388/1 "2020-02-07T14:06:44Z")

</div>

Hi,

I need to implement the UFL equivalent of

\frac{\partial \Psi(C)}{\partial C}\Big\rvert\_{C\_{n+\frac{1}{2}}}

as part of a variational formulation (hyperelasticity, one might guess ;)).  
\Psi(C) is a nonlinear function of a UFL tensor C=C(u) and u an actual FEniCS vector function.  
The index corresponds to time discretization, C\_{n+\frac{1}{2}} = \frac{1}{2}(C\_{n+1} + C\_n), with C\_{n+1} = C(u\_{n+1}) and C\_n=C(u\_n).

Is this correctly implemented by

```auto
C = 0.5*(C0 + C1)
C = variable(C)
Psi = some_function(C)
DPsi = diff(Psi, C)

```

? I.e., will `diff()` compute the derivative of `Psi` wrt `C`, and evaluate in `C = 0.5*(C0 + C1)`?

I don’t see what else these lines could be doing, but I am not really sure. Thank you for clearing up my confusion 🙂

---

<div class="post-metadata">

**Author:** ![kamensky](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@kamensky](https://fenicsproject.discourse.group/u/kamensky)\
**Post date:** [February 7, 2020, 2:43pm UTC](https://fenicsproject.discourse.group/t/question-on-ufl-diff/2388/2 "2020-02-07T14:43:28Z")

</div>

That sounds like the correct interpretation to me.

---

<div class="post-metadata">

**Author:** ![dajuno](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@dajuno](https://fenicsproject.discourse.group/u/dajuno)\
**Post date:** [February 7, 2020, 3:21pm UTC](https://fenicsproject.discourse.group/t/question-on-ufl-diff/2388/3 "2020-02-07T15:21:44Z")

</div>

Thanks for the feedback!
