## Learn Calculus with Python 

#### start


```python
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

x = np.arange(0,100,0.1)
y = np.cos(x)

plt.plot(x,y)
plt.show()
```

#### normal function 


```python
import numpy as np
import matplotlib.pyplot as plt
```


```python
def f(x):
    return x**3 / (25)

f(30)
f(10)
```




    1080.0






    40.0




```python
# num -> smooth ( how many dots )
x = np.linspace(-30,30,num=1000) 
y = f(x)

plt.plot(x,y)
```

#### exp


```python
def exp(x):
    return np.e**x


exp(2)
np.exp(2)


xz = np.linspace(1, 20, num=100)
yz = exp(xz)

plt.plot(xz, yz)
```


```python
def exp2(x):
    sum = 0
    for k in range(100):
        sum += float(x**k)/np.math.factorial(k)
    return sum

exp2(1)
exp(1)
```




    2.7182818284590455






    2.718281828459045



#### log


```python
# pick 100 items between 1 and 500 isometricly
x = np.linspace(1,500,100,endpoint=False)

y1 = np.log2(x)
y2 = np.log(x)
y3 = np.log10(x)

plt.plot(x,y1,'red',x,y2,'green',x,y3,'blue')
```

#### trigonometric


```python
pi_val = np.pi
pi_range = np.linspace(-2*pi_val,2*pi_val )

plt.plot(
    pi_range,
    np.sin(pi_range)
)

plt.plot(
    pi_range,
    np.cos(pi_range)
)
```

#### f(g(x))


```python
import numpy as np
import matplotlib.pyplot as plt
```


```python
f = lambda x:x+20
g = lambda x:x**2
h = lambda x:f(g(x))

x = np.array(range(-30,30))
plt.plot(x,h(x),'bs')
```

#### f<sup>-1</sup>(x)


```python
w = lambda x: x**2
winv = lambda x: np.sqrt(x)

x = np.linspace(0,2,100)

plt.plot(x,w(x),'b',x,winv(x),'r',x,x,'g-.')
```

#### *higher order functions*


```python
def horizontal_shift(f,W):
    return lambda x: f(x-W)

g = lambda x:x**2

x = np.linspace(-20,20,100)
shifted_g = horizontal_shift(g,5)

plt.plot(x,g(x),'b',x,shifted_g(x),'r')
```

<hr>

#### Euler's Formula


```python
# 即'欧拉公式'

rules_of_imaginary_number = '''
i^0 = 1   i^1 = i   i^2 = -1   i^3 = -i
i^4 = 1   i^5 = i   i^6 = -1   i^7 = -i '''

euler_equation = '''
e^(i*x) = cos(x) + i*sin(x) 
e^(i*pi) + 1 = 0  <= (if x=pi) '''

# --- sympy ---

from sympy import Symbol,expand,E,I

z = Symbol('z',real=True)
expand(E**(I*z),complex=True)

# --- numpy ---

x = np.linspace(-np.pi,np.pi)
lhs = np.e**(1j*x)
rhs = np.cos(x) + 1j*np.sin(x)

sum(lhs==rhs)
len(x)
```




    I*sin(z) + cos(z)






    50






    50



#### Higher Derivatives


```python
from sympy.abc import x,y

f = x**4

f.diff(x,2)
f.diff(x).diff(x)
```




    12*x**2






    12*x**2



#### Ordinary Differential Equations


```python
import numpy as np
import matplotlib.pyplot as plt
from sympy import *


t = Symbol('t')
c = Symbol('c')
domain = np.linspace(-3,3,100)

v = t**3-3*t-6
a = v.diff()
```


```python
for p in np.linspace(-2,2,20):
    slope = a.subs(t,p)
    intercept = solve(slope*p+c-v.subs(t,p),c)[0]
    lindomain = np.linspace(p-1,p+1,20)
    plt.plot(lindomain,slope*lindomain+intercept,'red',linewidth=1)
    
plt.plot(domain,[v.subs(t,i) for i in domain],linewidth=3)
```

<hr>

4aaba1677e740955f86a2230c3c6cde86bf20bd2
ipynb
Jupyter Notebook
learn-calc-with-python.ipynb
codingEzio/code_python_learn_math
bd7869d05e1b4ec250cc5fa13470a960b299654e
Unlicense
learn-calc-with-python.ipynb
codingEzio/code_python_learn_math
bd7869d05e1b4ec250cc5fa13470a960b299654e
Unlicense
learn-calc-with-python.ipynb
codingEzio/code_python_learn_math
bd7869d05e1b4ec250cc5fa13470a960b299654e
Unlicense
Qwen/Qwen-72B
1. YES
2. YES