python - how to compute a function in a list? -
i'm reading "a primer on scientific programming python" book , i'm stuck on exercise 2.26. said write function maxmin(f, a, b, n=1000) returns maximum , minimum values of mathematical function f(x) (evaluated @ n points) in interval between , b.
the maxmin function can compute set of n coordinates between , b stored in list x, compute f @ points in x , store values in list y. python functions max(y) , min(y) homecoming maximum , minimum values in list y, respectively.
as test, said
from math import cos, pi print maxmin(cos, -pi/2, 2*pi) should write out(1.0, -1.0)
this tried, doesn't homecoming anything!
from math import cos, pi def maxmin(f, a, b, n=1000): x = [f(i) in range(a, b, n)] #print x maximum = max(x) minimum = min(x) homecoming maximum, minimum print maxmin(cos, -pi/2, 2*pi)
your code using range() incorrectly. function not back upwards floating-point arguments , lastly argument isn't think is.
if remember rightly, book next based around numpy. if that's case, can replace range() numpy.linspace().
python
No comments:
Post a Comment