# Lucas_task 19 - Motor Control
### Introduction to modeling and simulation of human movement
https://github.com/BMClab/bmc/blob/master/courses/ModSim2018.md

### 1) Find the extrema in the function f(x)=x3−7.5x2+18x−10 analytically and determine if they are minimum or maximum.

Import Python libraries


```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
```

Finding the roots


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


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


Finding the minimum and maximum


```python
x1 = 2
x2 = 3
F1 = x1**3 - 7.5*x1**2 + 18*x1 - 10
F2 = x2**3 - 7.5*x2**2 + 18*x2 - 10

print("F(x1): ", F1)
print("\nF(x2): ", F2)


```

    F(x1):  4.0
    
    F(x2):  3.5



```python
print("Minimim:", x2,"\nMaximum:", x1)

```

    Minimim: 3 
    Maximum: 2


### 2) Find the minimum in the f(x)=x3−7.5x2+18x−10 using the gradient descent algorithm.


```python
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(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.000323195755751 with function value 3.5000001567170003.



```python

```

6ff82daa7871fb4b8aa286ee302113eecaf49f9b
ipynb
Jupyter Notebook
courses/modsim2018/tasks/Lucas_task19.ipynb
regifukuchi/bmc-1
f4418212664758511bb3f4d4ca2318ac48a55e88
MIT
courses/modsim2018/tasks/Lucas_task19.ipynb
regifukuchi/bmc-1
f4418212664758511bb3f4d4ca2318ac48a55e88
MIT
courses/modsim2018/tasks/Lucas_task19.ipynb
regifukuchi/bmc-1
f4418212664758511bb3f4d4ca2318ac48a55e88
MIT
Qwen/Qwen-72B
1. YES
2. YES