Frequently, curve fitting will be used to extract results from experimental data. In this section, we demonstrate use of scipy.optimize.curve_fit, the curve fitting function provided within SciPy.
p0 : (Optional) An array containing initial guesses for the fitting parameter values. [INCLUDE FURTHER DISCUSSION OF WHEN THIS MIGHT BE USEFUL TO USE.]
param_cov : The covariance matrix for our fit parameters. For our purposes, it is sufficient to know that numpy.sqrt(numpy.diag(pcov)) approximates the standard deviation of the fit parameters that arises from discrepancies between data and the best fit curve.
While additional input arguments and returns are available for the curve_fit function, the above parameters will be sufficient for all tasks in this text. Please see the official SciPy documentation for further functionality.
Below, we illustrate this curve fitting technique for the case of a laboratory investigation into the behavior of the resistor-based voltage divider shown in FigureΒ 13.3.1.
Here, \(R_\text{pot}\) is a potentiometer (or variable resistor) that the experimenter controls. Content from ChapterΒ 2 can be used to show that the expected circuit behavior should be governed by
This expression is used to create the user-defined function func which has the independent variable (\(R_\text{pot}\) in this case) as its first argument, variables representing each fitting parameter as subsequent arguments, and returns a value based on our fitting function.
If an experiment is conducted to collect \(V_\text{out}\) data as \(R_\text{pot}\) varies, a curve fit can be used to estimate values for \(V_\text{in}\) and \(R_\text{fixed}\) in our experiment. The Python code below demonstrates this process.
[INCLUDE A PREFIGURE PLOT SHOWING RESULT OF THE CODE ALONG WITH THE ACCOMPANYING TEXT OUTPUT.] When generating the synthetic data in the above example, values of \(V_\text{in}=5.0\text{V}\) and \(R_\text{fixed}=5\text{k}\Omega\) were used.