Week 6: Extrema and Optimization#
Demo by Christian Mikkelstrup, Hans Henrik Hermansen, Jakob Lemvig, Karl Johan Måstrup Kristensen and Magnus Troen.
from sympy import *
from dtumathtools import *
init_printing()
When one is about to investigate extrema of a function \(f:\Omega \to \mathbb{R}\), then there are in principle three things to investigate. In Theorem 5.2.2 these are described as follows:
\(\boldsymbol{x}_0 \in \partial \Omega \cap \Omega\) (that is, minimum and/or maximum can be a point on the boundary of the domain \(\Omega\))
\(\boldsymbol{x}_0\) is a point where \(f\) is not differentiable (called an exceptional point in the following)
\(\boldsymbol{x}_0\) is a point where \(f\) is differentiable and \(\nabla f(\boldsymbol{x}_0) = 0\) (called a stationary point)
Local Extrema, Example 1#
We will try to find all extremum points of a function \(f: \mathbb{R}^2 \to \mathbb{R}\) given by:
We notice that \(f\) is defined and differentiable at all points in \(\mathbb{R}^2\). So, there are no boundary points nor exceptional points to investigate.
Hence, we can do with just investigating the stationary points, that are given by the solution to the equations:
x,y = symbols("x y", real=True)
f = x**3 - 3 * x**2 + y**3 - 3 * y**2
eq1 = Eq(f.diff(x),0)
eq2 = Eq(f.diff(y),0)
display(eq1,eq2)
![../_images/a8ae2d19c1f0108fd7c4126d56363108d88c7b86f3930196df0392348db7e8ee.png](../_images/a8ae2d19c1f0108fd7c4126d56363108d88c7b86f3930196df0392348db7e8ee.png)
![../_images/68c8e4e0660277c151f9e4f95b047986e0d26e716b31a9876b1e23b632c29a4c.png](../_images/68c8e4e0660277c151f9e4f95b047986e0d26e716b31a9876b1e23b632c29a4c.png)
These equations are non-linear, but are still even so solved rather easily, either by hand or using SymPy:
sols = nonlinsolve([eq1, eq2], [x,y])
sols
![../_images/474f1091b180ed3157928aeb6444f83a49d36e104a68d35c5226c9647ef283f6.png](../_images/474f1091b180ed3157928aeb6444f83a49d36e104a68d35c5226c9647ef283f6.png)
Now we have our stationary points. We can then find the partial derivatives of second order and create the Hessian matrix.
H = dtutools.hessian(f)
H
Next, we insert the stationary points, and the eigenvalues of \(\boldsymbol{H}_f\) are investigated, which is easy since \(H\) already is diagonalized, and the eigenvalues thus can be read directly from the matrix.
[{(x0, y0): H.subs([(x,x0),(y,y0)])} for (x0,y0) in sols]
Theorem 5.2.4 now tells us that \(f\) at the point:
\((0,0)\) attains a strict (local) maximum, since both eigenvalues are negative
\((2,2)\) attains a strict (local) minimum, since both eigenvalues are positive
\((0,2)\) is a saddle point, since the eigenvalues have opposite signs
\((2,0)\) is a saddle point of the same reason as for \((0,2)\)
A saddle point is a stationary point that is not a local extremum point. Note that we are talking about local extremum values here. The function has no maximum or minimum values since it can be shown that \(\mathrm{im}(f)=]-\infty,\infty[\).
# Show points
list_of_stationary_points = [Matrix([x0,y0,f.subs([(x,x0),(y,y0)])]) for (x0,y0) in sols]
points = dtuplot.scatter(list_of_stationary_points, show=False, rendering_kw={"s" : 100,"color":"red"})
# Show the surface f with the stationary points
pf = dtuplot.plot3d(f,(x,-0.8,2.8),(y,-0.8,2.8),use_cm=True, colorbar=False,show=False,wireframe=True,rendering_kw={"alpha":0.6})
pf.camera = {"azim" : -50, "elev" : 30}
pf.extend(points)
pf.show()
![../_images/33f0fc361033b9ec0838a063c66af2110ad95e602aa48b2db8740650f01457fe.png](../_images/33f0fc361033b9ec0838a063c66af2110ad95e602aa48b2db8740650f01457fe.png)
Local Extrema, Example 2#
We now consider a function \(f:\mathbb{R}^2 \to \mathbb{R}\) given by
The domain is the entire \(\mathbb{R}^2\), which means that there are no boundary points nor exceptional points to investigate. We will thus just be investigating the stationary points.
x,y = symbols('x y', real=True)
f = x ** 4 + 4 * x**2 * y ** 2 + y ** 4 - 4 * x ** 3 - 4 * y ** 3 + 2
eq1 = Eq(f.diff(x),0)
eq2 = Eq(f.diff(y),0)
eq1,eq2,f
![../_images/5646da87eac0e1d7923a33edadb4d00a411c1a51c683949185a526e4db51c2c6.png](../_images/5646da87eac0e1d7923a33edadb4d00a411c1a51c683949185a526e4db51c2c6.png)
sols = nonlinsolve([eq1,eq2], [x, y])
sols
![../_images/b693e6e2f32657d0ab8f48003342cdca261eed03ad84d2f610ea32001141db49.png](../_images/b693e6e2f32657d0ab8f48003342cdca261eed03ad84d2f610ea32001141db49.png)
[(N(xsol),N(ysol)) for (xsol,ysol) in sols]
![../_images/b0e24c266b89c081932b959d55a74a793dd21ebec6d4941770a286fbb5f88cc7.png](../_images/b0e24c266b89c081932b959d55a74a793dd21ebec6d4941770a286fbb5f88cc7.png)
\(\verb|nonlinsolve()|\) does not take into account how we have defined our variables, so eventhough we have tried to define \(x,y\) with \(\verb|real=True|\), we must use our heads outselves as well. We can here see that four (real) stationary points are found:
stat_points = list(sols)[:-2] # Remove the last two elements (complex solutions) with list-slicing
stat_points
![../_images/5a1ef4c1b3fe180ee02355c74413d40a1735fc33f6c647fec330a7ab1dfccb9a.png](../_images/5a1ef4c1b3fe180ee02355c74413d40a1735fc33f6c647fec330a7ab1dfccb9a.png)
Let us determine the Hessian matrix to be able to investigate the points.
H = dtutools.hessian(f)
H
We insert the stationary points, and since \(\boldsymbol{H}_f\) is not diagonalized this time as in the previous example, we will ask SymPy for the eigenvalues:
Hessian_matrices = [H.subs([(x,x0),(y,y0)]) for x0,y0 in stat_points]
Eig_Hessian_matrices = [h.eigenvals() for h in Hessian_matrices]
display(*zip(stat_points,Eig_Hessian_matrices))
![../_images/66e60b5a34a93f55cff0c5482a3b6c1380d3a65eedda89ada56691c428f01764.png](../_images/66e60b5a34a93f55cff0c5482a3b6c1380d3a65eedda89ada56691c428f01764.png)
![../_images/e1cf21e365127787cfa77eaa4fcfdfff05d29b1e2854b2f7e5696c3d38cf7836.png](../_images/e1cf21e365127787cfa77eaa4fcfdfff05d29b1e2854b2f7e5696c3d38cf7836.png)
![../_images/e5b4521d07b631d8521496ed12c79f44bdbcca82328d4193a59c87f654cff1ad.png](../_images/e5b4521d07b631d8521496ed12c79f44bdbcca82328d4193a59c87f654cff1ad.png)
![../_images/dafce6536af73c3549a69b208edda373bb7b7a44dfd8a95e9b4b1e1ff67fd2fa.png](../_images/dafce6536af73c3549a69b208edda373bb7b7a44dfd8a95e9b4b1e1ff67fd2fa.png)
From this we read that \(f\) has strict minimum at both \((0,3)\) and \((3,0)\). we can see that \((1,1)\) is a saddle point and not an extremum, since we here have both a positive and a negative eigenvalue.
Since \(\boldsymbol{H}_f((0,0))\) has at least one eigenvalue which is zero, we have to perform a special investigation of the point \((0,0)\). Let us try to see how \(f\) behaves along the line \(y=x\).
dtuplot.plot(f.subs(y,x),2,(x,-0.5,1.2),axis_center="auto")
![../_images/e05264ef367620e40a973ec0ea64f8eb3b82eab94db53a84d3d342c99933a727.png](../_images/e05264ef367620e40a973ec0ea64f8eb3b82eab94db53a84d3d342c99933a727.png)
<spb.backends.matplotlib.matplotlib.MatplotlibBackend at 0x7f5734044610>
Already here it is clear that \((0,0)\) neither is a minimum nor a maximum point. We can investigate this in a bit more detail:
tmp = f.subs(y,x) - f.subs([(x,0),(y,0)])
display(tmp, tmp.factor())
![../_images/399e4aac434172290fa58f042320442e1d89cccba2475039dae810560becff89.png](../_images/399e4aac434172290fa58f042320442e1d89cccba2475039dae810560becff89.png)
![../_images/e6b90a88d64f681b863dda208f5182b1b25bd1e399c0d02a0efcf43bd9f07488.png](../_images/e6b90a88d64f681b863dda208f5182b1b25bd1e399c0d02a0efcf43bd9f07488.png)
From this we see that
\(f(x,x) - f(0,0) > 0\) when \(x < 0\) and \(f(x,x) - f(0,0) < 0\), when \(0 < x < \frac{4}{3}\).
Since \(f(x,x)\) attains values that are both larger and smaller than \(f(0,0)\) for \((x,x)\) arbitrarily close to \((0,0)\), then \(f\) cannot have extremum at \((0,0)\).
Let us see the graph of \(f\) along with the stationary points.
list_of_stationary_points = [Matrix([x0,y0,f.subs([(x,x0),(y,y0)])]) for (x0,y0) in stat_points]
points = dtuplot.scatter(list_of_stationary_points, show=False, rendering_kw={"color":"red", "s" : 30})
pf = dtuplot.plot3d(f,(x,-2, 5),(y,-2, 4),xlim=(-2,5),ylim=(-2,5),zlim=(-30,15), show=False, rendering_kw={"alpha":0.5})
pf.camera = {"azim" : -161, "elev" : 5}
(pf + points).show()
![../_images/ed54703d1d2a4e00f19466bff7c1434b2e2945c5b9593084a1fe57fd78818b50.png](../_images/ed54703d1d2a4e00f19466bff7c1434b2e2945c5b9593084a1fe57fd78818b50.png)
Global Extrema on Bounded and Closed Set, Example 1#
We now consider a function \(f:\Omega \to \mathbb{R}\) with the expression
Again, \(f\) is well-defined on the entire \(\mathbb{R}^2\), but this time the domain is bounded by
meaning the set that is enclosed by the lines \(y = -1\), \(y = 1-x\), and \(x=0\).
M = dtuplot.plot_implicit(Eq(x,0),Eq(y,1-x),Eq(y,-1), (1-x >= y) & (y >= -1) & (x >= 0),(x,-0.1,2.1),(y,-1.1,1.1),show=False)
M.legend = False
M.show()
The provided expression contains Boolean functions. In order to plot the expression, the algorithm automatically switched to an adaptive sampling.
![../_images/fa01f7d0baec5c8b062ba9a2509a758223a897225a6284ebdd65a40433a5a939.png](../_images/fa01f7d0baec5c8b062ba9a2509a758223a897225a6284ebdd65a40433a5a939.png)
We notice that \(f\) is differentiable on all of \(\Omega\), so there are no exceptional points to take into account. Hence it is also continuous on all of \(\Omega\). Since \(\Omega\) is closed and bounded, \(f\) has a global minimum and maximum on \(\Omega\). The two values will be attained either at stationary points or on the boundary.
Since \(\Omega\) is a connected set, the image/range is \(f(\Omega) = [m;M]\), where \(m\) is the global minimum and \(M\) the global maximum.
Let us first have a look at \(f\)’s level curves:
f = 3 + x - x ** 2 - y ** 2
niveau = dtuplot.plot_contour(f,(x,-0.1,2.1),(y,-1.1,1.1),show=False)
niveau.extend(dtuplot.plot_implicit(Eq(x,0),Eq(y,1-x),Eq(y,-1),(x,-0.1,2.1),(y,-1.1,1.1),show=False))
niveau.show()
![../_images/f816b8a78e6f2cdc6c9e8c0c344d10ac06cab120792dfd4d897f7624d59c9e67.png](../_images/f816b8a78e6f2cdc6c9e8c0c344d10ac06cab120792dfd4d897f7624d59c9e67.png)
Let us now compute the stationary points.
eq1 = Eq(f.diff(x),0)
eq2 = Eq(f.diff(y),0)
sols = nonlinsolve([eq1,eq2], [x,y])
sols
![../_images/81c999e33af797d98331cf85d3331b224297f73ac290f19c3060182bae20981f.png](../_images/81c999e33af797d98331cf85d3331b224297f73ac290f19c3060182bae20981f.png)
We see that the point is found in \(\Omega\), and is thus a relevant stationary point. Let us plot the restriction of \(f\) to each of the three boundary lines. After this, we will find the stationary points on each of these boundary lines.
dtuplot.plot(f.subs(y, -1),(x,0,2), title="Boundary line f(x, -1)")
![../_images/482985892a0a4b59452012436e91c5b5467081765f2dee9a27de8da0bcd7fe53.png](../_images/482985892a0a4b59452012436e91c5b5467081765f2dee9a27de8da0bcd7fe53.png)
<spb.backends.matplotlib.matplotlib.MatplotlibBackend at 0x7f5733d007f0>
dtuplot.plot(f.subs(y, 1-x),(x,0,2), title="Boundary line f(x, 1-x)")
![../_images/c372180cabe165da1fe70164a01a0d7a0c543053b76aa43c3b299fb8faf254f4.png](../_images/c372180cabe165da1fe70164a01a0d7a0c543053b76aa43c3b299fb8faf254f4.png)
<spb.backends.matplotlib.matplotlib.MatplotlibBackend at 0x7f57342bea60>
dtuplot.plot(f.subs(x,0), (y,-1,1), ylim = (0,3),aspect="equal", title="Boundary line f(0, y)")
![../_images/328b59408ae74d9ef5428829f41449c4ad913a802fa45f0666d6c727832dd6ea.png](../_images/328b59408ae74d9ef5428829f41449c4ad913a802fa45f0666d6c727832dd6ea.png)
<spb.backends.matplotlib.matplotlib.MatplotlibBackend at 0x7f573694ea60>
stat_points = set(sols)
vertical = solve(f.subs(x,0).diff(y))
horizontal = solve(f.subs(y,-1).diff(x))
atanangle = solve(f.subs(y,1-x).diff(x))
stat_points.update(set([(0,y0) for y0 in vertical]))
stat_points.update(set([(x0,-1) for x0 in horizontal]))
stat_points.update(set([(x0,1-x0) for x0 in atanangle]))
stat_points
![../_images/d9b68079c1c48928151834ce08c00765a73f2c0dc2649ab88ce02ba20e4392d2.png](../_images/d9b68079c1c48928151834ce08c00765a73f2c0dc2649ab88ce02ba20e4392d2.png)
Now we just have to find maximum and minimum between the found stationary points and the end points of the boundary lines, så \((0,1)\), \((0,-1)\), and \((2,-1)\)
investigation_points = list(stat_points) + [(0,1),(0,-1),(2,-1)]
f_values = [f.subs([(x,x0),(y,y0)]) for x0,y0 in investigation_points]
minimum = min(f_values)
maximum = max(f_values)
f_values, minimum, investigation_points[f_values.index(minimum)], maximum, investigation_points[f_values.index(maximum)]
![../_images/7ee73a319874c753c2c28ba34b1d41e763dfb34f1856f84e91a5e3606e8ceab5.png](../_images/7ee73a319874c753c2c28ba34b1d41e763dfb34f1856f84e91a5e3606e8ceab5.png)
Now we finally have:
Global minimum: \(0\) at the point \((2,-1)\)
Global maximum: \(\frac{13}{4}\) at the point \((\frac{1}{2},0)\)
Image: \([0,\frac{13}{4}]\)
Let us plot everything all together:
u = symbols("u")
pf = dtuplot.plot3d(f,(x,0,2),(y,-1,1),show=False,rendering_kw={"alpha":0.7})
points = dtuplot.scatter([Matrix([2,-1,0]),Matrix([1/2,0,13/4])],show=False,rendering_kw={"color":"red","s":20})
l1 = dtuplot.plot3d_parametric_line(u,-1,f.subs({x:u,y:-1}),(u,0,2),use_cm=False,show=False,rendering_kw={"color":"red","linewidth":2})
l2 = dtuplot.plot3d_parametric_line(0,u,f.subs({x:0,y:u}),(u,-1,1),use_cm=False,show=False,rendering_kw={"color":"red","linewidth":2})
l3 = dtuplot.plot3d_parametric_line(u,1-u,f.subs({x:u,y:1-u}),(u,0,2),use_cm=False,show=False,rendering_kw={"color":"red","linewidth":2})
combined = (pf + points + l1 + l2 + l3)
combined.camera = {"azim" : 148, "elev" : 40}
combined.legend = False
combined.show()
![../_images/1dfc5aefeaafb077176d3019a09e6433f8a5162546d249f4fc627c86206b1394.png](../_images/1dfc5aefeaafb077176d3019a09e6433f8a5162546d249f4fc627c86206b1394.png)