0

I am just starting to try and learn python. I am having trouble plotting functions. I follow examples from the web and it seems to work fine. However, when I try it on my own stuff I get an error that the function can only take length 1 arrays to be used as scalars but I don't see the difference in my own attempts then copying plot examples. My code is as follows:

i = np.arange(-16, -7, 1)
r = []
y = []
for x in i:
    r.append(math.pow(10, x))

x = np.asarray(r)
y = (math.cos(1.2) - (1 / x) * (math.sin(1.2 + x) - math.sin(1.2)))
plot(x,y)
show()

Basically I just want to output y=f(x) for the values 10^-16, 10^-15...10^-7. But I swear all the plotting examples I find implement it in the same fashion. For example, this works just fine:

x = arange(0, 2, 0.01)
y = 2 * sin(2 * pi * (x - 1 / 4))
plot(x, y)
1
  • Where are plot and show coming from? I assume np is numpy? Commented Sep 14, 2012 at 20:39

2 Answers 2

1

Are you using the functions in the correct module? Python's built-in math.cos only takes a single scalar value, whereas numpy.cos takes arrays as well:

>>> import numpy
>>> import math
>>> math.cos([1, 2, 3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required
>>> numpy.cos([1, 2, 3])
array([ 0.54030231, -0.41614684, -0.9899925 ])
Sign up to request clarification or add additional context in comments.

Comments

0

Your main problem is that you're mixing two types of mathematical operations

  • operations from the math module.
  • operations from NumPy, that work on ndarrays (NumPy arrays). It's more or less conventional to import numpy as np.

Functions from the math module work on float scalars only. If you want to use a math function on a set of scalars (a list, an array...), you have to apply it on every single item of your set by iteration. That's what you're currently doing when constructing your r list: your loop can be transformed as r = [math.pow(10,x) for x in i]

NumPy, on the contrary, is designed for handling large data arrays. Operations on an array are performed directly at the C level, which is far faster and more efficient than the iteratve method I described. When using np functions, the input set is transformed as a ndarray below the hood.

For example, you can create a r array very efficiently, using the np.power function:

r = np.power(10., i)

(Note that I'm using 10. and not 10 to force the output array to have a float dtype. I could have done np.power(10, i, dtype=float) as well).

Your y becomes:

y = (math.cos(1.2) - (1. / r) * (np.sin(1.2 + r) - math.sin(1.2)))

It's OK to keep math to compute the sine and cosine of 1.2, as it's only a scalar. But for the sin(1.2 + x) part, as x is already a ndarray, you should use np.sin. An alternative would be to iterate of x and create an array like np.array([math.sin(i+1.2) for i in x]), but it completely defeats the point of using ndarrays in the first place.

In the last example you mentioned, a step is missing:

from numpy import sin

You now have to remember that sin is the NumPy function, not its math counterpart. Because of the risk of confusion, you should probably not import functions independently and use np.sin instead.

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.