Search code examples
pythonnumpyplotpolynomialsexponential

plotting exponential and polynomial function together in python


I want to plot the following y and y' functions together on the same axes in python. The x-axis values should go from -10 to 10 in 0.1 intervals: enter image description here

what I tried: I tried plotting just y' (labelled y_p) and I am getting errors.

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-10,10,0.1)

A=1
B=0.1
C=0.1
y_p = (A*np.exp((-x**2)/2))(1+(B*((2*(np.sqrt(2))*(x**3))-(3*(np.sqrt(2))*x))/((np.sqrt(6)))))
plt.plot(x,y_p)

but this generates error:

TypeError                                 Traceback (most recent call last)
<ipython-input-71-c184f15d17c7> in <module>
      7 B=0.1
      8 C=0.1
----> 9 y_p = (A*np.exp((-x**2)/2))(1+(B*((2*(np.sqrt(2))*(x**3))-(3*(np.sqrt(2))*x))/((np.sqrt(6)))))
     10 plt.plot(x,y_p)

TypeError: 'numpy.ndarray' object is not callable

​

I am sure there is a better way to do this. I'm quite new to python, so any help is truly appreciated!


Solution

  • You cannot multiply implicitly (without using *) in python.

    Here is your code after the correction:

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.arange(-10, 10, 0.1)
    
    A = 1
    B = 0.1
    C = 0.1
    y_p = (A * np.exp((-x ** 2) / 2)) * (1 + (B * ((2 * (np.sqrt(2)) * (x ** 3)) - (3 * (np.sqrt(2)) * x)) / (np.sqrt(6))))
    plt.plot(x, y_p)
    plt.show()