```python
import numpy as np
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import sympy as sym
from sympy.plotting import plot
import pandas as pd
from IPython.display import display
from IPython.core.display import Math
```


```python
# EXERCISE 1.Find the extrema in the function f(x)=x^3-7.5x^2+18x-10 
# analytically and determine if they are minimum or maximum.

a = sym.symbols('a')
V = a**3 - 7.5*a**2 + 18*a - 10
Vdiff = sym.expand(sym.diff(V))
roots = sym.solve(Vdiff)
display(Math(sym.latex('Roots:') + sym.latex(roots)))
roots = np.asarray(roots)
```


$$Roots:\left [ 2.0, \quad 3.0\right ]$$



```python
def f(x):
    return x**3 - 7.5*x**2 + 18*x - 10

x = sym.symbols('x') # x como simbolo para realizar a derivada

fdiff = sym.diff(f(x),x)  #derivada

fdiff2 = sym.diff(fdiff, x)

for i in range(len(roots)):

    while fdiff2.evalf(subs ={x: roots[i]}) == 0: # fdiff.evalf(subs ={x: 0}) colocar 0 no lugar do simbolo x // caso a derivada der 0
        fdiff2 = sym.diff(fdiff2,x)

    print(fdiff2.evalf(subs ={x: roots[i]}))  # mostrar o valor da derivada se =/= 0
    
    if fdiff2.evalf(subs ={x: roots[i]}) <= 0: # determinar maxima ou minima
        print('maxima')
    else:
        print('minima')
```

    -3.00000000000000
    maxima
    3.00000000000000
    minima



```python
# EXERCISE 2. Find the minimum in the f(x)=x^3-7.5x^2+18x-10 
# using the gradient descent algorithm.

cur_x = 6
gamma = 0.01
precision = 0.00001
step_size = 1
max_iters = 10000
iters = 0

f = lambda x: x**3 - 7.5*x**2 + 18*x - 10
df = lambda x: 3*x**2 - 15*x + 18

while (step_size > precision) & (iters < max_iters):
    prev_x = cur_x
    cur_x -= gamma*df(prev_x)
    step_size - abs(cur_x - prev_x)
    iters+=1
    
print('True local minimum at {} with function value {}.'.format(9/4, f(9/4)))
print('Local minimum by gradient descent at {} with function value {}.'.format(cur_x, f(cur_x)))
```

    True local minimum at 2.25 with function value 3.921875.
    Local minimum by gradient descent at 3.000000000000008 with function value 3.5.



```python

```

4372adc7aae10c4a1824abdf43f48c80dd76d2f5
ipynb
Jupyter Notebook
courses/modsim2018/matheuspiquini/Optmization - 1.ipynb
MatheusKP/bmc
5c9c426eeb49b2b686d2da9e5ac092e695428345
MIT
courses/modsim2018/matheuspiquini/Optmization - 1.ipynb
MatheusKP/bmc
5c9c426eeb49b2b686d2da9e5ac092e695428345
MIT
courses/modsim2018/matheuspiquini/Optmization - 1.ipynb
MatheusKP/bmc
5c9c426eeb49b2b686d2da9e5ac092e695428345
MIT
Qwen/Qwen-72B
1. YES
2. YES