Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def F(C):
- x2 = x**2
- C5_2, C3_2, C1_2 = C
- y = (((C5_2 * x2) + C3_2) * x2 + C1_2) * x
- y *= 2 # scaling
- return y
- def Hrms(C):
- return 1E+08 * ((F(C) - truth)**2).sum()
- def Hmax(C):
- return 1E+06 * np.abs((F(C)-truth)).max()
- import numpy as np
- from scipy.special import factorial
- from scipy.optimize import minimize
- import matplotlib.pyplot as plt
- quarterpi, halfpi, pi, twopi = [f*np.pi for f in (0.25, 0.5, 1, 2)]
- C5_2 = 0.03635510 # per https://space.stackexchange.com/a/30955/12102
- C3_2 = -0.32161470
- C1_2 = 0.78531340
- C0 = np.array([C5_2, C3_2, C1_2])
- Cnom = np.array([halfpi**5 /(2.*factorial(5)),
- halfpi**3 /(2.*factorial(3)),
- halfpi**1 /(2.*factorial(1))]) # if expanding about 0
- angle = np.linspace(0, halfpi, 1001)
- truth = np.sin(angle)
- x = angle/halfpi # scaling
- Cst = np.array([0.1, 0.0, 0.5]) # arbitrary starting point
- opts = {'xtol': 1E-12, 'disp': False, 'maxiter':1000, 'maxfev':2000}
- resrms = minimize(Hrms, Cst, method='Nelder-Mead', options=opts)
- Crms = resrms['x']
- resmax = minimize(Hmax, Cst, method='Nelder-Mead', options=opts)
- Cmax = resmax['x']
- np.set_printoptions(precision=6)
- print ("C0: {}, Hmax(C0): {:0.2f}, Hmrms(C0): {:0.2f}".format(C0, Hmax(C0), Hrms(C0)))
- print ("Cnom: {}, Hmax(Cnom): {:0.2f}, Hmrms(Cnom): {:0.2f}".format(Cnom, Hmax(Cnom), Hrms(Cnom)))
- print ("Crms: {}, Hmax(Crms): {:0.2f}, Hmrms(Crms): {:0.2f}".format(Crms, Hmax(Crms), Hrms(Crms)))
- print ("Cmax: {}, Hmax(Cmax): {:0.2f}, Hmrms(Cmax): {:0.2f}".format(Cmax, Hmax(Cmax), Hrms(Cmax)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement