# Lecture 21:  The Calculus of Variations

## What to Learn?

* The concept of a "function of functions" and the definition of a functional
* The concept of finding a function that makes the functional an extremum
* How to practically compute the functional derivative for simple problems
* The concept of a conserved and non-conserved order parameter
* The definition of the non-classical chemical potential in a heterogeneous system
* How to use the functional derivative to solve for the order parameter profile through an interface

## What to do?

* Recall the arc length formula
* Write down a functional for all arc lengths between two points
* Find the shortest path length between two points (minimize the functional)
* Using the above process, find the shape of the minimum soapfilm between two rings
* Using the above process, set up the differential equation for a heterogeneous chemical system

$$
F(y_x,y,x)
$$

A functional is a function of functions.  It is necessary to treat $x$, $y$, and $y_x$ as independent (as though they are held constant during partial differentiation. 

### On Your Own
----

An (Imperfect but Colorful) Analogy

Using the calculus of variations is like this: you want to travel to Phoenix, AZ but you don't yet know the cheapest and fastest way to get there.  So - you imagine a nearly exhaustive list of ways to travel there (including things like walking, giant trebuchets, teleportation, etc.) and work out the costs and time required to each mode of transportation.  Once you evaluate all the modes (consider each mode as a different function of cost and time) - you pick the mode (function) that is optimal for cost and time.

In a picture, imagine all the functions that connect the two points "A" and "B".  We are searching for the function that minimizes the path between "A" and "B" subject to whatever constraints we place on the path.  The calculus of variations is a formal mathematical strategy for FINDING that function from all possible functions.



If calculus describes how numbers behave when mapped through functions, then the calculus of variations describe how functions behave when mapped through functions-of-functions.

Using the mean value theorem you can derive a formula for arc-length that reads:

$$
L(x,y,y_x) = \int_a^b \sqrt{1+ \left( \frac{dy}{dx} \right)^2} dx
$$

You can integrate this expression between two points $a$ and $b$ on a function $y(x)$ to get the length of the line between $a$ and $b$.  In the CoV we call $F$ a functional.  A functional is a function of functions.

The utility of the CoV is to produce a differential equation that is subsequently solved to produce a function that makes $F$ an extreme value.  In this case we are searching for the function $y(x)$ that minimizes $F$ betwen two points.  The CoV tells us that the following equation must be true for $y(x)$ to make $F$ an extreme value:

$$
\frac{\delta F}{\delta y} = \frac{\partial F}{\partial y} - \frac{d}{dx} \left( \frac{\partial F}{\partial y_x} \right)= 0
$$

This expression (for one dependent and one independent variable) is the core of the CoV.  IT is not the only result that can be developed, but for us this is the important one.  We can start by writing the above equation "by hand".  

We'll start with the usual imports:


```python
%matplotlib notebook
import sympy as sp
sp.init_printing()

f = sp.symbols('f', cls=sp.Function)
x, y = sp.symbols('x, y', real=True) 
```

We define our functional.  Note that the functional is only a function of the _derivative_ of $y$ rather than $y$ alone.


```python
f = sp.sqrt(1+(y(x).diff(x))**2)
f
```

You may be tempted to do this:


```python
f.diff(y)-(f.diff(y(x).diff(x))).diff(x)
```

You wouldn't be wrong to do the above, however things can be made a bit easier.  Let us look at each term in the ELE (Euler-Lagrange Equations):


```python
firstTerm = f.diff(y(x))
firstTerm
```


```python
secondTerm = f.diff(y(x).diff(x))
secondTerm
```

So - since the first term is zero we can use the fundamental theorem of the calculus to perform the first integral.  We add a constant and solve for the derivative.


```python
sp.var('C1')
integratedFunctional = sp.Eq(secondTerm,C1)
integratedFunctional
```


```python
firstSolution = sp.solve(integratedFunctional, y(x).diff(x))
firstSolution
```

This clearly indicates that:

$$
\frac{dy}{dx} = C
$$

and from this point it should be clear that the function $y(x)$ and makes $F$ an extreme is:

$$
y = mx + b
$$

If you would like to have `SymPy` finish the calculation, you can write:


```python
functionalExtremizer = sp.dsolve(sp.Eq(y(x).diff(x),firstSolution[0]), y(x))
functionalExtremizer
```

And that is a linear function.  To the extent that this is a proof - you've proven that a straight line is the shortest distance between two points.

#### Using SymPy's Functions

`SymPy` has an `euler_equation` function that we can try to use, too:


```python
L = sp.sqrt(1+(y(x).diff(x))**2)
differentialEquationFromELFunction = sp.euler_equations(L, y(x), x)
differentialEquationFromELFunction
```

A bit messy, but correct nonetheless.

#### DIY:  Find the Euler-Lagrange Equation (ELE)
----

Find the ELE for the functional, and if you can - solve for $y(x)$:

$$
v(y(x)) = \int_0^{\pi/2} (y_x^2 - y^2)dx
$$

The endpoint conditions are $y(0)=0$ and $y(\pi/2)=1$.  For reference, the general solution is:

$$
y(x)=C_1 \sin(x) + C_2 \cos(x)
$$

Don't forget to check the end points of the domain to find the constants.

#### The Problem of a Minimum Soapfilm
----

A classic problem in wetting and capillary science is that of the minimum soapfilm between two rings.  The soap film adopts a shape that minimizes its area.

The area of a soap film (found by rotating a curve through $2\pi$ around one axis is given by:

$$
A = L(x,y,y_x) = \int_{x_1}^{x_2} 2 \pi y (1+y_x^2)^{1/2} dx
$$

Note there is no explicit x dependence.


```python
Lsoapfilm = y(x)*sp.sqrt(1+(y(x).diff(x))**2)
(sp.euler_equations(Lsoapfilm,y(x),x)[0].lhs).simplify()
```

Attacking the problem this way leads to a second order ODE that we need to integrate.  Although this could be done - the lack of an explicit $x$ dependence permits using an identity that makes the problem a bit easier.

An equivalent statement of the ELE is:

$$
\frac{d}{dx} \left(F - y_x \frac{\partial F}{\partial y_x} \right) = \frac{\partial F}{\partial x}
$$

If there is no explicit $x$ dependence therefore the RHS of the above equation is zero - the first integral can be had for "free". Adding the integration constant we have:

$$
F - y_x \frac{\partial F}{\partial y_x} = C_2
$$

We can therefore write:


```python
C2 = sp.symbols('C2', positive=True)
beltramiODE = sp.Eq(Lsoapfilm - y(x).diff(x)*Lsoapfilm.diff(y(x).diff(x)),C2)
beltramiODE.simplify()
```

Now we solve the differential equation using `dsolve`.


```python
sp.dsolve(beltramiODE,y(x)) 
```

#### DIY:  Use the general solution and find the constants.


```python
# Find the constants if the curve is required to pass through a pair of particular points.
```

#### DIY:  Create a tool to explore the shape of different soapfilms.


```python
# Create an interactive widget to explore the values of the constants.
```

### Homework
----

Find the variational derivative of the following functional:

$$
F = W \phi^2 (1-\phi)^2 + \frac{1}{2} \epsilon^2 \left(\frac{d \phi}{dx} \right)^2
$$

e.g. compute:

$$
\frac{\delta F}{\delta \phi}
$$

The ODE that results will be solved to find the function $\phi(x)$.  $W$ and $\epsilon$ are parameters of the functional.


```python

```

80b91b70e1616658d27eb2269d6c9d23f72f0bb0
ipynb
Jupyter Notebook
Lecture-21-Calculus-of-Variations.ipynb
juhimgupta/MTLE-4720
41797715111636067dd4e2b305a782835c05619f
MIT
2017-07-19T04:04:38.000Z
2022-02-18T19:33:43.000Z
Lecture-21-Calculus-of-Variations.ipynb
juhimgupta/MTLE-4720
41797715111636067dd4e2b305a782835c05619f
MIT
2019-04-08T15:21:45.000Z
2020-03-03T20:19:00.000Z
Lecture-21-Calculus-of-Variations.ipynb
juhimgupta/MTLE-4720
41797715111636067dd4e2b305a782835c05619f
MIT
2017-07-27T02:27:49.000Z
2022-01-27T08:16:40.000Z
Qwen/Qwen-72B
1. YES
2. YES