Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Getting an unexpected generator object ? mcgrim Spam, spam, eggs, and spam Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #1 Mar-27-2019, 12:33 PM (This post was last modified: Mar-27-2019, 12:58 PM by Yoriz. Edited 1 time in total. Edit Reason: Title ) This program should find the difference between the integral of a specific function and the number obtained with the trapezoid formula. I expect to get some values within a range so I can graph them, but instead this is what I am getting: generator object at 0x000001A1B1A66F48 ```a=0 b=1 n=500 h=(b-a)/(n) def f(x): return x**2 def trapezoidal(f,a,b,n): return (h/2)*(f(a)+f(b))+h*( sum(f(a+h*i) for i in range(1, (n-1))) ) print(trapezoidal(f,a,b,n)) lambda x: f(x) c= integrate.quad(f,0,1) print(c) error=(abs(trapezoidal-c) for n in range (300,900)) print(error) ``` buran Posts: 4,986 Threads: 89 Joined: Sep 2016 Reputation: 350 Likes received: 1078 #2 Mar-27-2019, 01:03 PM line 24 - using () brackets makes it generator expression use square brackets and it will be list comprehension (i.e. if you want to see the numbers when print) mcgrim Spam, spam, eggs, and spam Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #3 Mar-27-2019, 01:03 PM (This post was last modified: Mar-27-2019, 01:05 PM by Yoriz. Edited 1 time in total.) sorry, I think I posted this in the wrong forum. Yoriz wrote Mar-27-2019, 01:05 PM:It's ok I moved it Yoriz Posts: 1,148 Threads: 28 Joined: Sep 2016 Reputation: 99 Likes received: 259 #4 Mar-27-2019, 01:04 PM Try `print(list(error))`And then look up generator expressions mcgrim Spam, spam, eggs, and spam Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #5 Mar-27-2019, 01:10 PM I changed () to [] on line 24 and added 'list' to 25 but now I get an error "unsupported operand type(s) for -: 'function' and 'tuple'" here is the new code: ```a=0 b=1 n=500 h=(b-a)/(n) def f(x): return x**2 def trapezoidal(f,a,b,n): return (h/2)*(f(a)+f(b))+h*( sum(f(a+h*i) for i in range(1, (n-1))) ) print(trapezoidal(f,a,b,n)) lambda x: f(x) c= integrate.quad(f,0,1) print(c) error=[abs(trapezoidal-c) for n in range (200,900) ] print(list(error)) ``` gontajones Minister of Silly Walks Posts: 330 Threads: 3 Joined: Jun 2018 Reputation: 23 Likes received: 55 #6 Mar-27-2019, 01:12 PM ```from scipy import integrate a=0 b=1 n=500 h=(b-a)/(n) def f(x): return x**2 def trapezoidal(f,a,b,n): return (h/2)*(f(a)+f(b))+h*( sum(f(a+h*i) for i in range(1, (n-1)))) print(trapezoidal(f,a,b,n)) # Why this? lambda x: f(x) c= integrate.quad(f,0,1) print(c) error=(abs(trapezoidal(f,a,b,n)-c[0]) for n in range (300,900)) for idx, i in enumerate(error): print(idx, i) ``` mcgrim Spam, spam, eggs, and spam Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #7 Mar-27-2019, 01:13 PM and by the way, this thread is still here. Are you sure you moved it? Yoriz Posts: 1,148 Threads: 28 Joined: Sep 2016 Reputation: 99 Likes received: 259 #8 Mar-27-2019, 01:31 PM I moved it here because the issue is a general coding issue not data science mcgrim Spam, spam, eggs, and spam Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #9 Mar-27-2019, 01:36 PM thank you. I have changed line 24-25 with this code ```error=(abs(trapezoidal(f,a,b,n)-c[0]) for n in range (300,900)) print(list(error)) ```and it works, however, I am not understanding the meaning of c[0], I just notice that if I write just 'c' I get an error. What does the 0 stand for? It looks like is the first element of a list, but c is the outcome of the integral so I am not sure what is the relationship between the integral and the list. gontajones Minister of Silly Walks Posts: 330 Threads: 3 Joined: Jun 2018 Reputation: 23 Likes received: 55 #10 Mar-27-2019, 01:40 PM (This post was last modified: Mar-27-2019, 01:41 PM by gontajones.) In fact `c` is the return of the `integrate.quad()` from scipy.integrate.quad. And it returns a Tuple: ```>>> c= integrate.quad(f,0,1) >>> print(c) (0.33333333333333337, 3.700743415417189e-15) ```So you need to get the first element of this tuple (index=0) to get only the integral of function `f`. BTW I'm just checking the code...not the math! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post receive from a generator, send to a generator Skaperen 9 1,315 Feb-05-2018, 06:26 AM Last Post: Skaperen

Forum Jump:

Users browsing this thread: 1 Guest(s)