# Task for lecture 19

## 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
# import Python libraries
import numpy as np
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import sympy as sym
from sympy.plotting import plot
from sympy.interactive import printing
printing.init_printing()
import pandas as pd
from IPython.display import display
from IPython.core.display import Math
```


```python
a, b = sym.symbols('a b')
V = b*(a - 2*b)*(a - 2*b)
Vdiff = sym.expand(sym.diff(V, b))
roots = sym.solve(Vdiff, b)
display(Math(sym.latex('Roots:') + sym.latex(roots)))
```

For a function of one variable, if the maximum or minimum of a function is not at the limits of the domain and if at least the first and second derivatives of the function exist, a maximum and minimum can be found as the point where the first derivative of the function is zero.


```python
# Question 1
x = sym.symbols('x')
y = x**3 -7.5*x**2 + 18*x -10
dy = sym.diff(y, x)
dy
```


```python
roots = sym.solve(dy, x)
display(Math(sym.latex('Roots:') + sym.latex(roots)))
```


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


If the second derivative on that point is positive, then it's a minimum, if it is negative, it's a maximum.
So, let's perform the second derivative.


```python
dy2 = sym.diff(dy, x)
dy2
```


```python
roots2 = sym.solve(dy2, x)
display(Math(sym.latex('Roots:') + sym.latex(roots2)))
```


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


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


```python
# From https://en.wikipedia.org/wiki/Gradient_descent

cur_x = 6               # The algorithm starts at x=6
gamma = 0.01            # step size multiplier
precision = 0.00001
step_size = 1           # initial step size
max_iters = 10000       # maximum number of iterations
iters = 0               # iteration counter

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

    True local minimum at 3 with function value 3.5.
    Local minimum by gradient descent at 3.000323195755751 with function value 3.5000001567170003.


dc003b40bd14f9b992a5654f6559b04288794a69
ipynb
Jupyter Notebook
courses/modsim2018/reginaldo/Task for lecture 19.ipynb
regifukuchi/bmc-1
f4418212664758511bb3f4d4ca2318ac48a55e88
MIT
courses/modsim2018/reginaldo/Task for lecture 19.ipynb
regifukuchi/bmc-1
f4418212664758511bb3f4d4ca2318ac48a55e88
MIT
courses/modsim2018/reginaldo/Task for lecture 19.ipynb
regifukuchi/bmc-1
f4418212664758511bb3f4d4ca2318ac48a55e88
MIT
Qwen/Qwen-72B
1. YES
2. YES