```python
import scipy as sp
import sympy
import numpy as np
import scipy as sp
import sklearn as sk

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D

%matplotlib inline

```

    /home/kang/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:8: UserWarning: 
    This call to matplotlib.use() has no effect because the backend has already
    been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
    or matplotlib.backends is imported for the first time.
    
    The backend was *originally* set to 'module://ipykernel.pylab.backend_inline' by the following code:
      File "/home/kang/anaconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main
        "__main__", mod_spec)
      File "/home/kang/anaconda3/lib/python3.6/runpy.py", line 85, in _run_code
        exec(code, run_globals)
      File "/home/kang/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
        app.launch_new_instance()
      File "/home/kang/anaconda3/lib/python3.6/site-packages/traitlets/config/application.py", line 657, in launch_instance
        app.initialize(argv)
      File "<decorator-gen-123>", line 2, in initialize
      File "/home/kang/anaconda3/lib/python3.6/site-packages/traitlets/config/application.py", line 87, in catch_config_error
        return method(app, *args, **kwargs)
      File "/home/kang/anaconda3/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 462, in initialize
        self.init_gui_pylab()
      File "/home/kang/anaconda3/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 403, in init_gui_pylab
        InteractiveShellApp.init_gui_pylab(self)
      File "/home/kang/anaconda3/lib/python3.6/site-packages/IPython/core/shellapp.py", line 207, in init_gui_pylab
        r = enable(key)
      File "/home/kang/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2930, in enable_matplotlib
        pt.activate_matplotlib(backend)
      File "/home/kang/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py", line 307, in activate_matplotlib
        matplotlib.pyplot.switch_backend(backend)
      File "/home/kang/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 229, in switch_backend
        matplotlib.use(newbackend, warn=False, force=True)
      File "/home/kang/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py", line 1305, in use
        reload(sys.modules['matplotlib.backends'])
      File "/home/kang/anaconda3/lib/python3.6/importlib/__init__.py", line 166, in reload
        _bootstrap._exec(spec, module)
      File "/home/kang/anaconda3/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 14, in <module>
        line for line in traceback.format_stack()
    
    
      



```python
def f2p(x) : 
    return -400.0 * x[0] * (-x[0] ** 2 + x[1]) + 2*x[0] - 2
```


```python
x
```


```python
result = sp.optimize.minimize(f2, (-2, -2), jac = f2p)
print(result)
```


```python
x, y = sympy.symbols(' x y ')
f2 = (1 - x) ** 2 + 100 * (y - x ** 2) ** 2
f2
```




    (-x + 1)**2 + 100*(-x**2 + y)**2




```python
sympy.diff(f2, x )
```




    -400*x*(-x**2 + y) + 2*x - 2




```python
f2 = 
```


```python
def f2(x) :
    return (1 - x[0]) ** 2 + 100.0 * (x [1] - x [0] ** 2) ** 2
```


```python
def f2p(x) :
    return -400 * x[0] * ( -x[0] ** 2 + x[1]) + 2 * x[0] - 2
```


```python
def f2g(x):
    """gradient of f2(x)"""
    return np.array((2.0 * (x[0] - 1) - 400.0 * x[0] * (x[1] - x[0]**2), 200.0 * (x[1] - x[0]**2)))
```


```python
result = sp.optimize.minimize(f2, (3, 3), jac = f2g)
print(result)
x0 = result['x']
x0
```

          fun: 7.880245049711645e-20
     hess_inv: array([[ 0.48216528,  0.9656648 ],
           [ 0.9656648 ,  1.93889528]])
          jac: array([ -1.11665241e-08,   5.53674884e-09])
      message: 'Optimization terminated successfully.'
         nfev: 58
          nit: 43
         njev: 58
       status: 0
      success: True
            x: array([ 1.,  1.])





    array([ 1.,  1.])




```python
def f1logs(x) :
    return np.log(x[0]) + np.log(x[1])

def eq_constraint(x) :
    return x[0] + x[1] - 1


sp.optimize.fmin_slsqp(f1logs, np.array([(1, 1)]) , eqcons = [eq_constraint])
```

    Optimization terminated successfully.    (Exit mode 0)
                Current function value: -1.3862943611198901
                Iterations: 2
                Function evaluations: 8
                Gradient evaluations: 2





    array([ 0.5,  0.5])




```python

```


```python
def f2(x) : 
    return np.sqrt((x[0] - 3))
```

fe9833ed2d8304d3b1d2073bc540ccd7448c504f
ipynb
Jupyter Notebook
Mathmatics/180201_optimization_basic.ipynb
KANG91/DSS_lecture_note
dc52aff6870146cc7b7ef0af8fd0a0887bc2734c
MIT
Mathmatics/180201_optimization_basic.ipynb
KANG91/DSS_lecture_note
dc52aff6870146cc7b7ef0af8fd0a0887bc2734c
MIT
Mathmatics/180201_optimization_basic.ipynb
KANG91/DSS_lecture_note
dc52aff6870146cc7b7ef0af8fd0a0887bc2734c
MIT
Qwen/Qwen-72B
1. YES
2. YES