from matplotlib import pyplot as plt got_plt = True except: got_plt = False from numpy import array from xymath.dataset import DataSet from xymath.nonlinfit import NonLinCurveFit xdata = array([-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9]) ydata = array([0.699369,0.700462,0.695354,1.03905,1.97389,2.41143, 1.91091,0.919576,-0.730975,-1.42001]) DS = DataSet(xdata, ydata, xName='x', yName='y') guessD = {'p1':1.0, 'p2':0.2} CFit = NonLinCurveFit(DS, rhs_eqnStr='p1*cos(p2*x) + p2*sin(p1*x)', constDinp=guessD, fit_best_pcent=0) # 0=fit best total error print( 'residuals from XYmath = %g'%sum( (CFit.eval_xrange( xdata ) - ydata)**2 ) ) print( 'residuals from author = 0.053812696547933969' ) print('') print(CFit.get_full_description()) if got_plt: plt.plot( xdata, ydata, 'o', markersize=10 ) xPlotArr, yPlotArr = CFit.get_xy_plot_arrays( Npoints=100, logScale=False) plt.plot( xPlotArr, yPlotArr ) plt.title('Trig Function: p1*cos(p2*x) + p2*sin(p1*x)') plt.show()
94.2, 92.5, 90.8, 89.1, 87.5, 85.9, 84.3, 81.2, 78.2, 75.3, 72.4, 69.7, 57.2, 46.6, 37.6, 30.1, 23.8, 18.7, 14.5, 11.1 ], dtype=double) DS = DataSet(alt_mArr, PaArr, xName='altitude', yName='pressure', xUnits='m', yUnits='Pa') guessD = {'A': 101325, 'c': 1, 'd': 2.25577E-5, 'n': 5.25588} print('guessD Before', guessD) CFit_toterr = NonLinCurveFit(DS, rhs_eqnStr='A*(c - d*x)**n', constDinp=guessD, fit_best_pcent=0) # 0=fit best total error print('guessD After', guessD) print('=' * 55) print('..........Total Error............') print(CFit_toterr.get_full_description()) print('=' * 55) print('..........Percent Error............') CFit_pcterr = NonLinCurveFit(DS, rhs_eqnStr='A*(c - d*x)**n', constDinp=guessD, fit_best_pcent=1) # 1=fit best percent error print(CFit_pcterr.get_full_description()) print('=' * 55)
def test_should_always_pass_cleanly(self): """Should always pass cleanly.""" nlf = NonLinCurveFit(self.myds, rhs_eqnStr='A*sin(2*pi*k*x+theta)', fit_best_pcent=1, constDinp=None)