numpy本质¶
向量运算¶
In [1]:
import numpy as np
In [2]:
x = np.linspace(0,10,20)
In [3]:
x
Out[3]:
array([ 0. , 0.52631579, 1.05263158, 1.57894737, 2.10526316, 2.63157895, 3.15789474, 3.68421053, 4.21052632, 4.73684211, 5.26315789, 5.78947368, 6.31578947, 6.84210526, 7.36842105, 7.89473684, 8.42105263, 8.94736842, 9.47368421, 10. ])
In [4]:
y = x ** 2
A = np.array([[140,28],[28,8]])
In [6]:
A
Out[6]:
array([[140, 28], [ 28, 8]])
In [7]:
b = np.array([717,208.5])
In [8]:
np.linalg.inv(A).dot(b)
Out[8]:
array([-0.30357143, 27.125 ])
sinx导数计算¶
In [11]:
import matplotlib.pyplot as plt
In [9]:
x_sinx = np.linspace(0,2 * np.pi,30)
In [10]:
y_sinx = np.sin(x_sinx)
In [12]:
plt.plot(x_sinx,y_sinx)
Out[12]:
[<matplotlib.lines.Line2D at 0x110e70128>]
In [13]:
np.diff(np.array([1,3,5,7,9]))
Out[13]:
array([2, 2, 2, 2])
In [14]:
x_sinx
Out[14]:
array([0. , 0.21666156, 0.43332312, 0.64998469, 0.86664625, 1.08330781, 1.29996937, 1.51663094, 1.7332925 , 1.94995406, 2.16661562, 2.38327719, 2.59993875, 2.81660031, 3.03326187, 3.24992343, 3.466585 , 3.68324656, 3.89990812, 4.11656968, 4.33323125, 4.54989281, 4.76655437, 4.98321593, 5.1998775 , 5.41653906, 5.63320062, 5.84986218, 6.06652374, 6.28318531])
In [15]:
deltax = x_sinx[1] - x_sinx[0]
In [16]:
y_sinx
Out[16]:
array([ 0.00000000e+00, 2.14970440e-01, 4.19889102e-01, 6.05174215e-01, 7.62162055e-01, 8.83512044e-01, 9.63549993e-01, 9.98533414e-01, 9.86826523e-01, 9.28976720e-01, 8.27688998e-01, 6.87699459e-01, 5.15553857e-01, 3.19301530e-01, 1.08119018e-01, -1.08119018e-01, -3.19301530e-01, -5.15553857e-01, -6.87699459e-01, -8.27688998e-01, -9.28976720e-01, -9.86826523e-01, -9.98533414e-01, -9.63549993e-01, -8.83512044e-01, -7.62162055e-01, -6.05174215e-01, -4.19889102e-01, -2.14970440e-01, -2.44929360e-16])
In [18]:
sinx_diff = np.diff(y_sinx) / deltax
In [21]:
plt.plot(x_sinx,y_sinx)
plt.plot(x_sinx[1:],sinx_diff) #思考:应该删除哪一个值?
Out[21]:
[<matplotlib.lines.Line2D at 0x110ee4f28>]
In [ ]: