There are a many different ways to minimize a quadratic energy with constant constraints. I wrote out the exact steps for three such ways:
- Separate the energy into knowns and unknowns, then solve for the
unknowns by setting gradient with respect to those unknowns to zero. - Add additional term to energy that punishes not satisfying the known
values of your constant constraints in the least squares sense, the
solve for all variables by setting gradient with respect to all
variables to zero. - Add additional variables called lagrange multipliers to energy so
that the problem is no longer to find a minimum but a stationary
point in the new energy. Such a stationary point guarantees that our
constraints are met. Find the stationary point by the usual method
of setting the gradient to zero, this time the gradient is with
respect to all original variables and these new variables.
The pretty print type-up with MATLAB pseudocode.
A plain text, unicode version:
Quadratic energy
E=ZT∗Q∗Z+ZT∗L+C
We want to fix some of the values of Z, we can rearrange the Z’s that are still
free and the ones we want to fix so that
Z=[XY]
, where X are the free values
and Y are the fixed values;
Split energy into knowns and unknowns
Z=[XY]
or
Z(unknown) = X and Z(known) = Y
E=XT∗NQ∗X+XT∗NL+NC
where
NQ=Q(unknown,unknown)
NL=Q(unknown,known)∗Y+Q(known,unknown)T∗Y+L(unknown)
NC=YT∗Q(known,known)∗Y+YT∗L(known)+C
∂E∂X=2∗NQ∗X+NL
Solve for X with:
X=(−2∗NQ)−1∗NL
Enforce fixed values via soft contraints with high weights
E=ZT∗Q∗Z+ZT∗L+C
Add new energy term punishing being far from fixed variables
NE=ZT∗Q∗Z+ZT∗L+C+w∗(Z(known)−Y)T∗I∗(Z(known)−Y)
where w is a large number, e.g. 10000
NE=ZT∗Q∗Z+ZT∗L+C+w∗(ZT−[0 YT])∗[000I(known,known)]∗(Z−[0Y])
where W is a diagonal matrix with
Wii=0
if
i∈unknown
and
Wii=w
if
i∈known
NE=ZT∗Q∗Z+ZT∗L+C+(ZT−[0 YT])∗W∗(Z−[0Y])
NE=ZT∗Q∗Z+ZT∗L+C+ZT∗W∗Z−2∗ZT∗W∗[0Y]+[0 YT]∗W∗[0Y]
NE=ZT∗NQ∗Z+ZT∗NL+NC
NQ=Q+W
NL=L−2W∗[0Y]
NC=C+[0 YT]∗W∗[0Y]
Differentiate with respect to
Z
Solve for Z with:
Z=−0.5∗NQ−1∗NL
or re-expanded
Z=−0.5∗(Q+W)−1∗(L−2∗W∗[0Y])
Discard known parts of Z to find X
X=Z(unknown)
But be careful to look at Z(known) - Y to be sure your fixed values are being
met, if not then w is too low. If the X values look like garbage then perhaps
you’re getting numerical error because w is too high.
Lagrange multipliers
We want to minimize
E=ZT∗Q∗Z+ZT∗L+C
subject to the constant constraints:
Z(known)=Y
Find stationary point of new energy:
NE=E+∑λi∗(Zi−Yi)
,i∈known
NE=E+[ZT λT]∗QC∗[Zλ]−[ZT λT]∗[02Y]
(notice the 2 because
λZ
shows up twice in the quadratic part)
where
QC=⎡⎣⎢00000I(known,known)0I(known,known)0⎤⎦⎥
NE=[ZT λT]∗NQ∗[Zλ]+[ZT λT]∗NL+C
NQ=[Q000]+QC=⎡⎣⎢⎢Q012I(known,known)012I(known,known)0⎤⎦⎥⎥
NL=[L0]+[0Y]
Differentiate with respect to all variables, including lagrange multipliers
∂E∂[Zλ]=2∗NQ∗[Zλ]+NL
Solve with
[Zλ]=−0.5∗NQ−1∗NL
Discard fixed values and langrange multipliers.
X = Z(known)
The value of λi is the “force” of the constraint or how hard we had to pull the
energery minimum to meet the constraints. See
http://www.slimy.com/~steuard/teaching/tutorials/Lagrange.html#Meaning