赞
踩
[
1
2
3
4
5
6
7
8
9
10
11
12
]
\begin {bmatrix} 1 & 2 &3&4 \\ 5 & 6 &7&8\\ 9&10&11&12 \end{bmatrix} \quad
⎣⎡159261037114812⎦⎤
(1)使用array函数在Python中构建该矩阵
A = np.array(((1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)))
(2)使用arange函数构造该矩阵
A = np.arange(1, 13).reshape(3, 4)
(3)表达式A[2,:]的结果是什么?类似的表达式A[2:]的结果是什么?
[ 9 10 11 12]
[[ 9 10 11 12]]
[
1
1
1
1
1
2
4
8
1
3
9
27
1
4
16
64
]
\begin {bmatrix} 1 & 1 &1&1 \\ 1 & 2 &4&8 \\1&3&9&27 \\1&4&16&64 \end{bmatrix} \quad
⎣⎢⎢⎡1111123414916182764⎦⎥⎥⎤
提示:Numpy模块中可以通过vander命令直接构建
v = np.array([1,2,3,4])
V = np.vander(v, increasing=True)
numpy.vander使用方法
numpy.vander(x, N=None, increasing=False) 生成范德蒙矩阵。
P = v v T v T v 和 Q = I − P P=\frac{vv^T}{v^Tv} 和Q=I-P P=vTvvvT和Q=I−P
v = np.array([[1, -1, 1]]).T
P = np.dot(v,v.T)/np.dot(v.T,v)
I = np.eye(3)
Q = I - P
import random
x = [random.randint(0, 100) for i in range(1000)]
d = set(x)
print(d)
for i in d:
print(i, ':', x.count(i))
import numpy as np
x = np.random.randint(100, size=20)
x[0:10].sort() # 前10个元素升序排列
x[10:20] = abs(np.sort(-x[10:20])) # 后十个元素降序排列
import numpy as np
A = np.array([[9, 80, 205, 40],
[90, -60, 96, 1],
[210, -3, 101, 89]])
for i in range(A.shape[0]):
for j in range(A.shape[1]):
if A[i][j] == max(A[i]) and A[i][j] == max(A[:, j]):
print('鞍点为:', A[i][j])
f = open('problem.txt', 'r', encoding='utf-8') # 为输入打开一个文本文件
s = f.read() # 读取
s = s.swapcase() # 大小写互换
f = open('problem.txt', 'w', encoding='utf-8') # 为输出建立一个新的文本文件
f.write(s)
f.close()
y
=
k
x
2
s
i
n
(
x
)
+
2
k
+
c
o
s
(
x
3
)
,
k
=
1
,
2
,
.
.
.
,
6
y=kx^2sin(x)+2k+cos(x^3),k=1,2,...,6
y=kx2sin(x)+2k+cos(x3),k=1,2,...,6
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-10, 10, 0.01)
plt.figure()
for i in range(6):
k = i + 1
y = k * x ** 2 * np.sin(x) + 2 * k + np.cos(x ** 3)
text = 'k = ' + str(k)
plt.plot(x, y, label=text)
plt.legend()
plt.show()
y
=
k
x
2
s
i
n
(
x
)
+
2
k
+
c
o
s
(
x
3
)
,
k
=
1
,
2
,
.
.
.
,
6
y=kx^2sin(x)+2k+cos(x^3),k=1,2,...,6
y=kx2sin(x)+2k+cos(x3),k=1,2,...,6
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-10, 10, 0.01)
plt.figure()
for i in range(6):
k = i + 1
ax = plt.subplot(2, 3, k)
y = k * x ** 2 * np.sin(x) + 2 * k + np.cos(x ** 3)
text = '$k = ' + str(k) + '$'
plt.plot(x, y, label=text)
plt.legend()
plt.show()
(1)单叶双曲面
x
2
8
+
y
2
10
−
z
2
6
=
1
\frac{x^2}{8}+\frac{y^2}{10}-\frac{z^2}{6}=1
8x2+10y2−6z2=1
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-20, 20, 0.1)
y = np.arange(-20, 20, 0.1)
X, Y = np.meshgrid(x, y)
ax = plt.subplot(1, 1, 1, projection='3d')
Z = (1 - X ** 2 / 8 - Y ** 2 / 10) * 6
ax.plot_surface(X, Y, Z, cmap='viridis')
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')
ax.set_zlabel(r'$z$')
plt.show()
(2)双叶双曲面
x
2
8
−
y
2
12
−
z
2
8
=
1
\frac{x^2}{8}-\frac{y^2}{12}-\frac{z^2}{8}=1
8x2−12y2−8z2=1
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-20, 20, 0.1)
y = np.arange(-20, 20, 0.1)
X, Y = np.meshgrid(x, y)
ax = plt.subplot(1, 1, 1, projection='3d')
Z = (1 - X ** 2 / 8 + Y ** 2 / 12) * 8
ax.plot_surface(X, Y, Z, cmap='rainbow')
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')
ax.set_zlabel(r'$z$')
plt.show()
(3)椭圆抛物面
x
2
10
+
y
2
6
=
z
\frac{x^2}{10}+\frac{y^2}{6}=z
10x2+6y2=z
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-20, 20, 0.1)
y = np.arange(-20, 20, 0.1)
X, Y = np.meshgrid(x, y)
Z = X ** 2 / 10 + Y ** 2 / 6
ax = plt.subplot(1, 1, 1, projection='3d')
ax.plot_surface(X, Y, Z)
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')
ax.set_zlabel(r'$z$')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
ax = plt.axes(projection='3d')
s = np.linspace(-1, 1, 1000)
t = np.linspace(0, 2 * np.pi, 1000)
x = (2 + s / 2 * np.cos(t / 2) * np.cos(t))
y = (2 + s / 2 * np.cos(t / 2) * np.sin(t))
z = s / 2 * np.sin(t / 2)
ax.plot3D(x, y, z, 'k')
plt.show()
(1)
z
=
x
e
−
x
2
−
y
2
,
−
2
≤
x
≤
2
,
−
2
≤
y
≤
3
;
z=xe^{-x^2-y^2},-2\leq x\leq 2,-2\leq y\leq3;
z=xe−x2−y2,−2≤x≤2,−2≤y≤3;
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2, 2, 1000)
y = np.linspace(-2, 3, 2000)
z = np.zeros((len(y), len(x)))
for i in range(len(x)):
for j in range(len(y)):
z[j][i] = x[i] * np.exp(-x[i] ** 2 - y[j] ** 2)
contr = plt.contour(x, y, z)
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.clabel(contr)
plt.show()
(2)
z
=
(
1
−
x
2
−
y
2
)
e
−
y
3
3
,
−
1.5
≤
x
≤
2
,
−
1.5
≤
y
≤
2
z=(1-x^2-y^2)e^{\frac{-y^3}{3}},-1.5\leq x \leq2,-1.5\leq y\leq2
z=(1−x2−y2)e3−y3,−1.5≤x≤2,−1.5≤y≤2
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1.5, 2, 1000)
y = np.linspace(-1.5, 2, 2000)
z = np.zeros((len(y), len(x)))
for i in range(len(x)):
for j in range(len(y)):
z[j][i] = (1-x[i]**2-y[j]**2)*np.exp(-y[j]**3/3)
contr = plt.contour(x, y, z)
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.clabel(contr)
plt.show()
import matplotlib.pyplot as plt import numpy as np import pandas as pd z = np.zeros((1165, 874)) x = np.arange(0, 50 * 874, 50) y = np.arange(0, 50 * 1165, 50) excel = pd.read_excel('附件1:区域高程数据.xlsx', header=None) # 不加索引 excel = excel.drop([874, 875, 876, 877]) # 去掉无用行数据 for i in range(874): # 把excel二维数组存储到Python中 for j in range(1165): z[j][i] = int(excel.iloc[i, j]) ax = plt.axes(projection='3d') X, Y = np.meshgrid(x, y) ax.plot_surface(X, Y, z, cmap='viridis') ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') plt.show() contr = plt.contour(x, y, z) plt.xlabel('x') plt.ylabel('y') plt.clabel(contr) plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。