• API
• FAQ
• Tools
• Archive
SHARE
TWEET Untitled a guest Feb 13th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import sympy as sp
2. from sympy import Symbol
3. import numpy as np
4. import matplotlib.pyplot as plt
5. %matplotlib inline
6. plt.rcParams['figure.figsize'] = 7, 7
7.
8. def lag_sym(data_points):
9.     x = Symbol('x')
10.     L = np.zeros(data_points.shape) # this stores our basis functions.
11.     L = []
12.     for i, xi in enumerate(data_points):
13.         temp_product = 1.0
14.         other_data_points = np.delete(data_points, i)
15.
16.         for xj in other_data_points: # compute product
17.             temp_product *= (x-xj)/(xi-xj)
18.         L.append(temp_product)
19. #         L[i] = temp_product  # store the product.
20.     # finished computing all basis functions.
21.     return L
22.
23. def fun_eval(F, mesh):
24.     # F should be a list of basis polynomials of sympy.Mul class
25.     return np.sum(np.array([[F[i].evalf(subs={'x':mesh[k]}) for i in range(len(F))] for k in range(len(mesh)) ]).transpose(), axis=1)
26.
27. def herm_sym(L, data_points):
28.     x = Symbol('x')
29.     H = []
30.     for i, xi in enumerate(data_points):
31.         H.append( (1 - 2*(x - xi)*L[i].diff() ) * L[i]**2 )
32.     return H
33.
34.
35.
36.
37.
38. mesh_resolution = 
39. for n in mesh_resolution:
40.     plt.figure()
41. #    plt.title('data points =')
42.
43.     data_l = np.linspace(-1, 1, 2*n)
44.     lag_fun = lag_sym(data_l)
45.
46.     data_h = np.linspace(-1, 1, n)
47.     L = lag_sym(data_h)
48. #     herm_fun = herm_sym(L, data_h)
49.
50.     plot_mesh = np.linspace(-1,1,100)
51.     lag_eval =  fun_eval(lag_fun, plot_mesh)
52. #     herm_eval = fun_eval(herm_fun, plot_mesh)
53.     plt.plot(plot_mesh,lag_eval)
54. #     plt.plot(plot_mesh,herm_eval)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top