0

I was trying out a program for legendre's function as given below, but it showed some error. I'm not used to arrays in Python, but know them in C++.

from math import *

j = 0
arr = [0 for i  in range (6)]
k= 3.75
arr[0]= 1
arr[1] = 1 
x0= -1
xf = 1
x= x0
h= 0.1

f1 = open('leg.dat', 'w')

while x< xf:
    for j in range(0,5):
        arr[j+2]= (arr[j] *(j*j + j -k)/((j+2)*(j+1)))
        print >>f1, x,(x**j)*(((j+2)(j+1)*arr[j]) - (j*(j-1)*arr[j]) - (2*j*arr[j]) + k*arr[j])
        x = x+h

f1.close ()

error shown :

print>>f1, x,(x**j)*(((j+2)(j+1)*arr[j]) - (j*(j-1)*arr[j]) - (2*j*arr[j]) + k*arr[j])

TypeError: 'int' object is not callable

2 Answers 2

4

Here's the problem:

(j+2)(j+1)

Python is trying to call the j+2 with j+1 as argument.

>>> j = 1
>>> (j+2)(j+1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable

Did you mean (j+2)*(j+1)?

Sign up to request clarification or add additional context in comments.

2 Comments

oops! got it! the program i'm trying to write is of legendre's function upto 4 terms and draw its graph. but even when i corrected that, the data points are giving 0 for every value of x. can u pls suggest a correction?
After removing writing to a file (and printing to stdout instead) your code produces the following output on my machine: -1 5.75 -0.9 -6.975 -0.8 -11.7 -0.7 1.17548958333 Traceback (most recent call last): File "55pkt.py", line 16, in <module> arr[j+2]= (arr[j] (jj + j -k)/((j+2)*(j+1))) IndexError: list assignment index out of range Are you experiencing the same results?
1
print >>f1, x,(x**j)*(((j+2)(j+1)*arr[j]) - (j*(j-1)*arr[j]) - (2*j*arr[j]) + k*arr[j])
                        ^^^  ^^^

You didn't specify an operation between these two values. Did you mean to multiply them?

print >>f1, x,(x**j)*(((j+2)*(j+1)*arr[j]) - (j*(j-1)*arr[j]) - (2*j*arr[j]) + k*arr[j])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.