Consider the electrostatics problem of placing 10 electrons in a conducting body. The electrons will arrange themselves in a way that minimizes their total potential energy, subject to the constraint of lying inside the body. All the electrons are on the boundary of the body at a minimum. The electrons are indistinguishable, so the problem has no unique minimum (permuting the electrons in one solution gives another valid solution). This example was inspired by Dolan, Moré, and Munson [1].
For an equivalent solver-based example using Symbolic Math Toolbox™, see Symbolic Math Toolbox™ Calculates Gradients and Hessians.
This example involves a conducting body defined by the following inequalities. For each electron with coordinates ,
These constraints form a body that looks like a pyramid on a sphere. To view the body, enter the following code.
[X,Y] = meshgrid(-1:.01:1); Z1 = -abs(X) - abs(Y); Z2 = -1 - sqrt(1 - X.^2 - Y.^2); Z2 = real(Z2); W1 = Z1; W2 = Z2; W1(Z1 < Z2) = nan; % only plot points where Z1 > Z2 W2(Z1 < Z2) = nan; % only plot points where Z1 > Z2 hand = figure; % handle to the figure, since we'll plot more later set(gcf,'Color','w') % white background surf(X,Y,W1,'LineStyle','none'); hold on surf(X,Y,W2,'LineStyle','none'); view(-44,18)
A slight gap exists between the upper and lower surfaces of the figure. This gap is an artifact of the general plotting routine used to create the figure. The routine erases any rectangular patch on one surface that touches the other surface.
The problem has ten electrons. The constraints give bounds on each and value from –1 to 1, and the value from –2 to 0. Define the variables for the problem.
N = 10; x = optimvar('x',N,'LowerBound',-1,'UpperBound',1); y = optimvar('y',N,'LowerBound',-1,'UpperBound',1); z = optimvar('z',N,'LowerBound',-2,'UpperBound',0); elecprob = optimproblem;
The problem has two types of constraints. The first, a spherical constraint, is a simple polynomial inequality for each electron separately. Define this spherical constraint.
elecprob.Constraints.spherec = (x.^2 + y.^2 + (z+1).^2) <= 1;
The preceding constraint command creates a vector of ten constraints. View the constraint vector using show
.
show(elecprob.Constraints.spherec)
((x.^2 + y.^2) + (z + 1).^2) <= arg_RHS where: arg2 = 1; arg1 = arg2([1 1 1 1 1 1 1 1 1 1]); arg_RHS = arg1(:);
The second type of constraint in the problem is linear. You can express the linear constraints in different ways. For example, you can use the abs
function to represent an absolute value constraint. To express the constraints this way, write a MATLAB function and convert it to an expression using fcn2optimexpr
. For a different approach, write the absolute value constraint as four linear inequalities. Each constraint command returns a vector of ten constraints.
elecprob.Constraints.plane1 = z <= -x-y; elecprob.Constraints.plane2 = z <= -x+y; elecprob.Constraints.plane3 = z <= x-y; elecprob.Constraints.plane4 = z <= x+y;
The objective function is the potential energy of the system, which is a sum over each electron pair of the inverse of their distances:
Define the objective function as an optimization expression.
energy = optimexpr(1); for ii = 1:(N-1) for jj = (ii+1):N tempe = (x(ii) - x(jj))^2 + (y(ii) - y(jj))^2 + (z(ii) - z(jj))^2; energy = energy + tempe^(-1/2); end end elecprob.Objective = energy;
Start the optimization with the electrons distributed randomly on a sphere of radius 1/2 centered at [0,0,–1].
rng default % For reproducibility x0 = randn(N,3); for ii=1:N x0(ii,:) = x0(ii,:)/norm(x0(ii,:))/2; x0(ii,3) = x0(ii,3) - 1; end init.x = x0(:,1); init.y = x0(:,2); init.z = x0(:,3);
Solve the problem by calling solve
.
[sol,fval,exitflag,output] = solve(elecprob,init)
Solving problem using fmincon. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance. <stopping criteria details>
sol = struct with fields:
x: [10×1 double]
y: [10×1 double]
z: [10×1 double]
fval = 34.1365
exitflag = OptimalSolution
output = struct with fields:
iterations: 94
funcCount: 2959
constrviolation: 0
stepsize: 7.0129e-06
algorithm: 'interior-point'
firstorderopt: 2.0944e-06
cgiterations: 0
message: '↵Local minimum found that satisfies the constraints.↵↵Optimization completed because the objective function is non-decreasing in ↵feasible directions, to within the value of the optimality tolerance,↵and constraints are satisfied to within the value of the constraint tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The relative first-order optimality measure, 6.083643e-07,↵is less than options.OptimalityTolerance = 1.000000e-06, and the relative maximum constraint↵violation, 0.000000e+00, is less than options.ConstraintTolerance = 1.000000e-06.↵↵'
solver: 'fmincon'
Plot the solution as points on the conducting body.
figure(hand) plot3(sol.x,sol.y,sol.z,'r.','MarkerSize',25) hold off
The electrons are distributed fairly evenly on the constraint boundary. The electrons are mainly on edges and the pyramid point.
[1] Dolan, Elizabeth D., Jorge J. Moré, and Todd S. Munson. “Benchmarking Optimization Software with COPS 3.0.” Argonne National Laboratory Technical Report ANL/MCS-TM-273, February 2004.