Python課程程式碼實現

Bonstoppo發表於2018-09-21

1.Numpy

#-*- coding:utf-8 -*-
import numpy as np
a = np.array([2 , 0 , 1 , 5])
print( a )
print(a.min())
a.sort()#sort可以記錄儲存資料
print(a[:3])
print(a)
b = np.array([[1 ,2 ,3] , [4 , 5 , 6]])
print(b * b)

2.Scipy

#-*- coding:utf-8 -*-
from scipy.optimize import fsolve
def f(x):
    x1 = x[0]
    x2 = x[1]
    return [2 * x1 - x2 ** 2 - 1 , x1 ** 2 - x2 - 2]

result = fsolve(f , [1 , 1])
print(result)

from scipy import integrate
def g(x):
    return (1 - x ** 2) ** 0.5

pi_2 , err = integrate.quad(g , -1 , 1)//辛普森自適應函式,在[-1 , 1]之間求g(x)的定積分//
print(err)

3.Matplotlib

#-*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0 , 10 , 1000)
y = np.sin(x) + 1
z = np.cos(x ** 2) + 1

plt.figure(figsize = (8 , 4))
plt.plot(x , y , label = '$\sin x+1$' , color = 'red' , linewidth = 2)
plt.plot(x , z , 'b--' ,  label = '$\cos x^2+1$' )
plt.xlabel('Time(s)')
plt.ylabel('Volt')
plt.title('A Simple Example')
plt.ylim(0 , 2.2)
plt.legend()
plt.show()

相關文章