def exponential_model(x, A0=1., x0=5.): # Our second model is a simple exponential function # The kwargs in the function header specify parameter defaults. return A0 * np.exp(x / x0) # Read in the measurement data from a yaml file. # For more information on reading/writing kafe2 objects from/to files see TODO xy_data = XYContainer.from_file("data.yml") # Create 2 XYFit objects with the same data but with different model functions linear_fit = XYFit(xy_data=xy_data, model_function=linear_model) exponential_fit = XYFit(xy_data=xy_data, model_function=exponential_model) # Optional: Assign LaTeX strings to parameters and model functions. linear_fit.assign_parameter_latex_names(a='a', b='b') linear_fit.assign_model_function_latex_expression("{a}{x} + {b}") exponential_fit.assign_parameter_latex_names(A0='A_0', x0='x_0') exponential_fit.assign_model_function_latex_expression("{A0} e^{{{x}/{x0}}}") # Perform the fits. linear_fit.do_fit() exponential_fit.do_fit() # Optional: Print out a report on the result of each fit. linear_fit.report() exponential_fit.report() # Optional: Create a plot of the fit results using Plot. p = Plot(fit_objects=[linear_fit, exponential_fit], separate_figures=False) p.plot(fit_info=True)
# -- Next, read the data from an external file # load all data into numpy arrays U, I, T = np.loadtxt('OhmsLawExperiment.dat', unpack=True) # data sigU, sigI, sigT = 0.1, 0.1, 0.1 # uncertainties T0 = 273.15 # 0 degrees C as absolute Temperature (in Kelvin) T -= T0 # Measurements are in Kelvin, convert to °C # -- Finally, go through the fitting procedure # Step 1: perform an "auxiliary" fit to the T(U) data auxiliary_fit = XYFit(xy_data=[U, T], model_function=empirical_T_U_model) # (Optional): Assign names for models and parameters auxiliary_fit.assign_parameter_latex_names(x='U', p2='p_2', p1='p_1', p0='p_0') auxiliary_fit.assign_model_function_expression('{1}*{x}^2 + {2}*{x} + {3}') auxiliary_fit.assign_model_function_latex_expression( r'{1}\,{x}^2 + {2}\,{x} + {3}') # declare errors on U auxiliary_fit.add_error(axis='x', err_val=sigU) # declare errors on T auxiliary_fit.add_error(axis='y', err_val=sigT) # perform the auxiliary fit auxiliary_fit.do_fit() # (Optional) print the results auxiliary_fit.report()