Python Graphing a cos function

  • Python
  • Thread starter Arman777
  • Start date
  • #1
1,904
147
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

Answers and Replies

  • #2
Merlin3189
Homework Helper
Gold Member
1,655
769
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?
 
  • #3
1,904
147
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?
Theres 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
 
  • #4
Merlin3189
Homework Helper
Gold Member
1,655
769
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 Arman777
  • #5
Ibix
Science Advisor
Insights Author
2020 Award
7,383
6,467
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 Arman777
  • #6
verty
Homework Helper
2,164
198
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 Arman777
  • #7
1,904
147
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:

Related Threads on Python Graphing a cos function

Replies
6
Views
568
  • Last Post
Replies
9
Views
2K
  • Last Post
Replies
9
Views
368
Replies
2
Views
5K
Replies
19
Views
1K
Replies
6
Views
3K
Replies
17
Views
327
  • Last Post
Replies
1
Views
822
Replies
5
Views
963
Replies
2
Views
537
Top