注解
此笔记本可在此处下载: 0_Scientifc_Python.ipynb
科学 Python
python是一个多功能的环境,但是如果没有一些关键包,它就不能为科学使用提供正确的工具:
NumPy公司(http://www.numpy.org/):显著提高python的数值分析能力。
scipy(https://www.scipy.org/):提供所有经典科学算法。
matplotlib(https://matplotlib.org/gallery/index.html:高质量科学绘图。
熊猫(https://pandas.pydata.org/):快速高效的数据处理
简单介绍:绘制函数
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib nbagg
def func(t, damp = 1., freq = 1., phase = 0.):
"""
The solution of a second order linear ordinary differential equation:
func(t) = exp(-t * damp) * cos(2 * pi * f * t + phase)
Inputs:
* damp: dampening coefficient.
* freq: frequency
* phase: the phase of the signal
Ouput: data as a DataFrame for easier post processing.
"""
return pd.DataFrame( {"a": np.exp(-t * damp) * np.cos(2. * np.pi * freq * t + phase),
"t": t})
t = np.linspace(0., 5., 1001)
data = func(t, damp = .1, freq = 1.)
data.head()
a | t | |
---|---|---|
0 | 1.000000 | 0.000 |
1 | 0.999007 | 0.005 |
2 | 0.997029 | 0.010 |
3 | 0.994070 | 0.015 |
4 | 0.990132 | 0.020 |
plt.figure()
for f in [1., .5, .25]:
data = func(t, damp = .2, freq = f)
plt.plot(data.t, data.a, label = "$f={0}$".format(f))
data = func(t, damp = .2, freq = 0)
plt.plot(data.t, data.a, "k--", label = "Enveloppe")
plt.plot(data.t, -data.a, "k--")
plt.grid()
plt.legend(loc = "best")
plt.xlabel("Time, $t$")
plt.ylabel("Amplitude, $a$")
plt.title(r"A damped harmonic oscillator: $a(t) = \exp(-t d) \cos (2 \pi f t + \phi)$")
plt.show()
<IPython.core.display.Javascript object>
进一步阅读: