In [87]:
%matplotlib inline
In [88]:
import scipy.special as ss
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
# for nicer plots, make fonts larger and lines thicker
matplotlib.rcParams['font.size'] = 12
matplotlib.rcParams['axes.linewidth'] = 2.0
#Define the function to asymptotically describe Bessel function
def bessel_asymp(n,x):
return np.sqrt(2.0/np.pi/x)*np.cos(x-(n*np.pi/2+np.pi/4)) #true for x >> n
#Compare the built-in Bessel function in scipy and the one defined above
x = np.linspace(0.,80,900)
#Plot built-in 3 Bessel functions: J_0, J_2, J_5
for n in [7]:
plt.plot(x,ss.jn(n,x), label='$J_%d$' % (n))
#Plot defined Bessel function: J_5
x_large = x[x>n]
plt.plot(x_large,bessel_asymp(n,x_large), label='$J_%d$ (Asymptotic)' % n)
#Complete the plot with label and title
plt.title('Bessel Functions')
plt.xlabel('x')
plt.ylabel('$J_n(x)$')
plt.legend() #show legend on the plot
plt.axhline(0)
Out[88]:
In [89]:
import scipy.special as ss
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
# for nicer plots, make fonts larger and lines thicker
matplotlib.rcParams['font.size'] = 12
matplotlib.rcParams['axes.linewidth'] = 2.0
# The recursion relation
x = np.linspace(0.1,50,500)
n = 7
j_n_rec = 2*(n-1)/x*ss.jn(n-1,x) - ss.jn(n-2,x)
#Plot Bessel functions with above recursion function
plt.semilogy(x,abs(j_n_rec-ss.jn(n,x)), 'r+-', linewidth=2.0, label='n = %d' %n)
plt.xlabel('x')
plt.ylabel('$|J_n - J_{n,rec}|$')
plt.grid()
plt.legend()
Out[89]:
In [90]:
import matplotlib.pyplot as plt
x = np.linspace(0,50,500)
y = x
plt.plot(x,y)
plt.grid()
In [91]:
import matplotlib.pyplot as plt
plt.semilogy(x,y)
plt.grid()
In [ ]: