mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-01 05:51:01 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
%matplotlib inline
|
||||
@@ -13,7 +13,7 @@ import matplotlib.pyplot as plt
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
np.set_printoptions(precision=2, suppress=True)
|
||||
|
||||
```
|
||||
@@ -22,7 +22,7 @@ np.set_printoptions(precision=2, suppress=True)
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
data = np.genfromtxt("JANAF_CH4.txt",
|
||||
delimiter="\t", # TAB 分隔
|
||||
skiprows=1, # 忽略首行
|
||||
@@ -36,14 +36,14 @@ data = np.genfromtxt("JANAF_CH4.txt",
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
for row in data[:7]:
|
||||
print "{}\t{}".format(row['TK'], row['Cp'])
|
||||
print "...\t..."
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
0.0 0.0
|
||||
100.0 33.258
|
||||
200.0 33.473
|
||||
@@ -59,7 +59,7 @@ print "...\t..."
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
p = plt.plot(data['TK'], data['Cp'], 'kx')
|
||||
t = plt.title("JANAF data for Methane $CH_4$")
|
||||
a = plt.axis([0, 6000, 30, 120])
|
||||
@@ -250,20 +250,20 @@ ZmaWKwcWMzPLlQOLmZnl6n8BKYzjzfpoiFsAAAAASUVORK5CYII=
|
||||
|
||||
先导入一维插值函数 `interp1d`:
|
||||
|
||||
```
|
||||
```py
|
||||
interp1d(x, y)
|
||||
```
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.interpolate import interp1d
|
||||
|
||||
```
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
ch4_cp = interp1d(data['TK'], data['Cp'])
|
||||
|
||||
```
|
||||
@@ -274,14 +274,14 @@ ch4_cp = interp1d(data['TK'], data['Cp'])
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
ch4_cp(382.2)
|
||||
|
||||
```
|
||||
|
||||
Out[8]:
|
||||
|
||||
```
|
||||
```py
|
||||
array(39.565144000000004)
|
||||
```
|
||||
|
||||
@@ -289,14 +289,14 @@ array(39.565144000000004)
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
ch4_cp([32.2,323.2])
|
||||
|
||||
```
|
||||
|
||||
Out[9]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ 10.71, 36.71])
|
||||
```
|
||||
|
||||
@@ -304,12 +304,12 @@ array([ 10.71, 36.71])
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
ch4_cp(8752)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
ValueError Traceback (most recent call last)
|
||||
<ipython-input-10-5d727af9aa33> in <module>()
|
||||
@@ -342,7 +342,7 @@ ValueError: A value in x_new is above the interpolation range.
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
ch4_cp = interp1d(data['TK'], data['Cp'],
|
||||
bounds_error=False)
|
||||
|
||||
@@ -352,14 +352,14 @@ ch4_cp = interp1d(data['TK'], data['Cp'],
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
ch4_cp(8752)
|
||||
|
||||
```
|
||||
|
||||
Out[12]:
|
||||
|
||||
```
|
||||
```py
|
||||
array(nan)
|
||||
```
|
||||
|
||||
@@ -367,7 +367,7 @@ array(nan)
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
ch4_cp = interp1d(data['TK'], data['Cp'],
|
||||
bounds_error=False, fill_value=-999.25)
|
||||
|
||||
@@ -375,14 +375,14 @@ ch4_cp = interp1d(data['TK'], data['Cp'],
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
ch4_cp(8752)
|
||||
|
||||
```
|
||||
|
||||
Out[14]:
|
||||
|
||||
```
|
||||
```py
|
||||
array(-999.25)
|
||||
```
|
||||
|
||||
@@ -399,7 +399,7 @@ array(-999.25)
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
T = np.arange(100,355,5)
|
||||
plt.plot(T, ch4_cp(T), "+k")
|
||||
p = plt.plot(data['TK'][1:7], data['Cp'][1:7], 'ro', markersize=8)
|
||||
@@ -514,7 +514,7 @@ SSViqEtSiRjqklQihroklcj/A8xgms4BB0nlAAAAAElFTkSuQmCC
|
||||
|
||||
In [16]:
|
||||
|
||||
```
|
||||
```py
|
||||
cp_ch4 = interp1d(data['TK'], data['Cp'], kind="nearest")
|
||||
p = plt.plot(T, cp_ch4(T), "k+")
|
||||
p = plt.plot(data['TK'][1:7], data['Cp'][1:7], 'ro', markersize=8)
|
||||
@@ -612,7 +612,7 @@ iKEuSQUx1CWpIIa6JBXk/wGLsxItFlRd2gAAAABJRU5ErkJggg==
|
||||
|
||||
In [17]:
|
||||
|
||||
```
|
||||
```py
|
||||
cp_ch4 = interp1d(data['TK'], data['Cp'], kind="zero")
|
||||
p = plt.plot(T, cp_ch4(T), "k+")
|
||||
p = plt.plot(data['TK'][1:7], data['Cp'][1:7], 'ro', markersize=8)
|
||||
@@ -707,7 +707,7 @@ xFCXpIIY6pJUEENdkgpiqEtSQf4fzDOJu6Be93AAAAAASUVORK5CYII=
|
||||
|
||||
In [18]:
|
||||
|
||||
```
|
||||
```py
|
||||
cp_ch4 = interp1d(data['TK'], data['Cp'], kind="quadratic")
|
||||
p = plt.plot(T, cp_ch4(T), "k+")
|
||||
p = plt.plot(data['TK'][1:7], data['Cp'][1:7], 'ro', markersize=8)
|
||||
@@ -810,7 +810,7 @@ qEtShRjqklQh/w+L/6C1jqXsbQAAAABJRU5ErkJggg==
|
||||
|
||||
In [19]:
|
||||
|
||||
```
|
||||
```py
|
||||
cp_ch4 = interp1d(data['TK'], data['Cp'], kind="cubic")
|
||||
p = plt.plot(T, cp_ch4(T), "k+")
|
||||
p = plt.plot(data['TK'][1:7], data['Cp'][1:7], 'ro', markersize=8)
|
||||
@@ -916,7 +916,7 @@ AElFTkSuQmCC
|
||||
|
||||
In [20]:
|
||||
|
||||
```
|
||||
```py
|
||||
cp_ch4 = interp1d(data['TK'], data['Cp'], kind=4)
|
||||
p = plt.plot(T, cp_ch4(T), "k+")
|
||||
p = plt.plot(data['TK'][1:7], data['Cp'][1:7], 'ro', markersize=8)
|
||||
@@ -1025,7 +1025,7 @@ AABJRU5ErkJggg==
|
||||
|
||||
In [21]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.interpolate import interp2d, interpnd
|
||||
|
||||
```
|
||||
@@ -1042,7 +1042,7 @@ from scipy.interpolate import interp2d, interpnd
|
||||
|
||||
$$\Phi(x,c) = \Phi(\|x-c\|)$$In [22]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = np.linspace(-3,3,100)
|
||||
|
||||
```
|
||||
@@ -1053,7 +1053,7 @@ x = np.linspace(-3,3,100)
|
||||
|
||||
In [23]:
|
||||
|
||||
```
|
||||
```py
|
||||
plt.plot(x, np.exp(-1 * x **2))
|
||||
t = plt.title("Gaussian")
|
||||
|
||||
@@ -1230,7 +1230,7 @@ wKmQi4gUOBVyEZEC9/8BloffFHVXiTcAAAAASUVORK5CYII=
|
||||
|
||||
In [24]:
|
||||
|
||||
```
|
||||
```py
|
||||
plt.plot(x, np.sqrt(1 + x **2))
|
||||
t = plt.title("Multiquadric")
|
||||
|
||||
@@ -1408,7 +1408,7 @@ JJwSuYhIwv0/gG56R7Cwt98AAAAASUVORK5CYII=
|
||||
|
||||
In [25]:
|
||||
|
||||
```
|
||||
```py
|
||||
plt.plot(x, 1. / np.sqrt(1 + x **2))
|
||||
t = plt.title("Inverse Multiquadric")
|
||||
|
||||
@@ -1619,7 +1619,7 @@ $$ f(x) = \sum_j n_j \Phi(\|x-x_j\|) $$
|
||||
|
||||
In [26]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.interpolate.rbf import Rbf
|
||||
|
||||
```
|
||||
@@ -1628,7 +1628,7 @@ from scipy.interpolate.rbf import Rbf
|
||||
|
||||
In [27]:
|
||||
|
||||
```
|
||||
```py
|
||||
cp_rbf = Rbf(data['TK'], data['Cp'], function = "multiquadric")
|
||||
plt.plot(data['TK'], data['Cp'], 'k+')
|
||||
p = plt.plot(data['TK'], cp_rbf(data['TK']), 'r-')
|
||||
@@ -1793,7 +1793,7 @@ SJJR8IuIJBkFv4hIklHwi4gkGQW/iEiSUfCLiCSZ/wfUE99TkRdDtgAAAABJRU5ErkJggg==
|
||||
|
||||
In [28]:
|
||||
|
||||
```
|
||||
```py
|
||||
cp_rbf = Rbf(data['TK'], data['Cp'], function = "gaussian")
|
||||
plt.plot(data['TK'], data['Cp'], 'k+')
|
||||
p = plt.plot(data['TK'], cp_rbf(data['TK']), 'r-')
|
||||
@@ -1986,7 +1986,7 @@ wgAAAABJRU5ErkJggg==
|
||||
|
||||
In [29]:
|
||||
|
||||
```
|
||||
```py
|
||||
cp_rbf = Rbf(data['TK'], data['Cp'], function = "inverse_multiquadric")
|
||||
plt.plot(data['TK'], data['Cp'], 'k+')
|
||||
p = plt.plot(data['TK'], cp_rbf(data['TK']), 'r-')
|
||||
@@ -2153,7 +2153,7 @@ iCQZBb+ISJJR8IuIJJn/D8ptq7/3lHsDAAAAAElFTkSuQmCC
|
||||
|
||||
In [30]:
|
||||
|
||||
```
|
||||
```py
|
||||
from mpl_toolkits.mplot3d import Axes3D
|
||||
|
||||
```
|
||||
@@ -2162,7 +2162,7 @@ from mpl_toolkits.mplot3d import Axes3D
|
||||
|
||||
In [31]:
|
||||
|
||||
```
|
||||
```py
|
||||
x, y = np.mgrid[-np.pi/2:np.pi/2:5j, -np.pi/2:np.pi/2:5j]
|
||||
z = np.cos(np.sqrt(x**2 + y**2))
|
||||
|
||||
@@ -2170,7 +2170,7 @@ z = np.cos(np.sqrt(x**2 + y**2))
|
||||
|
||||
In [32]:
|
||||
|
||||
```
|
||||
```py
|
||||
fig = plt.figure(figsize=(12,6))
|
||||
ax = fig.gca(projection="3d")
|
||||
ax.scatter(x,y,z)
|
||||
@@ -2179,7 +2179,7 @@ ax.scatter(x,y,z)
|
||||
|
||||
Out[32]:
|
||||
|
||||
```
|
||||
```py
|
||||
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x176b4da0>
|
||||
```
|
||||
|
||||
@@ -2921,14 +2921,14 @@ IbFKEARBEARBqJb/D6nRM2cdX17QAAAAAElFTkSuQmCC
|
||||
|
||||
In [33]:
|
||||
|
||||
```
|
||||
```py
|
||||
zz = Rbf(x, y, z)
|
||||
|
||||
```
|
||||
|
||||
In [34]:
|
||||
|
||||
```
|
||||
```py
|
||||
xx, yy = np.mgrid[-np.pi/2:np.pi/2:50j, -np.pi/2:np.pi/2:50j]
|
||||
fig = plt.figure(figsize=(12,6))
|
||||
ax = fig.gca(projection="3d")
|
||||
@@ -2938,7 +2938,7 @@ ax.plot_surface(xx,yy,zz(xx,yy),rstride=1, cstride=1, cmap=plt.cm.jet)
|
||||
|
||||
Out[34]:
|
||||
|
||||
```
|
||||
```py
|
||||
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x176e5c50>
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user