#*****************************************************************************80 # ## least_squares_plots: plot data, least squares line, exact formula. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 August 2020 # # Author: # # John Burkardt # cat ( "\n" ) cat ( "least_squares_plots:\n" ) cat ( " ", version$version.string, "\n" ) cat ( " Compare 15 data points with the least squares line\n" ) cat ( " y = A * x + B\n" ) cat ( " which minimizes the root-mean-square error.\n" ) cat ( " The data is actually perturbed values of a quadratic\n" ) cat ( " formula, which is shown for comparison:\n" ) cat ( " y = 1/2 x^2 + 1\n" ) # # Read the 15 data points. # filename = "least_squares1_data.txt" xy_data <- read.table ( filename, header = FALSE ) n = dim(data)[1] # # Read two points on the least squares line # filename = 'least_squares2_data.txt' xy_lsq = read.table ( filename, header = FALSE ) # # Read data on the exact formula. # filename = 'least_squares3_data.txt' xy_exact = read.table ( filename, header = FALSE ) # # Plot three curves on one image. # filename = "least_squares_plots.png" png ( filename ) plot ( xy_lsq[,1], xy_lsq[,2], xlab = "<--- X --->", ylab = "<--- Y --->", main = "Least squares approximation", col = "red", type = 'l', lwd = 4 ) lines ( xy_data[,1], xy_data[,2], type = 'p', col = "blue", pch = 16 ) lines ( xy_exact[,1], xy_exact[,2], type = 'l', col = "green", lwd = 4 ) # # Boy this legend() command is inadequate! # x = -0.5 y = 3.0 legend ( x, y, legend = c ( "red: Least squares fit", "blue: Data", "green: Exact" ) ) grid ( ) cat ( "\n" ) cat ( " Graphics saved as '", filename, "'\n" ) # # Terminate. # cat ( "\n" ) cat ( "least_squares_plots\n" ) cat ( " Normal end of execution.\n" ) quit ( )