WrittenReport02

.docx

School

University of Utah *

*We aren’t endorsed by this school

Course

3200

Subject

Computer Science

Date

Apr 3, 2024

Type

docx

Pages

20

Uploaded by UltraRoseArmadillo41

Report
CS 3200-001 Spring 2024 Instructor: Martin Berzins Duke Nguyen u1445624 February 13, 2024 Assignment #2 – Written Report (Due date : February 11, 2024 by 11:59PM) What to turn in: For the assignments, we expect both SOURCE CODE and a written REPORT be uploaded as a zip or tarball file to Canvas. Source code for all programs that you write, thoroughly documented. o Include a README file describing how to compile and run your code. Your report should be in PDF format and should stand on its own. o It should describe the methods used. o It should explain your results and contain figures. o It should also answer any questions asked above. o It should cite any sources used for information, including source code. o It should list all of your collaborators. This homework is due on February 11 th at 11:59 pm. If you don't understand these directions, please feel free to email the TAs or come see one of the TAs or the instructor during office hours well in advance of the due date. 1
Problem 1: Question 5.10 of Cleve Moler’s book (50 points) I initially sort a set of (x, y) data points in ascending order of their x-values and then to create a scatter plot of these sorted points. This line sorts the rows of the matrix yy based on the values in its second column. The matrix yy is assumed to contain data points, with each row representing a point. The second column likely contains the x-values of these points. After this line, yy will be rearranged so that its rows are in ascending order of the x-values. % Sort the data based on x values sortrows(yy, 2); sorted_x = yy(:, 2); sorted_y = yy(:, 1); % Plot the sorted data figure; plot(sorted_x, sorted_y, '*' ); xlabel( 'x' ); ylabel( 'y' ); title( 'Sorted and Plotted Data' ); grid on ; 2 (a) (10 points) As your first experiment, use the code to read this data file and sort it and plot it . You should see something like the figure of sample output below.
The first line sorts the rows of the matrix yy based on the values in its second column. The matrix yy is assumed to contain data points, with each row representing a point. The second column likely contains the x-values of these points. After this line, yy will be rearranged so that its rows are in ascending order of the x-values. The next two lines extract the second and first column of the now-sorted matrix yy and stores it in the variable sorted_x and sorted_y respectively. Those columns represent the x and y-values of the data points, now in sorted order. Use the plot method, a graph is obtained as below: Figure 1.1: A graph plotting the given data after being sorted 3
Using the formula above, the sorted data points are now scaled in the range [-1,1] by using the below snipper of codes. % Linear scaling for x and y x_min = min(sorted_x); x_max = max(sorted_x); y_min = min(sorted_y); y_max = max(sorted_y); scaled_y = (2 * sorted_y) / (y_max - y_min) - (y_max + y_min) / (y_max - y_min); scaled_x = (2 * sorted_x) / (x_max - x_min) - (x_max + x_min) / (x_max - x_min); Hence, we will obtain an updated version of the graph. Figure 1.2: A graph plotting the same data set after being sorted and scaled 4 (b) (10 points) Use a linear scaling to put both x and y values in the range [-1,1] such as y i = 2 ( y max y min ) y i ( y max + y min ) ( y max y min )
The below code is structured to execute polynomial fitting at varying degrees, specifically for degrees 5, 10, 15, 20, and 25. It caters to a dataset containing 82 points, with the data already scaled to fit within the range of -1 to 1. The process involves creating a Vandermonde matrix for each polynomial degree, which is then used to perform least squares fitting on the scaled data. This fitting process computes the polynomial coefficients that best approximate the scaled data in a least squares sense. For each polynomial degree, it evaluates the fitted polynomial over a collection of 1001 points in range from -1 to 1. This evaluation helps visualize how well the polynomial fits the data across this range. The results are plotted, showing both the original data points and the corresponding polynomial fit, thus providing a clear visual representation of the fitting quality. % Degree of the polynomial m_values = [5, 10, 15, 20, 25]; n = 82; % Number of data points % Evaluate the polynomial at 1001 points xplot = linspace(-1, 1, 1001); % Loop over each degree m for m = m_values 5 (c) (20 points) Write a program that uses the Monomial Vandermode matrix approximation to produce a least squares approximation to work with this data set. In this case n = 82 as there are 82 data points and m is the degree of the polynomial with m + 1 terms. Use values of m equal to 5, 10, 15, 20, 25. Plot the values of the polynomial by evaluating the Vandemonde polynomial at (say) 1001 points and use plots to show how the different polynomials behave. Contrast the visual appearance of the polynomial in each of the five cases m = 5, 10, 15, 20, 25. Use the supplied regression code on the canvas page as a model if you find that helpful. An easier option is to use the matlab functions polyfit and polyval . If you use these you will not be able to estimate the condition numbers of the matrices. Instead MATLAB will tell you that the matrix is ill-conditioned.
% Create Vandermonde matrix for fitting A = ones(n, m+1); for j = 2:m+1 A(:, j) = scaled_x.^(j-1); end % Perform least squares fitting B = A' * A; c = A' * scaled_y; a = B \ c; % Polynomial coefficients % Create Vandermonde matrix for evaluation Aplot = ones(length(xplot), m+1); for j = 2:m+1 Aplot(:, j) = xplot.^(j-1); end % Evaluate the polynomial yFit = Aplot * a; % Plot the data and the polynomial figure; plot(scaled_x, scaled_y, '*' , xplot, yFit, '-' ); xlabel( 'x' ); ylabel( 'y' ); title([ 'Polynomial Fit with m = ' , num2str(m)]); grid on ; legend( 'true values' , 'best fit line' ) NKK end The code block above performs polynomial fitting using the Vandermonde matrix and least squares method for different polynomial degrees (5, 10, 15, 20, and 25) on a set of 82 data points that have been scaled to the range [-1, 1]. For each specified degree, the code constructs a Vandermonde matrix based on the scaled x-values, solves the least squares problem to determine the polynomial coefficients, and then evaluates these polynomials at 1001 evenly spaced points within [-1, 1]. Each resulting polynomial is plotted against the scaled data, allowing for visual comparison of the fit quality across different polynomial degrees. This procedure illustrates how increasing the polynomial degree affects the fit to the data, highlighting the trade-off between fitting the data closely and potentially overfitting, especially with higher degrees. The graphs obtained for each degree of polynomial regression are as follow: 6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help