# Optimizing an energy functional in mathematica using variational calculus

I have an energy functional which I want to optimize using calculus of variation.

It would be nice if someone could please post a working example using mathematica. The procedure is as follows, specify the energy functional, compute the Euler-Lagrange equation, discretize them and if possible run for a sample data.

There are some links to the package for calculus of variation. But none of them is providing a complete example as I have described in the above paragraph.

UPDATE1

I am trying the simple Lucas-and-Kanade energy functional

$E(u(x_0,y_0),v(x_0,y_0)) = \int(f_x(x,y,t)u+f_y(x,y,t)v+f_t)dx dy$

I have tried to do it with [this link]

UPDATE2

The correct form of the energy functional is, courtesy of [rcollyer]

$E(u(x,y),v(x,y)) = \int(f_x(x,y,t)u+f_y(x,y,t)v+f_t)^2dx dy$

The question is now how to descritize the Euler-Lagrange equations. and solve it for some artificial data. (this is not extra thing specified in the question already)

* UPDATE3* the complete solution procedure is as follows:

The E-L equations for the integrand of the above energy functional are as follows and we set the first variation to zero

$2f_x(f_x(x,y,t)u+f_y(x,y,t)v+f_t)=0$

$2f_y(f_x(x,y,t)u+f_y(x,y,t)v+f_t)=0$

in the matrix form it will look as follows

Now we can simply solve for u and v because we have two equations and two unknowns but the derivatives are needed to be descritized. For example if we take the forward difference assuming the distance is 1

$f_x = f_{i+1}-f_{i}$

$f_y = f_{j+1}-f_{j}$

In above equations i and j represent the indexing in x and y direction respectively.

$f_t = f_{2}-f_{1}$

Here in the above equation the function is described by indices 1 and 2 describe the function in the temporal direction. (Mathematica provides a lot of variety for descritizing as per the documentation)

So the idea is to be able to replace the derivative with finite differences and we should be able to solve the equation system for u and v given the data.

thanks a lot

-
This is very similar to a homework assignment I gave to my students some time ago. Could you post what you have tried? – belisarius Jul 6 '12 at 0:28
Please post your energy functional, along with what you've tried. You ask for examples to show you something, but you never give us an example of what you need help with – rm -rf Jul 6 '12 at 0:28
@all, valid points, +1 to both, I have updated the question, Please let me know if anything is ambiguous! – Shan Jul 6 '12 at 0:49
Ok +1 to you now – belisarius Jul 6 '12 at 1:12
The nice thing about variational formulations is that the can also be used directly in discretized form. So why not do that here by minimizing the discretized integral with respect to the discretized target function? – Jens Jul 6 '12 at 15:02
show 1 more comment

There is some suggestion in what literature I had access to that your integrand should be squared. Regardless, I think I have some advice for using the VariationalMethods package. In order to make it work, I had to make $u$ and $v$ dependent on $x$ and $y$ despite the implication that they are independent of $x$ and $y$ on the RHS of your equation. Doing otherwise makes only one function available for differentiation: $f(x,y,t)$. The papers imply that it is really $u$ and $v$ that you are looking for, but taking the variational derivative of your integrand with respect to $f$, gives

VariationalD[D[f[x, y, t], x] u[x, y] + D[f[x, y, t], y] v[x, y] + D[f[x, y, t], t], f[x,y,t], {x,y,t}]
(* 0 *)


However, if you square the integrand you get

(* - 2*(Derivative[0, 0, 2][f][x, y, t]    + 2*v*Derivative[0, 1, 1][f][x, y, t]
+ v^2*Derivative[0, 2, 0][f][x, y, t]   + 2*u*Derivative[1, 0, 1][f][x, y, t]
+ 2*u*v*Derivative[1, 1, 0][f][x, y, t] + u^2*Derivative[2, 0, 0][f][x, y, t]) *)


This, however, differs quite considerably from your equation as it implies that there is an integral over $t$, also. This is indicated by the last term in VariationalD (and similarly in EulerEquations), but if you shorten it to only the spatial coordinates ({x,y}) you get the error

VariationalD::args: VariationalD takes a single integrand,
a function or list of functions, and a list of variables as input.


which is about the most useless error I have encountered. That said, that it generates an error makes sense as a functional is by definition a mapping from a function space to a number space, and by not integrating by $t$ you have a mapping between two function spaces. However, it works perfectly fine if you are concerned with $u(x,y)$ and $v(x,y)$ as they are not explicitly dependent on $t$ which of course implies that $E$ is dependent on $t$.

So, setting

integrand = D[f[x, y, t], x] u[x, y] + D[f[x, y, t], y] v[x, y] + D[f[x, y, t], t]


we get

VariationalD[integrand, {u[x, y], v[x, y]}, {x, y}]
(* {Derivative[1, 0, 0][f][x, y, t], Derivative[0, 1, 0][f][x, y, t]} *)


or

EulerEquations[integrand, {u[x, y], v[x, y]}, {x, y}]
(* {Derivative[1, 0, 0][f][x, y, t] == 0, Derivative[0, 1, 0][f][x, y, t] == 0} *)


If the integrand is supposed to be squared, then simply do this instead

VariationalD[integrand^2, {u[x, y], v[x, y]}, {x, y}]
(*
{  2*Derivative[1, 0, 0][f][x, y, t]
* (Derivative[0, 0, 1][f][x, y, t] + v[x, y]*Derivative[0, 1, 0][f][x, y, t]
+ u[x, y]*Derivative[1, 0, 0][f][x, y, t]
),
2*Derivative[0, 1, 0][f][x, y, t]
* (Derivative[0, 0, 1][f][x, y, t] + v[x, y]*Derivative[0, 1, 0][f][x, y, t]
+ u[x, y]*Derivative[1, 0, 0][f][x, y, t]
)
}
*)


(Note: Derivative[0, 1, 0][f][x, y, t] is the internal form an unevaluated derivative takes, and the above simply means $\partial f /\partial y$.)

-
super answer, You are right the integrand is squared and u,v are the function of x and y respectively. I ll update the question. After we have all this, next step is the descritization (we can specify anything like finite differences etc)and solving it for some data. Could you please take some time to explain how to proceed further. Thanks a lot! – Shan Jul 6 '12 at 7:29
I want to do the same as it has been specified in this tutorial reference.wolfram.com/mathematica/tutorial/NDSolvePDE.html. – Shan Jul 6 '12 at 7:36
@Shan Incidentally, the form Derivative[...][f][x,y,z]` is how it is interpreted not how it is displayed to the user. I used that form here because it is both readable and copyable into Mathematica. – rcollyer Jul 6 '12 at 13:47
@Shan In response to you comments, this is the point where I have to ask what have you tried? How much of the solution do you understand prior to attempting to tackle it with Mathematica? For instance, the PDE at the end is unusual in that neither $u$ nor $v$ have derivatives in it. I believe I can tackle it, but before I do so, I'd like more insight on your part as it may be involved and I don't wish to do all the work myself. – rcollyer Jul 6 '12 at 13:52
I have modified the question as per your comments... Please let me know if there is anything else which needs clarification. – Shan Jul 6 '12 at 21:41