[英]Summing up the output of multiple functions in python
I currently have three sine functions (y1, y2, y3) and would like to sum the output of the functions in a new function (ytotal) but only where the output of the sine functions are greater than 0.我目前有三个正弦函数(y1、y2、y3),并且想在一个新函数(ytotal)中对函数的输出求和,但仅在正弦函数的输出大于 0 的情况下。
import numpy as np
import matplotlib.pyplot as plt
#%%
phi = np.linspace(-2*np.pi, 2*np.pi, 100)
y1 = 0.2*np.sin(phi)
y2 = 0.2*np.sin(phi-(120*(np.pi/180)))
y3 = 0.2*np.sin(phi-(240*(np.pi/180)))
#if y1 or y2 or y3 > 0:
# ytotal = y1+y2+y3
plt.plot(phi,y1, label = "Piston 1")
plt.plot(phi,y2, label = "Piston 2")
plt.plot(phi,y3, label = "Piston 3")
#plt.plot(phi,ytotal, label = "Total output")
positions = (0,np.pi/3,2*np.pi/3,np.pi,4*np.pi/3,5*np.pi/3,2*np.pi)
labels = ("0","60","120","180","240","300","360")
plt.xticks(positions, labels)
plt.xlabel('Angular displacement')
plt.ylabel('Stroke')
plt.legend()
plt.show()
The output should be something like the following:输出应类似于以下内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.