Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- from scipy.optimize import least_squares
- def fitFunc(p, x):
- '''
- Fit function
- '''
- f = (p[0]+ p[1]*x) + p[2]*(np.exp(p[3]*(x-p[4])) - 1)
- return f
- def fitFuncDiff(p, x):
- '''
- Differential of fit function
- '''
- df = p[1] + p[3]*p[2]*np.exp(p[3]*(x-p[4]))
- return df
- def calcChiSq(p, x, y, xerr, yerr):
- '''
- Error function for fit
- '''
- e = (y - fitFunc(p, x))/(np.sqrt(yerr**2 + fitFuncDiff(p, x)**2*xerr**2))
- return e
- def fitStdError(jacMatrix):
- # Compute covariance
- jMat2 = np.dot(jacMatrix.T, jacMatrix)
- detJmat2 = np.linalg.det(jMat2)
- # Prepare output
- output = np.zeros(jMat2.shape[0])
- if detJmat2 < 1E-32:
- print("Value of determinat detJmat2",detJmat2)
- print("Matrix singular, error calculation failed.")
- return output
- else:
- covar = np.linalg.inv(jMat2)
- for i in range(len(output)):
- output[i] = np.sqrt(covar[i, i])
- return output
- planck_data = np.genfromtxt("Planck.csv", dtype=None, delimiter=',')
- V1_405nm = planck_data[:, 0]
- V1_405nm = V1_405nm[~np.isnan(V1_405nm)]
- i1_405nm = planck_data[:, 1]
- i1_405nm = i1_405nm[~np.isnan(i1_405nm)]
- V1_436nm = planck_data[:, 2]
- V1_436nm = V1_436nm[~np.isnan(V1_436nm)]
- i1_436nm = planck_data[:, 3]
- i1_436nm = i1_436nm[~np.isnan(i1_436nm)]
- V1_546nm = planck_data[:, 4]
- V1_546nm = V1_546nm[~np.isnan(V1_546nm)]
- i1_546nm = planck_data[:, 5]
- i1_546nm = i1_546nm[~np.isnan(i1_546nm)]
- V1_578nm = planck_data[:, 6]
- V1_578nm = V1_578nm[~np.isnan(V1_578nm)]
- i1_578nm = planck_data[:, 7]
- i1_578nm = i1_578nm[~np.isnan(i1_578nm)]
- V1_365nm = planck_data[:, 8]
- V1_365nm = V1_365nm[~np.isnan(V1_365nm)]
- i1_365nm = planck_data[:, 9]
- i1_365nm = i1_365nm[~np.isnan(i1_365nm)]
- yerror = i1_405nm*0.01+0.005*6
- xerror = V1_405nm*0.01+0.005*6
- # Set initial values of fit parameters
- # 0 1 2
- # A b c d v0
- pInit = [0, 0, 10, 0.1, -1.25]
- lBounds = [-10, -10, 0, 0, -1.5]
- uBounds = [10, 10, 100, 10, -1]
- nPoints = len(V1_405nm)
- nPars = 5
- # Run fit
- output = least_squares(calcChiSq, pInit, args = (V1_405nm, i1_405nm, xerror, yerror),
- bounds = (lBounds, uBounds))
- # Get least_squares output, stored in array output.x[]
- A = output.x[0]
- b = output.x[1]
- c = output.x[2]
- d = output.x[3]
- V0 = output.x[4]
- # Get errors from our fits using fitStdError(), defined above
- pErrors = fitStdError(output.jac)
- d_A = pErrors[0]
- d_b = pErrors[1]
- d_c = pErrors[2]
- d_d = pErrors[3]
- d_V0 = pErrors[4]
- # Output fit parameters
- print("Fitted parameters a: {0:.2f}, b: {1:.2f}, c: {2:.2f}, d: {3:.2f}, V0: {4:.2f}".format(A, b, c, d ,V0))
- print("Parameter errors: a: {0:.2f}, b: {1:.2f}, c: {2:.2f},d: {3:.2f}, V0: {4:.2f}" .format(d_A, d_b, d_c, d_d, d-V0))
- # Calculate chis**2 per point, summed chi**2 and chi**2/NDF
- chiarr = calcChiSq(output.x, V1_405nm, i1_405nm, xerror, yerror)**2
- chisq = np.sum(chiarr)
- NDF = nPoints - nPars
- chisqndf = chisq/NDF
- print("ChiSq = {:5.2e}, ChiSq/NDF = {:5.2f}.".format(chisq, chisqndf))
- # Calculate fitted y-values using our fit parameters and the original fit function
- xPlot = np.linspace(0.9*np.min(V1_405nm), 1.1*np.max(V1_405nm), 100)
- fitData = fitFunc(output.x, xPlot)
- fig = plt.figure(figsize = (8, 6))
- plt.title('405nM')
- plt.xlabel('V')
- plt.ylabel('I')
- plt.grid(color = 'g')
- plt.errorbar(V1_405nm, i1_405nm, yerror, xerror, marker='.', linestyle='', label='data')
- plt.plot(xPlot, fitData, color = 'r')
- plt.show()
- ###################################################################################
- del yerror
- del xerror
- yerror = i1_436nm*0.01+0.005*6
- xerror = V1_436nm*0.01+0.005*6
- pInit = [0, 0, 10, 0.1, -1.25]
- lBounds = [-10, -10, 0, 0, -1.5]
- uBounds = [10, 10, 100, 10, -1]
- nPoints = len(V1_436nm)
- nPars = 5
- # Run fit
- output = least_squares(calcChiSq, pInit, args = (V1_436nm, i1_436nm, xerror, yerror),
- bounds = (lBounds, uBounds))
- # Get least_squares output, stored in array output.x[]
- A = output.x[0]
- b = output.x[1]
- c = output.x[2]
- d = output.x[3]
- V0 = output.x[4]
- # Get errors from our fits using fitStdError(), defined above
- pErrors = fitStdError(output.jac)
- d_A = pErrors[0]
- d_b = pErrors[1]
- d_c = pErrors[2]
- d_d = pErrors[3]
- d_V0 = pErrors[4]
- # Output fit parameters
- print("Fitted parameters a: {0:.2f}, b: {1:.2f}, c: {2:.2f}, d: {3:.2f}, V0: {4:.2f}".format(A, b, c, d ,V0))
- print("Parameter errors: a: {0:.2f}, b: {1:.2f}, c: {2:.2f},d: {3:.2f}, V0: {4:.2f}" .format(d_A, d_b, d_c, d_d, d-V0))
- # Calculate chis**2 per point, summed chi**2 and chi**2/NDF
- chiarr = calcChiSq(output.x, V1_436nm, i1_436nm, xerror, yerror)**2
- chisq = np.sum(chiarr)
- NDF = nPoints - nPars
- chisqndf = chisq/NDF
- print("ChiSq = {:5.2e}, ChiSq/NDF = {:5.2f}.".format(chisq, chisqndf))
- # Calculate fitted y-values using our fit parameters and the original fit function
- xPlot = np.linspace(0.9*np.min(V1_436nm), 1.1*np.max(V1_436nm), 100)
- fitData = fitFunc(output.x, xPlot)
- fig = plt.figure(figsize = (8, 6))
- plt.title('436 nM')
- plt.xlabel('x')
- plt.ylabel('y')
- plt.grid(color = 'g')
- plt.errorbar(V1_436nm, i1_436nm, yerror, xerror, marker='.', linestyle='', label='data')
- plt.plot(xPlot, fitData, color = 'r')
- plt.show()
- ##############################################################################################################
- del yerror
- del xerror
- yerror = i1_546nm*0.01+0.005*6
- xerror = V1_546nm*0.01+0.005*6
- # Set initial values of fit parameters
- # 0 1 2
- # A b c d v0
- pInit = [0, 0, 10, 0.1, -1.25]
- lBounds = [-10, -10, 0, 0, -1.5]
- uBounds = [10, 10, 100, 10, -1]
- nPoints = len(V1_546nm)
- nPars = 5
- # Run fit
- output = least_squares(calcChiSq, pInit, args = (V1_546nm, i1_546nm, xerror, yerror),
- bounds = (lBounds, uBounds))
- # Get least_squares output, stored in array output.x[]
- A = output.x[0]
- b = output.x[1]
- c = output.x[2]
- d = output.x[3]
- V0 = output.x[4]
- # Get errors from our fits using fitStdError(), defined above
- pErrors = fitStdError(output.jac)
- d_A = pErrors[0]
- d_b = pErrors[1]
- d_c = pErrors[2]
- d_d = pErrors[3]
- d_V0 = pErrors[4]
- # Output fit parameters
- print("Fitted parameters a: {0:.2f}, b: {1:.2f}, c: {2:.2f}, d: {3:.2f}, V0: {4:.2f}".format(A, b, c, d ,V0))
- print("Parameter errors: a: {0:.2f}, b: {1:.2f}, c: {2:.2f},d: {3:.2f}, V0: {4:.2f}" .format(d_A, d_b, d_c, d_d, d-V0))
- # Calculate chis**2 per point, summed chi**2 and chi**2/NDF
- chiarr = calcChiSq(output.x, V1_546nm, i1_546nm, xerror, yerror)**2
- chisq = np.sum(chiarr)
- NDF = nPoints - nPars
- chisqndf = chisq/NDF
- print("ChiSq = {:5.2e}, ChiSq/NDF = {:5.2f}.".format(chisq, chisqndf))
- # Calculate fitted y-values using our fit parameters and the original fit function
- xPlot = np.linspace(0.9*np.min(V1_546nm), 1.1*np.max(V1_546nm), 100)
- fitData = fitFunc(output.x, xPlot)
- fig = plt.figure(figsize = (8, 6))
- plt.title('546nM')
- plt.xlabel('V')
- plt.ylabel('I')
- plt.grid(color = 'g')
- plt.errorbar(V1_546nm, i1_546nm, yerror, xerror, marker='.', linestyle='', label='data')
- plt.plot(xPlot, fitData, color = 'r')
- plt.show()
- ###########################################################################################################
- del yerror
- del xerror
- yerror = i1_578nm*0.01+0.005*6
- xerror = V1_578nm*0.01+0.005*6
- # Set initial values of fit parameters
- # 0 1 2
- # A b c d v0
- pInit = [0, 0, 10, 0.1, -1.25]
- lBounds = [-10, -10, 0, 0, -1.5]
- uBounds = [10, 10, 100, 10, -1]
- nPoints = len(V1_578nm)
- nPars = 5
- # Run fit
- output = least_squares(calcChiSq, pInit, args = (V1_578nm, i1_578nm, xerror, yerror),
- bounds = (lBounds, uBounds))
- # Get least_squares output, stored in array output.x[]
- A = output.x[0]
- b = output.x[1]
- c = output.x[2]
- d = output.x[3]
- V0 = output.x[4]
- # Get errors from our fits using fitStdError(), defined above
- pErrors = fitStdError(output.jac)
- d_A = pErrors[0]
- d_b = pErrors[1]
- d_c = pErrors[2]
- d_d = pErrors[3]
- d_V0 = pErrors[4]
- # Output fit parameters
- print("Fitted parameters a: {0:.2f}, b: {1:.2f}, c: {2:.2f}, d: {3:.2f}, V0: {4:.2f}".format(A, b, c, d ,V0))
- print("Parameter errors: a: {0:.2f}, b: {1:.2f}, c: {2:.2f},d: {3:.2f}, V0: {4:.2f}" .format(d_A, d_b, d_c, d_d, d-V0))
- # Calculate chis**2 per point, summed chi**2 and chi**2/NDF
- chiarr = calcChiSq(output.x, V1_578nm, i1_578nm, xerror, yerror)**2
- chisq = np.sum(chiarr)
- NDF = nPoints - nPars
- chisqndf = chisq/NDF
- print("ChiSq = {:5.2e}, ChiSq/NDF = {:5.2f}.".format(chisq, chisqndf))
- # Calculate fitted y-values using our fit parameters and the original fit function
- xPlot = np.linspace(0.9*np.min(V1_578nm), 1.1*np.max(V1_578nm), 100)
- fitData = fitFunc(output.x, xPlot)
- fig = plt.figure(figsize = (8, 6))
- plt.title('578nM')
- plt.xlabel('V')
- plt.ylabel('I')
- plt.grid(color = 'g')
- plt.errorbar(V1_578nm, i1_578nm, yerror, xerror, marker='.', linestyle='', label='data')
- plt.plot(xPlot, fitData, color = 'r')
- plt.show()
- ##############################################################################
- del yerror
- del xerror
- yerror = i1_365nm*0.01+0.005*6
- xerror = V1_365nm*0.01+0.005*6
- # Set initial values of fit parameters
- # 0 1 2
- # A b c d v0
- pInit = [0, 0, 10, 0.1, -1.25]
- lBounds = [-10, -10, 0, 0, -1.5]
- uBounds = [10, 10, 100, 10, -1]
- nPoints = len(V1_365nm)
- nPars = 5
- # Run fit
- output = least_squares(calcChiSq, pInit, args = (V1_365nm, i1_365nm, xerror, yerror),
- bounds = (lBounds, uBounds))
- # Get least_squares output, stored in array output.x[]
- A = output.x[0]
- b = output.x[1]
- c = output.x[2]
- d = output.x[3]
- V0 = output.x[4]
- # Get errors from our fits using fitStdError(), defined above
- pErrors = fitStdError(output.jac)
- d_A = pErrors[0]
- d_b = pErrors[1]
- d_c = pErrors[2]
- d_d = pErrors[3]
- d_V0 = pErrors[4]
- # Output fit parameters
- print("Fitted parameters a: {0:.2f}, b: {1:.2f}, c: {2:.2f}, d: {3:.2f}, V0: {4:.2f}".format(A, b, c, d ,V0))
- print("Parameter errors: a: {0:.2f}, b: {1:.2f}, c: {2:.2f},d: {3:.2f}, V0: {4:.2f}" .format(d_A, d_b, d_c, d_d, d-V0))
- # Calculate chis**2 per point, summed chi**2 and chi**2/NDF
- chiarr = calcChiSq(output.x, V1_365nm, i1_365nm, xerror, yerror)**2
- chisq = np.sum(chiarr)
- NDF = nPoints - nPars
- chisqndf = chisq/NDF
- print("ChiSq = {:5.2e}, ChiSq/NDF = {:5.2f}.".format(chisq, chisqndf))
- # Calculate fitted y-values using our fit parameters and the original fit function
- xPlot = np.linspace(0.9*np.min(V1_365nm), 1.1*np.max(V1_365nm), 100)
- fitData = fitFunc(output.x, xPlot)
- fig = plt.figure(figsize = (8, 6))
- plt.title('365nM')
- plt.xlabel('V')
- plt.ylabel('I')
- plt.grid(color = 'g')
- plt.errorbar(V1_365nm, i1_365nm, yerror, xerror, marker='.', linestyle='', label='data')
- plt.plot(xPlot, fitData, color = 'r')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement