Home > Back-end >  How can I use np.arange especially when I want to do math operations within a equation?
How can I use np.arange especially when I want to do math operations within a equation?

Time:10-19

I want to use my t=np.arange(0,0.99,0.01) in x=2*cos(2*pi*12*t) but I'm not able to get it. I'm not sure but I think that problem is calling an error when I want to access a member of t array one by one. How can I solve my problem?

from math import pi
from numpy import cos
import numpy as np
import math
import matplotlib.pyplot as plt
t=np.arange(0,0.99,0.01)
print(t)
for i in range(0,len(t)):
  m=2*pi*12*t[i]
  x=2*cos(m)
  print(x)

CodePudding user response:

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

x_coordinates = []
y_coordinates = []

for t in np.arange(0,0.99,0.01):
    omega=2*math.pi*t*12
    x_coordinates.append(2*math.cos(omega))
    y_coordinates.append(t)
    
    
figure(num=None,figsize=(13,7))
plt.plot(x_coordinates, y_coordinates)
plt.legend()
plt.ylabel('Time(seconds)', fontsize=20)
plt.xlabel('Cos', fontsize=20)
plt.grid(True)
plt.show()
plt.show()

enter image description here

CodePudding user response:

Slight modifications of your import section:

from math import pi
from numpy import cos
import numpy as np
import matplotlib.pyplot as plt

And plotting x vs. m:

t=np.arange(0,0.99,0.01)
m=2*pi*12*t
x=2*cos(m)
plt.plot(m, x, 'b--o')
plt.xlabel('x')
plt.ylabel('m')

Output: enter image description here

Note that all x, t, and m are, now, arrays with the shape of (99,) as you would get from:

x.shape
m.shape
t.shape
  • Related