pythonpython-3.xmathparametric-equations

How do I plot a cycloid parametrically?


I'm in a beginning python class and I'm supposed to plot this cycloid using subplots. I was given the cycloid in parametric form and told to leave it in that form.

Here are the equations I was given:

x = r (θ − sin θ )

y = r (1 − cos θ )

r is supposed to be a user input into my function.

I don't understand how to define theta, or how to plot parametrically. thank you so much!!

Here's the code I have so far:

import matplotlib.pyplot as plt 
import sympy as sp

def cycloid(r):
    x = r(theta - sp.sin(theta))
    y = r(1 - sp.cos(theta))
    sp.plot_parametric(x, y, (r, -2*sp.pi, 2*sp.pi))
    plt.show()

cycloid(5)

Solution

  • # Various imports
    import matplotlib.pyplot as plt
    from math import sqrt, cos, sin
    import numpy as np
    
    def cycloid(r):
      x = [] #create the list of x coordinates
      y = [] #create the list of y coordinats
      for theta in np.linspace(-2*np.pi, 2*np.pi, 100): #loop over a list of theta, which ranges from -2π to 2π
        x.append(r*(theta - sin(theta))) #add the corresponding expression of x to the x list
        y.append(r*(1 - cos(theta))) #same for y
      plt.plot(x,y)  #plot using matplotlib.piplot
      plt.show()  #show the plot
    
    cycloid(5) #call the function
    

    You can change the resolution of the plot by changing the "100" in the np.linspace parameters. Try to set it to very low (5) then very high (1000) to see the difference.