Search code examples
pythonplotscipynumerical-integration

Python Plot an Integral with discrete and continuous parameters


I have the following function:

def sbeta(beta,gamma,y):
    k = 2/(np.pi)**2.
    return k * np.sqrt(1 - (np.sqrt(1-y**2.)*np.sin(beta)*np.cos(gamma) - y*np.cos(beta))**2.)

where beta is a constant and y is defined between -1 and 1:

beta = 23.4
y = numpy.linspace(-1, 1, 100)

I want to plot the integral of this function for gamma evaluated from 0 to 2pi:

def integral(beta,gamma,y):
    for i in range(len(y)):
        I = integrate.quad(sbeta, 0., 2*np.pi, args=(beta, y[i]))
        print(I)
        plt.plot(y[i],I[0])

gamma = np.linspace(0., 2*np.pi, 10)
integral(beta,gamma,y)
plt.show()

There are no errors at this point, but I don't think this is correct. I would like to compute the integral for gamma as variable from 0 to 2pi but y is a discrete array. How to compute and plot this for 10 y values from -1 and 1? Should I use scipy.integrate.cumtrapz?


Solution

  • This is my attempt to the answer:

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy import integrate
    
    
    def f(gamma, y):
        k = 2/(np.pi)**2.
        beta = 23.4*(np.pi/180.)
        return k * np.sqrt(1 - (np.sqrt(1-y**2.)*np.sin(beta)*np.cos(gamma) - y*np.cos(beta))**2.)
    
    
    y = np.linspace(-1., 1.,10)
    low = 0.
    high = 2*np.pi
    
    Ivals = []
    for i in range(len(y)):
        I = integrate.quad(f, low, high, args=(y[i]))
        plt.scatter(y[i],I[0])
        Ivals.append(I)
    plt.show()
    

    which shows the following plot:

    enter image description here

    Can anyone confirm if this is correct?