接受 python 中的矩阵

accepting a matrix in python

我一直收到错误

     13    for k in range(0,n-1):
     14         for i in range(k+1,n):
---> 15             if A[i,k] != 0.0:
     16                 lAm = A [i,k]/A[k,k]
     17                 A[i,k+1:n] = A[i,k+1:n] - lAm*A[k,k+1:n]
TypeError: list indices must be integers, not tuple 

这是我的代码

# [L][U] = Lr([A])
    #doolittles decomp
    n = int(raw_input("Provide n: "))
    A = [];
    for rowi in range(n):
        row_list = list(map(float, raw_input("row {}: ".format(rowi +1)).split(',')))
        A.append(row_list)
    print(A)  


    def Lr(A):
        n = len(A)
        for k in range(0,n-1):
            for i in range(k+1,n):
                if A[i,k] != 0.0:
                    lAm = A [i,k]/A[k,k]
                    A[i,k+1:n] = A[i,k+1:n] - lAm*A[k,k+1:n]
                    A[i,k] = lAm
        return A

有人引用建议我不使用 raw_input 而是输入,但后来我得到了类似的错误。

为什么论文说 "sin(5x)" 时你用 sin(2*pi*t1)?当论文说 "sin(exp(x))" 时,你为什么要使用 sin(2*2*pi*t2)

要将多个绘图添加到同一轴:

ax1 = fig.add_subplot(211)
ax1.plot(x, y)
ax1.plot(x2, y2)

此外,您在第二个图上的 y 轴是错误的。

small changes to your code

此代码有效。请注意我所做的修改,并附上评论。或许你能弄清楚为什么除了 5 乘数之外还需要除以 6。

from matplotlib.pyplot import figure, show
from numpy import arange, sin, pi, cos, exp #cos, exp added

t1 = arange(0.0, 1.0, 0.01)
t2 = arange(-3.0,3.0,0.01)

fig = figure(1)

ax1 = fig.add_subplot(211)
ax1.plot(t1, sin(5*2*pi*t1/6))  #modified
ax1.plot(t1, cos(5*2*pi*t1/6)) #Added
ax1.grid(True)
ax1.set_ylim((-2, 2))
ax1.set_ylabel('y')
ax1.set_xlabel('x')
ax1.set_title('y = sin(5x) und y = cos(5x)')

ax2 = fig.add_subplot(212)
#ax2.plot(t2, sin(2*2*pi*t2))   #muted
ax2.plot(t2, sin(2*pi*exp(t2)/6))   #Added

ax2.set_ylim((-2, 2))
ax2.set_xlabel('x')
ax2.set_ylabel('y')

show()