# functionalDerivative

Functional derivative (variational derivative)

## Syntax

``G = functionalDerivative(f,y)``

## Description

example

````G = functionalDerivative(f,y)` returns the functional derivative $\frac{\delta S}{\delta y}\left(x\right)$ of the functional $S\left[y\right]={\int }_{a}^{b}f\left[x,y\left(x\right),y\text{'}\left(x\right),...\right]\text{\hspace{0.17em}}dx$ with respect to the function y = y(x), where x represents one or more independent variables. The functional derivative relates the change in the functional S[y] with respect to a small variation in y(x).The functional derivative is also known as the variational derivative.If `y` is a vector of symbolic functions, `functionalDerivative` returns a vector of functional derivatives with respect to the functions in `y`, where all functions in `y` must depend on the same independent variables.```

## Examples

collapse all

Find the functional derivative of the functional $S\left[y\right]={\int }_{b}^{a}y\left(x\right)\mathrm{sin}\left(y\left(x\right)\right)\phantom{\rule{0.2222222222222222em}{0ex}}dx$ with respect to the function $y$, where the integrand is $f\left[y\left(x\right)\right]=y\left(x\right)\phantom{\rule{0.16666666666666666em}{0ex}}\mathrm{sin}\left(y\left(x\right)\right)$.

Declare `y(x)` as a symbolic function and define `f` as the integrand of $S$. Use `f` and `y` as the parameters of `functionalDerivative`.

```syms y(x) f = y*sin(y); G = functionalDerivative(f,y)```
`G(x) = $\mathrm{sin}\left(y\left(x\right)\right)+\mathrm{cos}\left(y\left(x\right)\right) y\left(x\right)$`

Find the functional derivative of the functional $S\left[u,v\right]={\int }_{b}^{a}\left({u}^{2}\left(x\right)\frac{dv\left(x\right)}{dx}+v\left(x\right)\frac{{d}^{2}u\left(x\right)}{d{x}^{2}}\right)\phantom{\rule{0.2222222222222222em}{0ex}}dx$ with respect to the functions $u$ and $v$, where the integrand is $f\left[u\left(x\right),v\left(x\right),{u}^{\prime \prime }\left(x\right),{v}^{\prime }\left(x\right)\right]={u}^{2}\frac{dv}{dx}+v\frac{{d}^{2}u}{d{x}^{2}}$.

Declare `u(x)` and `v(x)` as symbolic functions, and define `f` as the integrand of $S$.

```syms u(x) v(x) f = u^2*diff(v,x) + v*diff(u,x,x);```

Specify a vector of symbolic functions `[u v]` as the second input argument in `functionalDerivative`.

`G = functionalDerivative(f,[u v])`
```G(x) =  ```

`functionalDerivative` returns a vector of symbolic functions containing the functional derivatives of the integrand `f` with respect to `u` and `v`, respectively.

Find the Euler–Lagrange equation of a mass `m` that is connected to a spring with spring constant `k`.

Define the kinetic energy `T`, potential energy `V`, and Lagrangian `L` of the system. The Lagrangian is the difference between the kinetic and potential energy.

```syms m k x(t) T = 1/2*m*diff(x,t)^2; V = 1/2*k*x^2; L = T - V```
```L(t) =  ```

In Lagrangian mechanics, the action functional of the system is equal to the integral of the Lagrangian over time, or $S\left[x\right]={\int }_{{t}_{1}}^{{t}_{2}}L\left[t,x\left(t\right),\underset{}{\overset{˙}{x}}\left(t\right)\right]\phantom{\rule{0.16666666666666666em}{0ex}}dt$. The Euler–Lagrange equation describes the motion of the system for which $S\left[x\left(t\right)\right]$ is stationary.

Find the Euler–Lagrange equation by taking the functional derivative of the integrand `L` and setting it equal to `0`.

`eqn = functionalDerivative(L,x) == 0`
```eqn(t) =  ```

`eqn` is the differential equation that describes mass-spring oscillation.

Solve `eqn` using `dsolve`. Assume the mass `m` and spring constant `k` are positive. Set the initial conditions for the oscillation amplitude as $x\left(0\right)=10$ and the initial velocity of the mass as $\underset{}{\overset{˙}{x}}\left(0\right)=0$.

```assume(m,'positive') assume(k,'positive') Dx(t) = diff(x(t),t); xSol = dsolve(eqn,[x(0) == 10, Dx(0) == 0])```
```xSol =  $10 \mathrm{cos}\left(\frac{\sqrt{k} t}{\sqrt{m}}\right)$```

Clear assumptions for further calculations.

`assume([k m],'clear')`

The Brachistochrone problem is to find the quickest path of descent of a particle under gravity without friction. The motion is confined to a vertical plane. The time for a body to move along a curve $y\left(x\right)$ from point $a$ to $b$ under gravity $g$ is given by

`$t={\int }_{a}^{b}\sqrt{\frac{1+{{y}^{\prime }}^{2}}{2gy}}\phantom{\rule{0.16666666666666666em}{0ex}}dx.$`

Find the quickest path by minimizing the change in $t$ with respect to small variations in the path $y$. The condition for a minimum is $\frac{\delta t}{\delta y}\left(x\right)=0$.

Compute the functional derivative to obtain the differential equation that describes the Brachistochrone problem. Use `simplify` to simplify the equation to its expected form.

```syms g y(x) assume(g,'positive') f = sqrt((1 + diff(y)^2)/(2*g*y)); eqn = functionalDerivative(f,y) == 0; eqn = simplify(eqn)```
```eqn(x) =  ```

This equation is the standard differential equation of the Brachistochrone problem. To find the solutions of the differential equation, use `dsolve`. Specify the `'Implicit'` option to `true` to return implicit solutions, which have the form $F\left(y\left(x\right)\right)=g\left(x\right)$.

`sols = dsolve(eqn,'Implicit',true)`
```sols =  ```

The symbolic solver `dsolve` returns general solutions in the complex space. Symbolic Math Toolbox™ does not accept the assumption that the symbolic function $y\left(x\right)$ is real.

Depending on the boundary conditions, there are two real-space solutions to the Brachistochrone problem. One of the two solutions below describes a cycloid curve in real space.

`solCycloid1 = sols(3)`
```solCycloid1 =  ${C}_{1} \mathrm{atan}\left(\sqrt{-\frac{{C}_{1}}{y\left(x\right)}-1}\right)-y\left(x\right) \sqrt{-\frac{{C}_{1}}{y\left(x\right)}-1}={C}_{4}+x$```
`solCycloid2 = sols(4)`
```solCycloid2 =  ${C}_{1} \mathrm{atan}\left(\sqrt{-\frac{{C}_{1}}{y\left(x\right)}-1}\right)-y\left(x\right) \sqrt{-\frac{{C}_{1}}{y\left(x\right)}-1}={C}_{5}-x$```

Another solution in real space is a horizontal straight line, where $y$ is a constant.

`solStraight = simplify(sols(5))`
`solStraight = ${C}_{1}+y\left(x\right)=0$`

To illustrate the cycloid solution, consider an example with boundary conditions $y\left(0\right)=5$ and $y\left(4\right)=1$. In this case, the equation that can satisfy the given boundary conditions is `solCycloid2`. Substitute the two boundary conditions into `solCycloid2`.

```eq1 = subs(solCycloid2,[x y(x)],[0 5]); eq2 = subs(solCycloid2,[x y(x)],[4 1]);```

The two equations, `eq1` and `eq2`, have two unknown coefficients, ${C}_{1}$ and ${C}_{5}$. Use `vpasolve` to find numerical solutions for the coefficients. Substitute these solutions into `solCycloid2`.

```coeffs = vpasolve([eq1 eq2]); eqCycloid = subs(solCycloid2,{'C1','C5'},{coeffs.C1,coeffs.C5})```
```eqCycloid =  $-6.4199192418473511250705556729108 \mathrm{atan}\left(\sqrt{\frac{6.4199192418473511250705556729108}{y\left(x\right)}-1}\right)-y\left(x\right) \sqrt{\frac{6.4199192418473511250705556729108}{y\left(x\right)}-1}=-x-5.8078336827583088482183433150164$```

The implicit equation `eqCycloid` describes the cycloid solution of the Brachistochrone problem in terms of $x$ and $y\left(x\right)$.

You can then use `fimplicit` to plot `eqCycloid`. Since `fimplicit` only accepts implicit symbolic equations that contain symbolic variables $x$ and $y$, convert the symbolic function $y\left(x\right)$ to a symbolic variable $y$. Use `mapSymType` to convert $y\left(x\right)$ to $x$. Plot the cycloid solution within the boundary conditions $0 and $1.

```funToVar = @(obj) sym('y'); eqPlot = mapSymType(eqCycloid,'symfun',funToVar); fimplicit(eqPlot,[0 4 1 5])``` For a function $u\left(x,y\right)$ that describes a surface in 3-D space, the surface area can be determined by the functional

`$F\left[u\right]={\int }_{{y}_{1}}^{{y}_{2}}{\int }_{{x}_{1}}^{{x}_{2}}f\left[x,y\left(x\right),u\left(x,y\right),{u}_{x},{u}_{y}\right]\phantom{\rule{0.2777777777777778em}{0ex}}dx\phantom{\rule{0.2777777777777778em}{0ex}}dy={\int }_{{y}_{1}}^{{y}_{2}}{\int }_{{x}_{1}}^{{x}_{2}}\sqrt{1+{u}_{x}^{2}+{u}_{y}^{2}}\phantom{\rule{0.2777777777777778em}{0ex}}dx\phantom{\rule{0.2777777777777778em}{0ex}}dy$`

where ${u}_{x}$ and ${u}_{y}$ are the partial derivatives of $u$ with respect to $x$ and $y$.

Find the functional derivative of the integrand `f` with respect to `u`.

```syms u(x,y) f = sqrt(1 + diff(u,x)^2 + diff(u,y)^2); G = functionalDerivative(f,u)```
```G(x, y) =  ```

The result is the equation `G` that describes the minimal surface of a 3-D surface defined by `u(x,y)`. The solutions to this equation describe minimal surfaces in 3-D space, such as soap bubbles.

## Input Arguments

collapse all

Integrand of a functional, specified as a symbolic variable, function, or expression. The argument `f` represents the density of the functional.

Differentiation function, specified as an unassigned symbolic function or a vector, matrix, or multidimensional array of unassigned symbolic functions. The argument `y` can be a function of one or more independent variables. If `y` is a vector of symbolic functions, `functionalDerivative` returns a vector of functional derivatives with respect to the functions in `y`, where all functions in `y` must depend on the same independent variables.

Example: `syms y(x); G = functionalDerivative(y*sin(y),y)`

## Output Arguments

collapse all

Functional derivative, returned as a symbolic function or a vector of symbolic functions. If input `y` is a vector, matrix, or an array, then `G` is a vector.

collapse all

### Functional Derivative

Consider a functional

`$S\left[y\right]={\int }_{a}^{b}f\left[x,y\left(x\right),y\text{'}\left(x\right),...,{y}^{\left(n\right)}\left(x\right)\right]\text{\hspace{0.17em}}dx,$`

which can take any path from a to b in the x-space.

For a small variation in the path y(x), define the change as $\delta y\left(x\right)=\epsilon \varphi \left(x\right)$ in which ϕ(x) is an arbitrary test function. The change in the functional S is

`$DS\left[y\right]=\underset{\epsilon \to 0}{\mathrm{lim}}\frac{S\left[y+\epsilon \varphi \right]-S\left[y\right]}{\epsilon }={\int }_{a}^{b}\frac{\delta S}{\delta y}\left(x\right)\varphi \left(x\right)dx\text{.}$`

The expression $\frac{\delta S}{\delta y}\left(x\right)$ is the functional derivative of S with respect to y. The linear functional DS[y] is also known as the first variation or the Gateaux differential of the functional S.

One method to calculate the functional derivative is to apply Taylor expansion to the expression S[y + εϕ] with respect to ε. By keeping the first order terms in ε, performing integration by parts, and choosing the boundary conditions ϕ(a) = ϕ(b) = ϕ'(a) = ϕ'(b) = ... = 0, the functional derivative becomes

`$\begin{array}{lll}\frac{\delta S}{\delta y}\left(x\right)\hfill & =\hfill & \frac{\partial f}{\partial y}-\frac{d}{dx}\frac{\partial f}{\partial {y}^{\text{'}}}+\frac{{d}^{2}}{d{x}^{2}}\frac{\partial f}{\partial {y}^{\text{'}\text{'}}}-...+{\left(-1\right)}^{n}\frac{{d}^{n}}{d{x}^{n}}\left(\frac{\partial f}{\partial {y}^{\left(n\right)}}\right)\hfill \\ \hfill & =\hfill & \sum _{i=0}^{n}{\left(-1\right)}^{i}\frac{{d}^{i}}{d{x}^{i}}\left(\frac{\partial f}{\partial {y}^{\left(i\right)}}\right).\hfill \end{array}$`

## Version History

Introduced in R2015a