python solutions to bessel function problems

11
Cas Lab #3 Joshua Cook This lab was prepared using Python’s Interactive Terminal. Joshuas-MacBook-Air:Desktop joshuacook$ python Python 2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. Python Basics In order to prepare these plots, several critical libraries must be imported. I have also added a custom save script to the initial import. import os import numpy as np import matplotlib.pyplot as plt import scipy import scipy.special as sp import pylab from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm Problem 1 define Bessel Function Approximations def BesselJ(v,x): """ Defines a Bessel Function of the first kind J_v """ return np.sqrt(2/(np.pi*x))*np.cos(x-np.pi/4-v*np.pi/2) def BesselY(v,x): """ Defines a Bessel Function of the second kind Y_v """ return np.sqrt(2/(np.pi*x))*np.sin(x-np.pi/4-v*np.pi/2) Create an evenly sampled time vector at 200ms intervals t = np.arange(0., 100., 0.2) 1

Upload: joshua-cook

Post on 24-Nov-2015

109 views

Category:

Documents


7 download

DESCRIPTION

Python Solutions to Bessel Function Problems

TRANSCRIPT

  • Cas Lab #3

    Joshua Cook

    This lab was prepared using Pythons Interactive Terminal.

    Joshuas-MacBook-Air:Desktop joshuacook$ pythonPython 2.7.5 (default, Aug 25 2013, 00:04:04)[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwinType "help", "copyright", "credits" or "license" for more information.

    Python Basics

    In order to prepare these plots, several critical libraries must be imported. Ihave also added a custom save script to the initial import.

    import osimport numpy as npimport matplotlib.pyplot as pltimport scipyimport scipy.special as spimport pylabfrom mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import cm

    Problem 1

    define Bessel Function Approximations

    def BesselJ(v,x):""" Defines a Bessel Function of the first kind J_v"""return np.sqrt(2/(np.pi*x))*np.cos(x-np.pi/4-v*np.pi/2)

    def BesselY(v,x):""" Defines a Bessel Function of the second kind Y_v"""return np.sqrt(2/(np.pi*x))*np.sin(x-np.pi/4-v*np.pi/2)

    Create an evenly sampled time vector at 200ms intervals

    t = np.arange(0., 100., 0.2)

    1

  • Prepare Plots

    Plot 1 - First Order

    p1,= plt.plot(t, BesselJ(4,t), r--)p2,= plt.plot(t, sp.jn(4,t), gs)plt.title(User generated Bessel functions v Scipy Bessel \n of first order with v = 4)plt.axis([0, 100, -2, 2])plt.legend([p2, p1], ["scipy", "user"])plt.savefig(/Users/joshuacook/Desktop/cas_lab_3_plot_1a)

    Figure 1: Generated Plot - First Order

    2

  • Plot 2 - Second Order

    p1,= plt.plot(t, BesselY(4,t), r--)p2,= plt.plot(t, sp.yn(4,t), gs)plt.title(User generated Bessel functions v Scipy Bessel \n of second order with v = 4)plt.axis([0, 100, -2, 2])plt.legend([p2, p1], ["scipy", "user"])plt.savefig(/Users/joshuacook/Desktop/cas_lab_3_plot_1b)

    Figure 2: Generated Plot - Second Order

    3

  • Problem 2

    Python was rebooted for this problem.

    Joshuas-MacBook-Air:Desktop joshuacook$ pythonPython 2.7.5 (default, Aug 25 2013, 00:04:04)[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwinType "help", "copyright", "credits" or "license" for more information.>>>

    Import Libraries

    import numpy as npfrom scipy import *from scipy.special import jn, jn_zerosimport pylabfrom mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import cm

    Define function for modes of oscillation

    def u(m,n,distance,angle,t):""" Defines a user function for a graph of a Bessel function in time, as in say, for a drumhead"""nth_zero = jn_zeros(m,n)return jn(m,distance*nth_zero[n-1])*np.cos(m*angle)*np.cos(t)

    Set up spatial increments

    theta = r_[0:2*pi:50j]radius = r_[0:1:50j]x = array([r*cos(theta) for r in radius])y = array([r*sin(theta) for r in radius])

    4

  • Prepare Plots

    Plot 1 - m = 0, n = 1

    z = np.array([u(0,1,r,theta,0) for r in radius])fig = pylab.figure()ax = Axes3D(fig)ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap=cm.jet)ax.set_xlabel(X)ax.set_ylabel(Y)ax.set_zlabel(Z)pylab.savefig(/Users/joshuacook/Desktop/cas_lab_3_plot_2a)

    Figure 3: Generated Plot - m = 0, n = 1

    5

  • Plot 2 - m = 0, n = 2

    z = np.array([u(0,2,r,theta,0) for r in radius])fig = pylab.figure()ax = Axes3D(fig)ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap=cm.jet)ax.set_xlabel(X)ax.set_ylabel(Y)ax.set_zlabel(Z)fig.savefig(/Users/joshuacook/Desktop/cas_lab_3_plot_2b)

    Figure 4: Generated Plot - m = 0, n = 2

    6

  • Plot 3 - m = 0, n = 3

    z = np.array([u(0,3,r,theta,0) for r in radius])fig = pylab.figure()ax = Axes3D(fig)ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap=cm.jet)ax.set_xlabel(X)ax.set_ylabel(Y)ax.set_zlabel(Z)fig.savefig(/Users/joshuacook/Desktop/cas_lab_3_plot_2c)

    Figure 5: Generated Plot - m = 0, n = 3

    7

  • Plot 4 - m = 3, n = 1

    z = np.array([u(3,1,r,theta,0) for r in radius])fig = pylab.figure()ax = Axes3D(fig)ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap=cm.jet)ax.set_xlabel(X)ax.set_ylabel(Y)ax.set_zlabel(Z)fig.savefig(/Users/joshuacook/Desktop/cas_lab_3_plot_2d)

    Figure 6: Generated Plot - m = 3, n = 1

    8

  • Plot 5 - m = 3, n = 2

    z = np.array([u(3,2,r,theta,0) for r in radius])fig = pylab.figure()ax = Axes3D(fig)ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap=cm.jet)ax.set_xlabel(X)ax.set_ylabel(Y)ax.set_zlabel(Z)fig.savefig(/Users/joshuacook/Desktop/cas_lab_3_plot_2e)

    Figure 7: Generated Plot - m = 3, n = 2

    9

  • Problem 3

    Here is a plot of a drumhead with m = 5 and n = 4

    z = np.array([u(5,4,r,theta,0) for r in radius])fig = pylab.figure()ax = Axes3D(fig)ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap=cm.jet)ax.set_xlabel(X)ax.set_ylabel(Y)ax.set_zlabel(Z)fig.savefig(/Users/joshuacook/Desktop/cas_lab_3_plot_3)

    Figure 8: Generated Plot - m = 5, n = 4

    10

  • Number of Nodal Curves

    Four circular nodal curves can be seen.

    Figure 9: Generated Plot - m = 5, n = 4, alternate angle

    11

    Cas Lab #3Joshua CookPython BasicsProblem 1define Bessel Function ApproximationsCreate an evenly sampled time vector at 200ms intervalsPrepare Plots

    Problem 2Import LibrariesDefine function for modes of oscillationSet up spatial incrementsPrepare Plots

    Problem 3Number of Nodal Curves