How to have a scalar term in the functional that i try to minimize?


I try to minimize a functional under pde constraints according to some scalar coefficients. I would like to add a penalization term to the functional so the solution of the minimization problem stays close to the initial guess.
Here is a minimal problem that illustrate what i try to achieve. Let’s say that i want to solve the following optimization problem :
\min_{k\in R} \frac{1}{2} \int_{\Omega}(u - u^{*})^2dx + \frac{1}{2}(k-k_0)^2
Where :

  • u is solution of the following differential equation :
    \dfrac{\partial u}{\partial x} = ku
    With u(0)=1 and k \in R
  • u^* is the function that i want to fit with u by tweaking k.
  • k_0 is the initial guess of k in my optimization procedure.

i am able to define the forward problem in fenics as follows :

mesh = IntervalMesh(10, 0, 1)

V = FunctionSpace(mesh, "P", 1)
u = Function(V, name='State')
v = TestFunction(V)
u_star = project(Expression("exp(-0.8*x[0])", degree=1),V)

k0 = -1
k = Constant(k0)
k0 = Constant(k0)

F = (inner(u.dx(0), v) - k*inner(u,v)) * dx
bc = DirichletBC(V, 1, 'near(x[0], 0)')
solve(F == 0, u, bc)

Then i don’t know how to define my functional J =\frac{1}{2} \int_{\Omega}(u - u^{*})^2dx + \frac{1}{2}(k-k_0)^2

I have tried the following :

J = assemble(0.5*inner(u-u_star, u-u_star)*dx + 0.5*(k-k0)**2)

That gives me the following error :

TypeError: unsupported operand type(s) for +: 'Form' and 'Product'

With J correctly define in fenics, i would like to solve the optimisation problem like that :

control = Control(k)
rf = ReducedFunctional(J, control)
opt_k = minimize(rf, options={'maxiter': 50})

Thank you for your Help.