Python在循环中绘制线条

时间:2017-10-23 21:02:42

标签: python loops matplotlib

我需要在Kachmarz算法的循环中绘制每个迭代。但是当我尝试时有一个错误:

ValueError: x and y can be no greater than 2-D, but have shapes (2, 1, 1) and (2,)

Threre是我的代码(我们有矩阵A,每行都有一个平面,它必须通过Kaczmarz算法解决Ax = f并为其设置动画):

import os
import matplotlib.pyplot as plt 
import numpy as np
from numpy import linalg as la

x = np.linspace(-100, 15, 100)
A = np.matrix([[2, 3], [1, -1],[6,  1]]);
f = np.matrix([4, 2, 15]);
resh = la.pinv(A).dot(np.transpose(f))

e1 = (1 - 2*x)/3;
e2 = (x - 2);
e3 = 15 - 6*x;

plt.plot(x, e1, color="black")
plt.plot(x, e2, color="black")
plt.plot(x, e3, color="black")
plt.axis([-0, 4, -4, 4])

[m , n] = np.shape(A)

precision = 0.01
iteration = 100
lmbd = 0.8

nrm=[]
for i in range(0, m):
   nrm.append(la.norm(A[i][:]));

approx = np.zeros((n, 1))
predx  =

 np.zeros((n, 1))
print(np.shape(approx))
for i in range(0, iteration):
    j = (i-1) % m
    predx = approx
        approx = approx + np.multiply((f[:,j] - A[j,:].dot(approx)),(np.transpose(A[j, :])))
    plt.plot([predx[0], approx[0]], [predx[1], 2], "r-")
    # c = plt.plot([predx[1], 2], [predx[2], 2], "g-")
    # pause(0.01)
    if(la.norm(predx - approx) <= precision):
        break

plt.show()

我不知道如何修复它,最后它应该是这样的:Kaczmarz animation

我如何解决这个循环?我做错了什么?

0 个答案:

没有答案