! From: "COMPUTATIONAL PHYSICS, 2nd Ed" ! by RH Landau, MJ Paez, and CC Bordeianu ! Copyright Wiley - VCH, 2007. ! Electronic Materials copyright: R Landau, Oregon State Univ, 2007; ! MJ Paez, Univ Antioquia, 2007; and CC Bordeianu, Univ Bucharest, 2007. ! Supported by the US National Science Foundation ! ! bessel.f95: Computation spherical Bessel functions by recurrence Program bessel Implicit none Real*8 :: step, x, xmin, xmax, up, Down, t1, t2 Integer :: order, start xmin = 0.25 xmax = 40.0 step = 0.1 order = 10 start = 50 open(6, File = 'bessel.dat', Status = 'Unknown') ! open output file Do x = xmin, xmax, step t1 = Down(x, order, start) t2 = up(x, order) write (6, 50) x, t1, t2 End Do Close(6) 50 Format (f15.10, f15.10, f15.10) Stop 'data saved in bessel.dat' End Program bessel ! calculate using Downward recursion Function Down(x, order, start) Implicit none Integer :: k, order, start Real*8 :: Down, scale, x, j(100) ! the arbitrary start j(start + 1) = 1 j(start) = 1 Do k = start, 2, - 1 j(k - 1) = ((2*k - 1.0)/x)*j(k) - j(k + 1) End Do ! scale so that j(1) = sin(x)/x scale = (sin(x)/x)/j(1) Down = j(order + 1)*scale Return End ! calculate using upward recursion Function up(x, order) Implicit none Integer :: k, order Real*8 :: up, x, one, two, thr one = sin(x)/x two = (sin(x) - x*cos(x))/(x*x) Do k = 1, (order - 1) thr = ((2*k + 1.0)/x)*two - one one = two two = thr End Do up = thr Return End