Python Forum
Getting an unexpected generator object ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting an unexpected generator object ?
#11
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[0]) for n in range (300,900))
print(list(error))
plt.plot((list(error),list(h)) for n in range (300,900))
Reply
#12
Importing loglog:
from matplotlib.pyplot import loglog
error = (abs(trapezoidal(f, a, b, n) - c[0]) 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.
Reply
#13
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,)
Reply
#14
Try this:

error_list = [abs(trapezoidal(f, a, b, n) - c[0]) for n in range(300, 900)]
plt.plot(error_list, [(b - a) / n for n in range(300, 900)])
Reply
#15
same empty graph, different error message.
raise RuntimeError("matplotlib does not support generators "

RuntimeError: matplotlib does not support generators as input
Reply
#16
Please post the code...
Reply
#17
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))
print(list(error))
plt.plot(error, [h for n in range(300, 900)])
Reply
#18
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[0]) for n in range (300,900)]
print(error_list)
plt.plot(error_list, [h for n in range(300, 900)])
Reply
#19
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?
Reply
#20
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[0]) 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating list of lists from generator object t4keheart 1 2,162 Nov-13-2020, 04:59 AM
Last Post: perfringo
  unexpected object? urufu 2 1,964 Mar-08-2020, 02:35 PM
Last Post: urufu
  receive from a generator, send to a generator Skaperen 9 5,423 Feb-05-2018, 06:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020