Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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 $$(x,y,z)$$,

$$\begin{array}{l}z\le -\left|x\right|-\left|y\right|\\ {x}^{2}+{y}^{2}+(z+1{)}^{2}\le 1.\end{array}$$

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 $$x$$ and $$y$$ value from –1 to 1, and the $$z$$ 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 `showconstr`

.

showconstr(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:

$$energy=\sum _{i<j}\frac{1}{\Vert electron(i)-electron(j)\Vert}.$$

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)

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.

`sol = `*struct with fields:*
x: [10x1 double]
y: [10x1 double]
z: [10x1 double]

fval = 34.1365

exitflag = OptimalSolution

`output = `*struct with fields:*
iterations: 95
funcCount: 2990
constrviolation: 0
stepsize: 1.8867e-06
algorithm: 'interior-point'
firstorderopt: 1.9841e-06
cgiterations: 0
message: '...'
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.