0

using Python I would like to plot a curve for the function y=cosh(x)*cos(5x) in my Jupyter Notebook.

In other words: (cosine hyperbolicus of x) times (cosine of 5x)

How do I do this? What do I need to import? Thank you very much in advance.

Greetings

  • 1
    Have you tried anything? This is a question/answer website, not a "request algorithm" one. Moreover your question is really easy, you didn't even made the effort to look by yourself. – RandomGuy May 21 '21 at 11:59
  • 1
    You're right, I should try harder next time. Thank you for feedback. – cs_question_asker May 21 '21 at 15:50

2 Answers2

1

Specify the range of values for x that you need. You can use Seaborn on top of Matplotlib to make it prettier, but this is optional:

import seaborn as sns

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(-5,5,0.1)   # start,stop,step

y= (np.cosh(x))*(np.cos(5*x) )

# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above) 
sns.set(style="darkgrid")

plt.plot(x,y)
plt.show()

enter image description here

Sanardi
  • 75
  • 1
  • 8
0

You will need to import a plotting library and a maths library. The most commonly used plotting library is matplotlib, and for maths it's numpy. For plotting, bokeh is a an alternative to matplotlib, which I think is great because graphs are interactive by default. The disadvantage is that because it's not as widely used as matplotlib, you're less likely to find help on it in terms of StackOverflow answers and tutorials.

Anyway, to the code:

# Import the necessary packages and modules
import matplotlib.pyplot as plt
import numpy as np

# Set your x-range and calculate y
xmin = -2.5
xmax = 2.5
numPoints = 100

x = np.linspace(xmin, xmax, numPoints)
y = np.cosh(x)*np.cos(5*x)

# Plot -- it really can be this simple [1]
plt.plot(x,y)

Both of the graphing libraries above give you flexible options on where to place the axes, legends, titles, and so on. I recommend searching for beginner's tutorials on them to learn this stuff in depth.

[1] There are two ways to plot in matplotlib. What is shown here is the MATLAB-like interface. The other method is to use the object-based interface, which takes a bit more of getting used to, and requires a bit more boilerplate code, but that's what you will end up using once you demand more control over the appearance of your plots.

I recommend starting with the MATLAB-like commands first. The documentation has a good beginner's tutorial: https://matplotlib.org/stable/tutorials/introductory/pyplot.html

butterflyknife
  • 1,438
  • 8
  • 17