```python
#Task 18 - Caio Lima
```


```python
# import Python libraries
import numpy as np
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
#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.  
```


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


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



```python
Vdiff2 = sym.expand(sym.diff(Vdiff, x))
roots = sym.solve(Vdiff2, x)
display(Math(sym.latex('Roots:') + sym.latex(roots)))
```


$$Roots:\left [ 2.5\right ]$$



```python
sym.plot((x**3)+(-7.5*x**2)+(18*x)+(-10),(x,0,4.5))
```


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


```python
cur_x = 1              # The algorithm starts at x=1
gamma = 0.01            # step size multiplier
precision = 0.0000001
step_size = 0.1           # initial step size
max_iters = 100000       # maximum number of iterations
iters = 0      


f  = lambda x:(x**3)+(-7.5*x**2)+(18*x)+(-10) # lambda function for f(x)
df = lambda x:(3*x**2)-15*x+18    # lambda function for the gradient of f(x)

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 maximo at {} with function value {}.'.format(2, f(2)))
print('Local maximo by gradient descent at {} with function value {}.'.format(cur_x, f(cur_x)))
```

    True local maximo at 2 with function value 4.0.
    Local maximo by gradient descent at 1.9999968076198855 with function value 3.9999999999847162.


14db8bbf3b7f2b46175175f57b32689e9124b47c
ipynb
Jupyter Notebook
courses/modsim2018/caiolima/Task 18 Caio Lima.ipynb
regifukuchi/bmc-1
f4418212664758511bb3f4d4ca2318ac48a55e88
MIT
courses/modsim2018/caiolima/Task 18 Caio Lima.ipynb
regifukuchi/bmc-1
f4418212664758511bb3f4d4ca2318ac48a55e88
MIT
courses/modsim2018/caiolima/Task 18 Caio Lima.ipynb
regifukuchi/bmc-1
f4418212664758511bb3f4d4ca2318ac48a55e88
MIT
Qwen/Qwen-72B
1. YES
2. YES