A couple of functions for computing path integrals and scalar potentials
A couple of functions for computing path integrals and scalar potentials in an arbitrary number of dimensions.
Requires Matlab R2012a or newer.
The main purpose of this piece of code is computing the scalar potential function associated to a given vector field. In order to do so, we’ll introduce the tool of path integral.
A path integral, also known as line or curve integral, is an integral where the integrand is evaluated along a curve:
The usual way of evaluating a path integral requires the specification of a parameterization of the curve:
So our path integral becomes the classical integral:
A gradient field is a vector field that can be derived as the gradient of a scalar field. For historical reasons, a minus sign is introduced
Path integrals over gradient fields depend only on the beginning and finishing points of the integration, being independent of the particular curve chosen to integrate between them. So, provided f is a gradient field, we can make non-ambiguous sense of an expression like:
One straightforward consequence of this is that any path integral over a gradient field and along a closed curve (which starts and ends at the same point) should equal zero.
Provided a gradient field, we can compute the associated scalar potential using:
Where the value of the potential at x_0 is an arbitrary integration constant.
% Underlying field
field = @(x) [-x(2), -x(1)];
% Parametric curve specification
curve = @(t) [t, t];
dcurve = @(t) [1, 1];
tmin = -2;
tmax = 1;
% Path integral
s = PathIntegral(field, curve, dcurve, tmin, tmax);
In the case of gradient fields the integral only depends in the initial and final points of the integration path. Thus, we can specify only those points regardless of the integration curve
% Underlying field
field = @(x) [-2.*x(1).*x(2), -x(1).^2];
% Initial and final points
x0 = [1 2];
x = [3 4];
% Path integral
s = PathIntegral(field, x0, x);
For those used to work with the symbolic toolbox of Matlab
% Define symbolic variables
syms x y t;
% Underlying field
field(x,y) [-y, -x];
% Parametric curve specification
curve(t) = [t, t];
tmin = -2;
tmax = 1;
% Path integral
s = PathIntegral(field, curve, tmin, tmax);
Given a gradient field, we can compute the associated potential
% Underlying field
field = @(x) -4*x.^3 + 3*x.^2 + 10*x - 1;
% Grid characteristics
x = -2:0.05:3;
% Setting the reference potential
x0 = -2;
V0 = 0;
% Compute the potential
V = Potential(field, V0, x0, x);
% Plot results
plot(x, V);
% Underlying field
field = @(x) [ -x(1).^3 + x(1), -x(2).^3 + x(2)];
% Grid characteristics
x = -1.5:0.1:1.5;
y = -1.5:0.1:1.5;
[xm, ym] = meshgrid(x,y);
% Setting the reference potential
x0 = zeros(1, 2);
V0 = 0;
% Compute the potential
V = Potential(field, V0, x0, xm, ym);
% Plot results
figure; surf(xm, ym, V);
By Pablo Rodríguez-Sánchez, March 2017.