![]() |
||
![]() |
Department of
Mathematics and Computer Science |
![]() |
![]() |
CS/Math 166 -- Winter, 2018
|
|
20 points DUE: noon: Monday, March 5, 2018 |
Section 8.1 of Burden, Faires and Burden (10th ed., pp 506-517; 9th ed., pp 497-510; or 8th ed., pp 481-494) describes the theory of least-squares approximation. (This corresponds to the lectures notes pages 5-1 to 5-5 in lecture 12).
This theory has been coded in Matlab/Octave via the function p = polyfit(x,y,n) where x and y are vectors containing the data points for which a polynomial approximation is desired, and n is the degree of the approximation. Thus, if n is 1, for example, the function finds the linear least-squares parameters (i.e., the slope and the y-intercept) and if n is 2, the function gives the 3 quadratic least squares parameters. The output p from polyfit is a vector containing the coefficients for the desired polynomial with the coefficient of the highest power first and the constant term last.
Matlab also provides a function ynew = polyval(p,xnew) which "evaluates" a polynomial whose coefficients are given in the vector p for the x-values given in vector xnew. (Usually, xnew is computed as a tight range of values such as xnew = 2:.1:5 [which computes all values of between 2 and 5 in intervals of 0.1 and stores them in the vector xnew].)
More information can be obtained for polyfit and polyval via the Matlab commands "help polyfit" or "help polyval" and "doc polyfit" or "doc polyval" (examples are included as well).
You can plot the original points (with little circles marking each point) using the Matlab/Octave command plot(x,y,'o'). To plot both the original points and the "line(s)" created by polyval you can use the command plot(x,y,'o',xnew,ynew) or plot(x,y,'o',xnew,ynew1,xnew,ynew2).
This assignment is for you to use Matlab/Octave to create a program to find the linear and quadratic least squares coefficients for the data sets given in Burden, Faires and Burden, 10th ed, page 514 (9th ed, page 507; 8th ed, page 491), problems 4, 5, and 6. Ideally, you would create a script file/program which you can invoke. In the script you would include x and y vectors for each of the three datasets, and then invoke polyfit appropriately to determine the two coefficients for a linear least squares fit and the three for the quadratic least squares fit. To assist you in making sure your answers are correct, please have the code print out all the x, y values for the three problems before giving the two sets coefficients. IN ADDITION, for each of the three datasets, on different plots, plot the points and both the linear and the quadratic approximation curves.
To ensure you get different plots for each dataset that will remain, you can invoke figure(1) before the plot command for the first dataset, figure(2) before the plot command for the second dataset, and figure(3) before the plot command for the final dataset.
As usual, your completed program may be turned in to my office, O'Connor 7, or to my mailbox in O'Connor 31. Make sure to include your (1) script file, (2) diary file with the output of two sets of coefficients for each data set, and (3) plots. Please staple the pages yourself and do not turn in disconnected pages!
This page is maintained by Dennis C. Smolarski, S.J.
Email: dsmolarski "at" scu.edu
Last changed: 21 October 2017.