I am trying to plot only the maximum points (maximum amplitude) of several sine waves that are generated with the same phase, for which I used append to store the data of these points, however, the amplitudes are plotted, but over the entire range from the first to the last wave, and the phase where each maximum amplitude occurs is not respected and plotted for all values of z (I used the z-x plane).
For example, for three waves, I expect to get the following:
However, with my code I get this:
Could you help me correct my mistake? I'd be really grateful
My code is the following:
import matplotlib.pyplot as plt
import numpy as np
from numpy import cos, sin
#constants
pi = 3.14159
f=2 #frequency
A=1 #Amplitude
#Range to evaluate z in pha
z = np.linspace(0.0, 3.00,80)
x1=[]
z1=[]
#Creating the waves:
#range of j define the number of waves are plotting
for j in range(1,4,1):
#general phase:
pha=2*pi*f*z
#general wave equation:
x=A*cos(pha) j
#plot of the sinusoidal waves:
plt.plot(z,x,'blue')
#Saving the values of each amplitude:
#Max value when cos(pha)=1 --> x=A j
if (x == A j).any():
xx=np.amax(x)
x1.append(xx)
z1.append(z)
#Plotting the dots for each wave:
plt.plot(z1,x1,'red')
plt.grid()
plt.show()
CodePudding user response:
import matplotlib.pyplot as plt
import numpy as np
from numpy import cos, sin
#constants
pi = 3.14159
f=2 #frequency
A=1 #Amplitude
#Range to evaluate z in pha
z = np.linspace(0.0, 3.00,80)
# number of cycles and its
n_cycles = int(f*np.amax(z))
k = np.linspace(0, n_cycles-1, n_cycles)
x1=[]
z1=[]
#Creating the waves:
#range of j define the number of waves are plotting
for j in range(1,4,1):
#general phase:
pha=2*pi*f*z
#general wave equation:
x=A*cos(pha) j
#plot of the sinusoidal waves:
plt.plot(z,x,'blue')
#Saving the values of each amplitude:
#Max value when cos(pha)=1 --> x=A j
if (x == A j).any():
# each cycle has a maximum...
xx = np.repeat(np.amax(x), n_cycles)
x1.append(xx)
# If cos(pha) = 1 --> pha = k*2*pi, for k = 0, 1, 2 ... n_cycles
# f*z = k
# so,
# z = k/f
z1.append(k/f)
#Plotting the dots for each wave:
plt.plot(z1,x1,'red', ls='', marker='o')
plt.grid()
plt.show()