繁体   English   中英

如何在python中的范围内向前和向后循环?

[英]How to loop forward and backward in a range in python?

在此处输入图像描述

我想按照上图中的描述对运动进行编程。 角度根据以下等式变化: theta = Amp*np.sin(2*np.pi*ftheta*p) 我正在遍历 p(time),这是这个等式中唯一的变量,没有其他变化。 我如何让它在达到振幅后停止并开始反向移动直到它达到 -(振幅)

import numpy as np
import matplotlib.pyplot as plt
import math

r=20
h=1.7
num_of_steps=100
emp=3
phi = []
theta = []
time=np.arange(0,100,1)
fphi = 1
ftheta = 1
Amp = 90
for j in time:
kampas = np.degrees(2*np.pi*fphi*j)
kitaskampas = np.degrees(np.sin(2*np.pi*ftheta*j))
if kampas > 360:
    temp = math.floor(kampas/360)
    sukasi = round(kampas - 360*temp)
    print(sukasi)
    phi.append(sukasi)
if kitaskampas == Amp:

print(phi)

你试过自己写一些代码吗? 如果是这样,请分享,以便我们提供帮助。

据我了解 while 循环可以为您工作:

exit_flag = 0
time = 0
theta = 0
amplitude = 1
while(exit_flag == 0):
    if(amplitude > 0):
        time = time + 1
    else:
        time = time - 1
    
    theta = some_equasion_dependning_of_time(time)
    
    if(we_wanna_exit)
        exit_flag = 1

我不明白 kampas 变量到底是怎么回事。 但是,这是在两个极限之间移动的最小示例。 您可以将计算放在我们更改值变量的地方。

direction = 1
_range = 90
unit_of_change = 7
value = 0
def looper():
   global direction, value
   
   if(value < -_range):
      #if value(theta) hits -amplitude change its direction
      direction = 1
   if(value > _range):
      #if value(theta) hits +amplitude change its direction reverse
      direction = 0

   #make required calculations with your theta
   if direction:
      value += unit_of_change
   else:
      value -= unit_of_change


unit_of_process = 200

#run for a while
while unit_of_process:
   print(value, end="  ")
   looper()
   unit_of_process -= 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM