How can I adjust the range and scaling for a cos function in Python?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 2K views
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
Python:
r = np.arange(0,400,0.1)
t = np.cos(r)+10
plt.plot(r,t)

How can I use the range of the cos function to look like this
 

Attachments

  • Adsız.png
    Adsız.png
    16 KB · Views: 455
Physics news on Phys.org
Do you mean
change t = np.cos(r)+10
to t = np.(cos(r) + 1) edit: or t = 12*(np.cos(r) + 1) ; if np. is not the amplitude
so that the lower extreme is on the y=0 axis?
 
Merlin3189 said:
Do you mean
change t = np.cos(r)+10
to t = np.(cos(r) + 1) edit: or t = 12*(np.cos(r) + 1) ; if np. is not the amplitude
so that the lower extreme is on the y=0 axis?
there's a syntax error in your code. Also no, I mean that I need to write a cos function that will look like my graph. Or cos^2x function. Both are okay for me
 
Sorry. I'm not a Python person, so I don't know "np." nor "plt." . They look like references to objects defined elsewhere, but there's no way I can see of knowing what those objects are like.

I just noticed that +10 would leave some negative values.

I'm not sure what you want that's different from the graph you're getting? I can't see where it gets the y-axis values, but that's probably just how you've labelled the axis.
 
  • Like
Likes   Reactions: Arman777
What's the wavelength of ##\cos(x)##? What's the wavelength of your graph? What do you need to do to your array ##r## so that the wavelength matches?

What are the minimum and maximum values of ##\cos(x)##? What are the minimum and maximum values of your graph? What do you need to do to make them match?
 
  • Like
Likes   Reactions: Arman777
Have you seen this formula before: ##y = A \cos(\omega t + \phi)##? A is the amplitude, ##\omega## is the angular frequency in radians per second and ##\phi## is the phase angle in radians. You need ##\omega## so that it has completed two revolutions for t = 360 degrees.
 
  • Like
Likes   Reactions: Arman777
Python:
r = np.arange(0,400,0.4)
t = (11*np.cos(r/28.7))+11
plt.plot(r,t)
I guess I find it :) , This works well
 
Last edited: