def cub_spl_nd(x1, y1): roots = [] x_spline = [] y_spline = [] x_spline_range = [] for i in range(len(x1)): f, x_domain, y_range = inter.cubicSpline(x1[i], y1[i]) x_spline.append(x_domain) y_spline.append(y_range) g = poly_nd_1[i](x_domain) index = 0 h = y_range - g root = 100 while h[index] > 0.5e-1: if index < len(x_domain) - 1: index = index + 1 root = x_domain[index] else: break for j in range(len(x1[i]) - 1): if root > x1[i][j] and root < x1[i][j + 1]: #x_spline_range.append([x[i][j],x[i][j+1],x[i][j+1]-x[i][j]]) x_spline_range.append(x1[i][j + 1] - x1[i][j]) roots.append(root) return roots, x_spline, y_spline, x_spline_range
import LeastSquares as LS import numpy as np from math import exp from scipy.special import jn import matplotlib.pyplot as plt # Exercise 3.6 #******************************************************************* # generate points of Bessel function J(1,x) x = np.arange(0,12,0.5) npts = len(x) bess = [jn(1,i) for i in x] # call cubic spline method from my interpolation module xx,yy = Interp.cubicSpline(x,bess,npts,0.1) # plot data and interpolation plt.figure(1) plt.plot([0,12],[0,0],'k--') plt.plot(3.83,0,'gx',markersize=20) plt.plot(x,bess,'bo',label='Bessel Points') plt.plot(xx,yy,'r',label='Cubic Spline Interpolation') plt.xlabel('x') plt.ylabel('y') plt.annotate('$x=3.83$',fontsize=18,xy=(0.38,0.46),xycoords='figure fraction') plt.legend() #******************************************************************* # Exercise 3.10