在Python上使用matplotlib绘制图形

gev0vcfq  于 2023-05-01  发布在  Python
关注(0)|答案(1)|浏览(150)

我在for循环中接收到变量,我想绘制这些变量以观察方法的收敛性。

x_b = exp(pi/sqrt(3))
def C(x,y):
    C1 = cos(sqrt(3)/2 * log(x))/sqrt(x)
    C2 = cos(sqrt(3)/2 * log(y))/sqrt(y)
    C3 = sin(sqrt(3)/2 * log(x))/sqrt(x)
    C4 = sin(sqrt(3)/2 * log(y))/sqrt(y)
    return np.matrix('{} {};{} {}'.format(C1, C3, C2, C4))
matrix = C(x_a, x_b)
Y_a = 1
Y_b = 2
def Y(x,y):
    Y1 = x
    Y2 = y
    return np.matrix('{};{}'.format(Y1, Y2))
Y = Y(Y_a, Y_b)
inverse_array = np.linalg.inv(matrix)
C = np.dot(inverse_array, Y)
C1 = 1
C2 = 2 * exp(pi/(2*sqrt(3)))
k = 1/(exp(pi/sqrt(3))-1)
print("k:")
print(k)
b = 1 - k
W = pi/(exp(pi/sqrt(3))-1)
B = pi - W

这里我们计算矩阵的变量

X_1 = x_a + (x_b - x_a)/5
X_2 = X_1 + (x_b - x_a)/5
X_3 = X_2 + (x_b - x_a)/5
X_4 = X_3 + (x_b - x_a)/5
X_5 = X_4 + (x_b - x_a)/5
A11 = sin(1 * (W * X_1 + B)) * (1 / (X_1 * X_1) - 1 * 1 * W * W)
A12 = sin(1 * (W * X_2 + B)) * (1 / (X_2 * X_2) - 1 * 1 * W * W)
A13 = sin(1 * (W * X_3 + B)) * (1 / (X_3 * X_3) - 1 * 1 * W * W)
A14 = sin(1 * (W * X_4 + B)) * (1 / (X_4 * X_4) - 1 * 1 * W * W)
A21 = sin(2 * (W * X_1 + B)) * (1 / (X_1 * X_1) - 2 * 2 * W * W)
A22 = sin(2 * (W * X_2 + B)) * (1 / (X_2 * X_2) - 2 * 2 * W * W)
A23 = sin(2 * (W * X_3 + B)) * (1 / (X_3 * X_3) - 2 * 2 * W * W)
A24 = sin(2 * (W * X_4 + B)) * (1 / (X_4 * X_4) - 2 * 2 * W * W)
A31 = sin(3 * (W * X_1 + B)) * (1 / (X_1 * X_1) - 3 * 3 * W * W)
A32 = sin(3 * (W * X_2 + B)) * (1 / (X_2 * X_2) - 3 * 3 * W * W)
A33 = sin(3 * (W * X_3 + B)) * (1 / (X_3 * X_3) - 3 * 3 * W * W)
A34 = sin(3 * (W * X_4 + B)) * (1 / (X_4 * X_4) - 3 * 3 * W * W)
A41 = sin(4 * (W * X_1 + B)) * (1 / (X_1 * X_1) - 4 * 4 * W * W)
A42 = sin(4 * (W * X_2 + B)) * (1 / (X_2 * X_2) - 4 * 4 * W * W)
A43 = sin(4 * (W * X_3 + B)) * (1 / (X_3 * X_3) - 4 * 4 * W * W)
A44 = sin(4 * (W * X_4 + B)) * (1 / (X_4 * X_4) - 4 * 4 * W * W)
A_matrix = np.matrix('{} {} {} {};{} {} {} {};{} {} {} {};{} {} {} {}'.format(A11, A21, A31, A41, A12, A22, A32, A44, A13, A23, A33, A43, A14, A24, A34, A44))
inverse_matrix_A = np.linalg.inv(A_matrix)
def K(x, y, z):
    k = -(x/y + z/(y*y))
    return k
K_matrix = np.matrix('{};{};{};{}'.format(K(k, X_1, b), K(k, X_2, b), K(k, X_3, b), K(k, X_4, b)))
A = np.dot(inverse_matrix_A, K_matrix)
vals = np.arange(x_a, x_b, 0.1)

基本上这就是问题产生的地方,我用方法计算精确的和解,我想画出它们的图形,但是我得到了一个错误

kol, tochnoe, Eps = [], [], []
for i in vals:
    fi_0 = k*i+b
    fi_1 = A[0]*sin(1*(W*i+B))
    fi_2 = A[1]*sin(2*(W*i+B))
    fi_3 = A[2]*sin(3*(W*i+B))
    fi_4 = A[3]*sin(4*(W*i+B))
    kol.append(fi_0 + fi_1 + fi_2 + fi_3 + fi_4)
    tochnoe.append(C1 * cos(sqrt(3) / 2 * log(i))/sqrt(i) + C2 * sin(sqrt(3) / 2 * log(i))/sqrt(i))
    Eps.append(tochnoe[-1] - kol[-1])

但我得到了

r1wp621o

r1wp621o1#

因为你画的是一个点,而不是一条线
尝试如下:

vals = np.arange(x_a, x_b, 0.1)
kol, tochnoe, Eps = [], [], []
for i in vals:
    fi_0 = k*i+b
    fi_1 = (A[0]*sin(1*(W*i+B)))[0,0]
    fi_2 = (A[1]*sin(2*(W*i+B)))[0,0]
    fi_3 = (A[2]*sin(3*(W*i+B)))[0,0]
    fi_4 = (A[3]*sin(4*(W*i+B)))[0,0]
    kol.append(fi_0 + fi_1 + fi_2 + fi_3 + fi_4)
    tochnoe.append(C1 * cos(sqrt(3) / 2 * log(i))/sqrt(i) + C2 * sin(sqrt(3) / 2 * log(i))/sqrt(i))
    Eps.append(tochnoe[-1] - kol[-1])
        tochnoe.append(C1 * cos(sqrt(3) / 2 * log(i))/sqrt(i) + C2 * sin(sqrt(3) / 2 * log(i))/sqrt(i))
        Eps.append(tochnoe[-1] - kol[-1])

plt.plot(vals, kol, label='kol')
plt.plot(vals, tochnoe, label='tochnoe')
plt.plot(vals, Eps, label='Eps')
plt.legend()
plt.show()

相关问题