-3

I keep getting the error

     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 

This is my code

# [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

One cite suggested I do not use raw_input but rather input, but then I get a similar error.

Beginner Java
  • 65
  • 1
  • 8
  • Note that the use of `hold` is deprecated. Maybe you should inform your instructor about that. – ImportanceOfBeingErnest Jan 24 '18 at 10:07
  • ive edited the question – Beginner Java Jan 25 '18 at 23:54
  • The question is now totally different. Please do not change the complete meaning of a question to which you already got an answer. This question is closed anyways. Just leave it as it is (since you already accepted an answer, you cannot delete it anymore). If you now have a different question, ask a different question. Just mind that the problem you apparently face here, has also already been asked about, so if you were to ask this question now, it would be closed again. – ImportanceOfBeingErnest Jan 26 '18 at 00:01
  • Concerning your problem: The error is self-explanatory: You cannot index a list with a tuple. Instead of A[i,k] you need A[i][k]. – ImportanceOfBeingErnest Jan 26 '18 at 00:02

2 Answers2

0

Why are you using sin(2*pi*t1) when the paper says "sin(5x)"? And why are you using sin(2*2*pi*t2) when the paper says "sin(exp(x))"?

To add multiple plots to same axes:

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

Also, your y-axis on the 2nd plot is wrong.

small changes to your code

SROY
  • 29
  • 3
0

This code works. Note the modifications I have made, with comments alongside. Perhaps you can figure out why the division by 6 is required, in addition to the 5 multiplier.

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()
Dlamini
  • 285
  • 1
  • 9