简体   繁体   English

绘制变量增加的方程 python

[英]plotting equations with increasing variables python

i have been trying to create a function (Pmotion in the code below) that with several parameters gives me real and imaginary parts of the equation(that part is ok)我一直在尝试创建一个 function (下面代码中的 Pmotion),它有几个参数给我等式的实部和虚部(那部分没问题)

but in the next step i want to run the function for an increasing variable(in this case time(t) going up in jumps of 0.1 all the way to 2) and be able to plot the all these samples in an plot of the real part(Up_real in the y axis) and t in the x axis but in the next step i want to run the function for an increasing variable(in this case time(t) going up in jumps of 0.1 all the way to 2) and be able to plot the all these samples in an plot of the real part(y 轴上的 Up_real) 和 x 轴上的 t

how can i get to increase while still retaining the possibility of an initial t input?我怎样才能在保持初始 t 输入的可能性的同时增加? any help would be amazing任何帮助都会很棒

 def Pmotion(x,t,A,alpha,f):
        w=2*np.pi*f
        k1 = (w/alpha)
        theta = k1*x-w*t
        Up = k1*A*complex(-np.sin(theta),np.cos(theta)) 
        Up_real = Up.real
        Up_imag = Up.imag
        plt.plot([t],[UP_real]) #here i want these to be in the x and y axis
        plt.show() 

    #Pmotion(x=0,t=0,A=1,alpha=6000,f=2) 

First of all, divide your code in small independent blocks (high cohesion) as such create a function with the desired calculation:首先,将您的代码分成小的独立块(高内聚性),这样创建一个具有所需计算的 function:

def Pmotion(x,t,A,alpha,f):
        w=2*np.pi*f
        k1 = (w/alpha)
        theta = k1*x-w*t
        Up = k1*A*complex(-np.sin(theta),np.cos(theta)) 
        Up_real = Up.real
        Up_imag = Up.imag
        return Up_real, Up_imag

Then you can begin to think of a plotting method.然后你就可以开始想一个绘图方法了。 eg例如

def plot_Pmotion_t():
t_range = np.arange(0,2,0.1)
reals = [Pmotion(0,t,1,6000,2) for t in t_range]
plt.plot(t_range, reals)
plt.show()

You can now freely alter or add inputs to the plot function without changing the Pmotion function.您现在可以在不更改 Pmotion function 的情况下自由更改或添加 plot function 的输入。

Note: You are now plotting both real and imaginary values, change it to reals = [Pmotion(0,t,1,6000,2)[0] for t in t_range] to only plot the real part.注意:您现在正在绘制实部和虚部值,将其更改为reals = [Pmotion(0,t,1,6000,2)[0] for t in t_range]以仅 plot 实部。 Hope this helps!希望这可以帮助!

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

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