5
import numpy as np
def romberg(f,l):
    val=np.zeros(l)
    for i in range(0,l):
        h=2**(1-i)
        N=2/h
        val[i]=trapez(f,h,N)
    return val

def trapez(f,h,N):
    result=0                     
    stuetz=np.zeros(N+1);
    for j in range(0,N+1):
        stuetz[j]=0+j/N                  
    sum=0
    for k in range(1,N):                 #bilde summe von 1-N-1
        sum+=np.polyval(f,stuetz[k])
    a=np.polyval(f,0)*0.5*h
    b=(np.polyval(f,stuetz[N]))*0.5*h
    result=a+(h*sum)+b
    return result

main:

p=[1,0,0]
romberg(p,5)

error:

----> 3 romberg(p,5)

----> 7         val[i]=trapez(f,h,N)

TypeError: 'float' object cannot be interpreted as an integer

how can i fix this please help me? i appreciate it!

2
  • 4
    N=int(2/h) in your first function You need N to be an integer to be use in range(), so you have to explicitely tell the program you want N to be an integer. Commented May 29, 2017 at 10:06
  • oh man thank you so much now its working ! :-* Commented May 29, 2017 at 10:54

1 Answer 1

8

change N=2/h to N=2//h to perform integer division.

def romberg(f,l):
    val=np.zeros(l)
    for i in range(0,l):
        h=2**(1-i)
        N=2//h
        val[i]=trapez(f,h,N)
    return val

edit:

You can also use int(2/h)

def romberg(f,l):
    val=np.zeros(l)
    for i in range(0,l):
        h=2**(1-i)
        N=int(2/h)
        val[i]=trapez(f,h,N)
    return val
Sign up to request clarification or add additional context in comments.

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.