2

我在一个开始的python类中,我应该使用子图来绘制这个摆线。我得到了参数形式的摆线,并被告知以这种形式保留它。

这是给我的方程式:

x = r (θ − sin θ )

y = r (1 − cos θ )

r应该是我功能的用户输入。

我不明白如何定义 theta 或如何以参数方式绘图。太感谢了!!

这是我到目前为止的代码:

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)
4

1 回答 1

1
# 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

您可以通过更改np.linspace参数中的“100”来更改绘图的分辨率。尝试将其设置为非常低 (5) 然后非常高 (1000) 以查看差异。

于 2020-08-05T22:06:47.707 回答