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.
plotandshowcoming from? I assumenpis numpy?