# Objective Function

**URL:** <https://fenicsproject.discourse.group/t/objective-function/7965>\
**Category:** variational formulation\
**Created:** [March 31, 2022, 6:32pm UTC](https://fenicsproject.discourse.group/t/objective-function/7965 "2022-03-31T18:32:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjuma1992](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jjuma1992/32/1846_2.png) [@jjuma1992](https://fenicsproject.discourse.group/u/jjuma1992)\
**Post date:** [March 31, 2022, 6:32pm UTC](https://fenicsproject.discourse.group/t/objective-function/7965/1 "2022-03-31T18:32:14Z")

</div>

Hello everyone. I have one question. I am trying to write the following objective function in Fenics and I am getting `TypeError: unsupported operand type(s) for /: 'Form' and 'float'` . Is it possible to have an objective function like this in Fenics? Any help is much appreciated. I guess I should mention that force `f` is given and both `u` and `v` are solutions of PDEs.

 ![Screen Shot 2022-03-31 at 14.24.30](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/f/f98fa19b9e79116dc6e7f43ed383476962a67e30.png)

---

<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:** [March 31, 2022, 6:51pm UTC](https://fenicsproject.discourse.group/t/objective-function/7965/2 "2022-03-31T18:51:24Z")

</div>

If the PDEs for u and v have been solved already, and you’re just computing J(u,v) as a post-processing step, then you could do something like

```python
J = 1.0 + assemble(f*v*ds)/assemble(f*v*ds)

```

---

<div class="post-metadata">

**Author:** ![jjuma1992](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jjuma1992/32/1846_2.png) [@jjuma1992](https://fenicsproject.discourse.group/u/jjuma1992)\
**Post date:** [April 1, 2022, 6:00am UTC](https://fenicsproject.discourse.group/t/objective-function/7965/3 "2022-04-01T06:00:21Z")

</div>

I guess my question was: I want to write it as the UFL form so I will be able to take derivative of the functioanal with respect to `u` . By writing like this `J = 1. + J1/J2` `J` is just a float and i will not be able to take derivative of `J` with respect to `u`

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 1, 2022, 7:25am UTC](https://fenicsproject.discourse.group/t/objective-function/7965/4 "2022-04-01T07:25:54Z")

</div>

You need to use software such as [dolfin-adjoint](http://www.dolfin-adjoint.org/en/latest) if you want to find the sensitivity of J wrt. u.
