-3

Matlab

x = 0:pi/100:2*pi;
y = sin(x);
plot(y)

I think range() cannot work here because it only accepts end arguments as integers but I need floats (0.0; 2*pi). Pseudocode in Python 2.7.11+

import math
x = pseudoRange(0.0, 2*math.pi, math.pi/100);

Taking ceil() is not practical for integers.


How can you have the trigonometric range of floats in Python?

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

3 Answers3

5

Use a generator expression:

(math.pi/100*k for k in range(200))

If you want the result as a list use:

[math.pi/100*k for k in range(200)]

You can also use the list-comprehension approach to get the corresponding y-values:

x = [math.pi/100*k for k in range(200)]
y = [math.sin(p) for p in x]

Then the pair of lists x,y can be plotted using matplotlib

Note that in the above, the points will range from 0.0 to 1.99*math.pi. If you wanted the final point to be 2*pi itself, you would need to replace range(200) by range(201).

John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • Since he apparently wants to plot in something like Matlab, namely matplotlib, he seems to want a list. Two lists, probably, one for the x-values (which is what you show) and another for the y-values. You may want to show the list of y-values, using the `sin()` function. – Rory Daulton Jun 06 '16 at 19:53
  • @RoryDaulton Good suggestion – John Coleman Jun 06 '16 at 19:58
3

You can use numpy.arange:

>>> np.arange(0, 2*math.pi, math.pi/100)
array([ 0.        ,  0.03141593,  0.06283185,  0.09424778,  0.12566371,
        # ... 190 more ...
        6.12610567,  6.1575216 ,  6.18893753,  6.22035345,  6.25176938])

You might also be interested in numpy.linspace:

>>> np.linspace(0, 2*math.pi, 200)
array([ 0.        ,  0.0315738 ,  0.06314759,  0.09472139,  0.12629518,
        # ... 190 more ...
        6.15689013,  6.18846392,  6.22003772,  6.25161151,  6.28318531])

Using np.arange, the third parameter is the step, while with np.linspace it's the total number of evenly-spaced values in the interval. Also note that linspace will include the 2*pi, while arange does not, but stop at the last multiple of step smaller than that.

You can then plot those with matplotlib.pyplot. For sin, you can just use np.sin which will apply the sine function to each element of the input array. For other functions, use a list comprehension.

>>> from matplotlib import pyplot
>>> x = np.arange(0, 2*math.pi, math.pi/100)
>>> y = np.sin(x) # using np.sin
>>> y = [math.sin(r) for r in x] # using list comprehension
>>> pyplot.plot(x, y)
[<matplotlib.lines.Line2D at 0xb3c3210c>]
>>>pyplot.show()
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • 1
    after edit you do show that `np.sin(x)` does apply `sin` to each element of the array but you may consider mentioning this more explicitly since the OP is coming from matlab where that is common behaviour. – Tadhg McDonald-Jensen Jun 06 '16 at 20:43
  • @TadhgMcDonald-Jensen Right, added. PS.: Thanks for the fix. Turns out that meant something entirely different in English... – tobias_k Jun 06 '16 at 20:51
0

Assuming that you have matplotlib already installed or that you can install it on your system, a sort of matlab clone is available under the pylab namespace,

import pylab as pl

x = pl.linspace(0, 2*pl.pi,201) #  x is an array
y = pl.sin(x)                   #  y is computed with an array expression
pl.plot(x, y, "ro-", label="Sine(t)")
pl.legend(loc='best')
pl.xlabel('$\\omega_o t$')
pl.show()

You can also import directly all pylab's namespace

from pylab import *

x = linspace(0, 2*pi, 201)
y = sin(x)
...

but this is not recommended, because you have now almost 1000 new, not qualified names in your namespace...

Coming to your question strictly, x = 0:pi/100:2*pi; maps to a different syntax, x = pl.linspace(0, 2*pl.pi,201), where you don't specify the increment but the number of points (as you have noted, due to fence effect you have to specify 201 to have the desired result).

gboffi
  • 22,939
  • 8
  • 54
  • 85