# Local Minima for a function of Two Variables Using Optimality Conditions

Find a local minimum point for the funtion:
$$ f(x) = x_1 + \frac{(4\times 10^6)}{x_1 x_2} + 250x_2$$

See reference for details.

## Solution


```python
import sympy as sp
import numpy as np
from scipy import linalg
from symopt import DiffNotation
```


```python
x1, x2 = sp.symbols(['x1', 'x2'])
```


```python
fx = x1 + (4*10**6) / (x1*x2) + 250*x2
fx
```




$\displaystyle x_{1} + 250 x_{2} + \frac{4000000}{x_{1} x_{2}}$




```python
# Necessary conditions
d1 = DiffNotation(fx, x1, x2)
grad_f = d1.gradient()
grad_f
```




$\displaystyle \left[\begin{matrix}1 - \frac{4000000}{x_{1}^{2} x_{2}}\\250 - \frac{4000000}{x_{1} x_{2}^{2}}\end{matrix}\right]$




```python
# The solution of the system gives the stationary points of the function
sp.nonlinsolve(grad_f, (x1, x2))
```




$\displaystyle \left\{\left( 1000, \  4\right), \left( -500 - 500 \sqrt{3} i, \  -2 - 2 \sqrt{3} i\right), \left( -500 + 500 \sqrt{3} i, \  -2 + 2 \sqrt{3} i\right)\right\}$




```python
# The Hessian of fx is a suficient condition of minimum if it is defined positive
hess_f = d1.hessian()
hess_f
```




$\displaystyle \left[\begin{matrix}\frac{8000000}{x_{1}^{3} x_{2}} & \frac{4000000}{x_{1}^{2} x_{2}^{2}}\\\frac{4000000}{x_{1}^{2} x_{2}^{2}} & \frac{8000000}{x_{1} x_{2}^{3}}\end{matrix}\right]$




```python
# we check the eigenvalues of the hessian at the stationary point (1000, 4)
hess_f_values = np.array(
    hess_f.subs(x1, 1000).subs(x2, 4),
    dtype=float
)
linalg.eig(hess_f_values)
```




    (array([1.499994e-03+0.j, 1.250005e+02+0.j]),
     array([[-0.999998  , -0.00200002],
            [ 0.00200002, -0.999998  ]]))



Eigenvalues positive implies that (1000, 4) is a point of minimum

## Contour plot


```python
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.mlab as mlab

%matplotlib notebook
```


```python
# Contornos
x1_arr = np.linspace(500, 3000, 1000)
x2_arr = np.linspace(2, 14, 1000)
x1_mesh, x2_mesh = np.meshgrid(x1_arr, x2_arr)
fx_mesh = [[x1_ + (4*10**6) / (x1_*x2_) + 250*x2_ 
           for x1_ in x1_arr] 
          for x2_ in x2_arr]
fx_mesh = np.array(fx_mesh)
```


```python
plt.figure()
CS1 = plt.contour(x1_mesh, x2_mesh, fx_mesh, 10)
plt.clabel(CS1, fontsize=9, inline=1)
origin = (1000, 4)
CS2 = plt.plot(*origin, 'ro',label='optimum')

plt.xlabel('x1')
plt.ylabel('x2')
plt.legend()
plt.grid()
plt.show()
#https://matplotlib.org/2.0.2/examples/pylab_examples/contour_demo.html
#https://www.tutorialspoint.com/how-to-plot-vectors-in-python-using-matplotlib
```


    <IPython.core.display.Javascript object>







```python
# The minimum is
fx.subs(x1, 1000).subs(x2, 4)
```




$\displaystyle 3000$



Reference
========
[1] ARORA, Jasbir S. Introduction to Optimum Design. Elservie. 2nd ed. Example 4.23 from page 115.

0647f6f88e17c52aed419039fb2d9bea1c1b2f9e
ipynb
Jupyter Notebook
docs/notebooks/Local_Minima.ipynb
rafaelpsilva07/symopt
0f3e4597c5f4e1956c792154a551af4f5339bd82
MIT
2022-03-13T23:28:14.000Z
2022-03-16T18:14:13.000Z
docs/notebooks/Local_Minima.ipynb
rafaelpsilva07/symopt
0f3e4597c5f4e1956c792154a551af4f5339bd82
MIT
docs/notebooks/Local_Minima.ipynb
rafaelpsilva07/symopt
0f3e4597c5f4e1956c792154a551af4f5339bd82
MIT
Qwen/Qwen-72B
1. YES
2. YES