Data Model Building through Function Discovery
At the end of this activity, students are expected to learn:
1. How to import data from Excel to MATLAB?
2. Mathematical relationships describing linear, power, and exponential functions.
3. The procedure to identify an underlying function describing a set of data.
4. How to generate scatter plots of data?
5. Use of first order polynomial fitting to determine the coefficients of underlying functions in the data.
Context for Use
1. Working with Files, and MATLAB Help System
2. Numeric Arrays, and Element-by-Element Operations
3. MATLAB Functions including user-defined functions
4. Advanced plotting
Description and Teaching Materials
In this activity, I introduce the students to the mathematical representation of three function types that are often used to describe physical phenomena. These include:
1.The linear function: y(x) = mx + b.
2. The power function: y(x) = b xm .
3. The exponential function: y(x) = b (10)mx
These functions give a straight line when plotted using a specific set of axis. The linear function gives straight line when plotted on rectilinear axis. Its slope is m and its intercept is b. The power function gives a straight line when plotted on log-log axis. The exponential function give a straight line when plotted on semilog plot whose y-axis is logarithmic. I ask the students to look for straight line on the plot because it will be easy to recognize and they will be able to easily tell whether the function will fit the data well. I demonstrate the following procedure to identify a function that describes a given set of data:
1. Import the data into MATLAB from an Excel spreadsheet.
2. Examine the scatter plot of the imported data near the origin. The exponential function can never pass through the origin unless it is trivial case of b=0. The linear function can pass through the origin only if b=0. The power function can pass through the origin only if m > 0.
3. Looking at the rectilinear plot of the data, if it forms a straight line, then the data can be represented by the linear function and we are done. Otherwise, if there is data at x = 0, then if y(0) = 0, try the power function. If y(0) is not equal to 0, try the exponential function. If data are not given for x = 0, proceed to step 4.
4. If a power function is suspected, plot the data using log-log scale. Only a power function will form a straight line on a log-log plot. If an exponential function is suspected, plot the data using semilog scales. The exponential function will form a straight line on semilog plot.
5. The log-log and semilog plots are only used to identify the function type, and not for finding coefficients b and m. This is because it is difficult to interpolate on log scales.
6. Find the values of b and m using MATLAB's polyfit function. The polyfit function finds the coefficients of a polynomial of specified degree n, that best fits the data in the least-square sense. The function is used to find a first-order polynomial that corresponds to a straight line
7. Use the polyfit to find the coefficients for the three function types as follows:
a. The Linear Function: The coefficients of a linear function are found by typing p = polyfit(x,y,1) in MATLAB. The first element of p will be coefficient m and the second element will be b.
b. The Power Function: The coefficients of power function are found by typing p = polyfit(log10(x), log10(y), 1). Again, the first element of p will be coefficient m and the second element will be log10(b).
c. The Exponential Function: The coefficients of exponential function are found by typing p = polyfit(x, log10(y), 1). The first element will be m, and the second element will be log10(b) from which b can be found.
Teaching Notes and Tips
Before the class, instructor needs to make sure that they have excel spreadsheets of data representing the three function types. The data can be uploaded to a Blackboard course module before class or can be emailed to students to practice with during the class. The instructor can start with explaining and demonstrating the linear function. The students follow the instructor as they go through the three sets of data and develop MATLAB code to discover the underlying functions in the data.
References and Resources
1. William J. Palm III, MATLAB for Engineering Applications, Fourth edition, McGraw-Hill Education, 2019.
Exponential_Data .xlsx (Excel 2007 (.xlsx) 9kB Aug25 19)
Linear_Data.xlsx (Excel 2007 (.xlsx) 8kB Aug25 19)
Power_Data.xlsx (Excel 2007 (.xlsx) 9kB Aug25 19)