Python Graphing a cos function

In summary, The cosine function can be used to create a graph that looks similar to the one shown. The cosine function can be used to create a graph that looks similar to the one shown. The cosine function can be used to create a graph that looks similar to the one shown.
  • #1
Arman777
Insights Author
Gold Member
2,168
193
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: 347
Technology news on Phys.org
  • #2
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
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?
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
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
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
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
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:

1. What is a cos function in Python?

A cos function in Python is a mathematical function that calculates the cosine value of a given angle. It is a part of the math module in Python that provides access to various mathematical functions, including trigonometric functions such as sine, cosine, and tangent.

2. How do I graph a cos function in Python?

To graph a cos function in Python, you need to import the matplotlib library, which provides a wide range of tools for creating graphs and charts. Then, you can use the plt.plot() function to plot the cos function with the desired inputs and display it using the plt.show() function.

3. What is the syntax for graphing a cos function in Python?

The syntax for graphing a cos function in Python is as follows:

import matplotlib.pyplot as plt
x = np.linspace(start, stop, num)
y = np.cos(x)
plt.plot(x, y)
plt.show()

Here, start and stop represent the start and end points of the x-axis, and num represents the number of points to be plotted. The np.linspace() function creates an evenly spaced array of numbers between the start and stop values.

4. How can I customize the graph of a cos function in Python?

There are several ways to customize the graph of a cos function in Python. You can change the color, line style, and width of the graph by passing in the desired parameters to the plt.plot() function. Additionally, you can add a title, labels for the x-axis and y-axis, and a legend using the plt.title(), plt.xlabel(), plt.ylabel(), and plt.legend() functions, respectively.

5. Can I graph multiple cos functions on the same plot in Python?

Yes, you can graph multiple cos functions on the same plot in Python by calling the plt.plot() function multiple times with different inputs for x and y. You can also customize each plot individually by passing in different parameters to each plt.plot() function. Additionally, you can use the plt.subplot() function to create subplots for each cos function.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
901
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
21
Views
4K
  • Programming and Computer Science
Replies
9
Views
2K
  • Advanced Physics Homework Help
Replies
6
Views
173
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top