Data Science and Computing with Python for Pilots and Flight Test Engineers
Curve Fit
In this section, you will let the computer fit a function of a predetermined form through the points and return the parameters to you.
First we need to import one more functionality into python. From the SciPy scientific computing library, we want to import the function curve_fit (we won’t need any of the rest of the library, so we import just this one function and not the whole SciPy library).
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Create a 2-dimensional array containing the input data we want to plot:
data = np.array([[0, 0.1],
[1, 1.1],
[2, 3.9],
[3, 8.5],
[4, 18],
[5, 24.5]])
xdata = np.array(data[:,0]) # horizontal data values (values of x)
ydata = np.array(data[:,1]) # vertical data values (values of y)
# Remember the first and last value of the data on the x-axis (will be used later for plotting)
x0 = xdata[0] # this takes the first element from array "xarray".
x1 = xdata[-1] # this takes the last element from array "xarray" in Python.
Define the Form of the Fit Function
For fit a function through the data, we must first choose a suitable form. For the data at hand, we shall choose a power law of the form.
$$ y = f(x) = ax^b $$
\(x\) is the variable along the horizontal axis of the plot. \(a\) and \(b\) are parameters which you can choose freely – or which you can let the computer determine for you, such that the resulting
def function3(x, a, b):
""" Defines shape of fit function. x is the variable, a and b are parameters to be determined by the fit. """
y = a * (x**b)
return y
Perform the Curve Fit
Now have the computer perform the curve fit. The computer will try to adjust the function parameters \(a\) and \(b\) such that the difference between the function and the data points is the smallest (using the least squares method). This happens in a black box hidden to the user and you do not need to worry about it at this point.
You will also want to extract the fit parameters the computer found for you and put them in separate variables \(a\) and \(b\). Do not worry about the syntax here, you do not need to modify anything in the cell below.
# Perform curve fit:
popt, pcov = curve_fit(function3, xdata, ydata)
# Extract function parameters a and b from the curve fit above:
a=popt[0]
b=popt[1]
You do not need to understand the details of the above cell. If you need to fit a function in another project, simply copy paste it into a new notebook, and make sure that the function and data are loaded into the proper variables.
# Plot the data points again:
plt.scatter(xdata, ydata, color="blue")
# Plot the function:
x = np.linspace(x0, x1, 200) # set up points where the function will be plotted in te figure.
y = function3(x, a, b)
plt.plot(x, y, "r-", label='a=%5.3f b=%5.3f' %tuple([a, b]))
# Plot the legend in upper right corner of plot:
plt.legend()
# Save plot to a JPEG file and also show it on the screen:
plt.savefig("./jupyter_notebook_plot5.jpg")
plt.show()