Menu

[r6218]: / trunk / py4science / examples / bessel.py  Maximize  Restore  History

Download this file

64 lines (47 with data), 1.7 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
"""Plot some Bessel functions of integer order, using Scipy and pylab"""
import scipy.special
import numpy as N
import scipy as S
import pylab as P
# shorthand
special = S.special
def jn_asym(n,x):
"""Asymptotic form of jn(x) for x>>n"""
return S.sqrt(2.0/S.pi/x)*S.cos(x-(n*S.pi/2.0+S.pi/4.0))
# build a range of values to plot in
x = N.linspace(0,30,400)
# Start by plotting the well-known j0 and j1
P.figure()
P.plot(x,special.j0(x),label='$J_0$')
P.plot(x,special.j1(x),label='$J_1$')
# Show a higher-order Bessel function
n = 5
P.plot(x,special.jn(n,x),label='$J_%s$' % n)
# and compute its asymptotic form (valid for x>>n, where n is the order). We
# must first find the valid range of x where at least x>n:
x_asym = S.compress(x>n,x)
P.plot(x_asym,jn_asym(n,x_asym),label='$J_%s$ (asymptotic)' % n)
# Finish off the plot
P.legend()
P.title('Bessel Functions')
# horizontal line at 0 to show x-axis, but after the legend
P.axhline(0)
# EXERCISE: redo the above, for the asymptotic range 0<x<<n. The asymptotic
# form in this regime is
# J(n,x) = (1/gamma(n+1))(x/2)^n
# Now, let's verify numerically the recursion relation
# J(n+1,x) = (2n/x)J(n,x)-J(n-1,x)
jn = special.jn # just a shorthand
# Be careful to check only for x!=0, to avoid divisions by zero
xp = S.compress(x>0.0,x) # positive x
# construct both sides of the recursion relation, these should be equal
j_np1 = jn(n+1,xp)
j_np1_rec = (2.0*n/xp)*jn(n,xp)-jn(n-1,xp)
# Now make a nice error plot of the difference, in a new figure
P.figure()
P.semilogy(xp,abs(j_np1-j_np1_rec),'r+-')
P.title('Error in recursion for $J_%s$' % n)
P.grid()
# Don't forget a show() call at the end of the script
P.show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.