Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Getting an unexpected generator object ? mcgrim Splitter  Posts: 157 Threads: 30 Joined: Mar 2019 Reputation: 0 Likes received: 1 #11 Mar-27-2019, 01:43 PM Thanks, I am also trying to graph the error vs h with a loglog function, but I have two separate issues. loglog is not recognized and when I omit it, the graph I obtain is empty. ```from scipy import * import numpy as np from numpy import array from scipy import integrate import matplotlib.pyplot as plt 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) for n in range (300,900)) print(list(error)) plt.plot((list(error),list(h)) for n in range (300,900)) ``` gontajones Minister of Silly Walks  Posts: 325 Threads: 3 Joined: Jun 2018 Reputation: 23 Likes received: 55 #12 Mar-27-2019, 01:58 PM Importing loglog: ```from matplotlib.pyplot import loglog ``````error = (abs(trapezoidal(f, a, b, n) - c) for n in range(300, 900)) plt.plot(list(error), [(b - a) / n for n in range(300, 900)]) ```From where are you taking this? Maybe with more info we could help you with the Math too. mcgrim Splitter  Posts: 157 Threads: 30 Joined: Mar 2019 Reputation: 0 Likes received: 1 #13 Mar-27-2019, 08:27 PM the graph is still empty and this error message shows up: "have shapes {} and {}".format(x.shape, y.shape)) ValueError: x and y must have same first dimension, but have shapes (0,) and (600,) gontajones Minister of Silly Walks  Posts: 325 Threads: 3 Joined: Jun 2018 Reputation: 23 Likes received: 55 #14 Mar-27-2019, 08:32 PM Try this: ```error_list = [abs(trapezoidal(f, a, b, n) - c) for n in range(300, 900)] plt.plot(error_list, [(b - a) / n for n in range(300, 900)]) ``` mcgrim Splitter  Posts: 157 Threads: 30 Joined: Mar 2019 Reputation: 0 Likes received: 1 #15 Mar-27-2019, 08:51 PM same empty graph, different error message. raise RuntimeError("matplotlib does not support generators " RuntimeError: matplotlib does not support generators as input gontajones Minister of Silly Walks  Posts: 325 Threads: 3 Joined: Jun 2018 Reputation: 23 Likes received: 55 #16 Mar-27-2019, 08:57 PM Please post the code... mcgrim Splitter  Posts: 157 Threads: 30 Joined: Mar 2019 Reputation: 0 Likes received: 1 #17 Mar-27-2019, 09:04 PM ```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) for n in range (300,900)) print(list(error)) plt.plot(error, [h for n in range(300, 900)]) ``` gontajones Minister of Silly Walks  Posts: 325 Threads: 3 Joined: Jun 2018 Reputation: 23 Likes received: 55 #18 Mar-27-2019, 09:50 PM You missed my last post... ```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_list=[abs(trapezoidal(f,a,b,n)-c) for n in range (300,900)] print(error_list) plt.plot(error_list, [h for n in range(300, 900)]) ``` mcgrim Splitter  Posts: 157 Threads: 30 Joined: Mar 2019 Reputation: 0 Likes received: 1 #19 Mar-29-2019, 08:58 AM I see that I forgot the square bracket. Is hard to see sometimes. Thanks. Now the graph is only a horizontal line, does that imply that there is a logical error? gontajones Minister of Silly Walks  Posts: 325 Threads: 3 Joined: Jun 2018 Reputation: 23 Likes received: 55 #20 Mar-29-2019, 11:35 AM I think that `h` should be a function of `n`... But to change it you'll have to think about `trapezoidal(f,a,b,n)` and check if it's really returning what are you expecting. For now... ```from scipy import integrate import matplotlib.pyplot as plt 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_list=[abs(trapezoidal(f,a,b,n)-c) for n in range (300,900)] print(error_list) plt.plot(error_list, [(b-a)/(n) for n in range(300, 900)]) plt.show() ```Like I said, you need to check the Math. Maybe you can use numpy.trapz. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Unexpected Output using classes and inheritance langley 2 118 Jul-04-2019, 09:33 AM Last Post: langley Lottery generator (beginner) Hullari 3 148 Jul-03-2019, 04:46 PM Last Post: perfringo generator function that yield from a list buran 9 424 Jun-04-2019, 10:26 PM Last Post: snippsat syntax error near unexpected token btom529 5 373 Apr-21-2019, 09:03 PM Last Post: btom529 Dictionary adds an unexpected list erina 1 211 Mar-14-2019, 01:13 PM Last Post: ichabod801 unexpected EOF while parsing whatloop 3 563 Mar-09-2019, 06:59 PM Last Post: whatloop Unexpected round behavior pythonCoder 1 194 Feb-19-2019, 02:39 PM Last Post: marienbad Unexpected expected type error result MartinMaker 1 225 Feb-16-2019, 05:02 PM Last Post: micseydel Unexpected ininite loop behavior RedSkeleton007 4 252 Jan-27-2019, 05:32 PM Last Post: aakashjha001 unexpected sub result after overloading operator jolinchewjb 1 262 Jan-24-2019, 08:23 AM Last Post: buran

Forum Jump:

Users browsing this thread: 1 Guest(s)