CSC 170 Introduction to Numerical Methods MP5 Instructions / Hints Student Name ________________________________________ Section ___ Date _______ Machine Problem 5 ( MP5 ) Instructions / Hints Part 1 Description This machine problem basically performs the following tasks: • A sequence of ( x , y ) ordered pair values are read from a sequential file named mp5.inp . • As the ordered pairs are read, various variables are additively and / or multiplicatively accumulating the individual x and y values. • A least squares line y = m x + b is formed using these formulas for the slope m and the y - intercept b . m = (sumx * sumy – count * sumxy) / (sumx * sumx – count * sumxx) b = (sumy – m * sumx)/count where sumx is the sum of all of the x values from the ordered pairs sumy is the sum of all of the y values from the ordered pairs count is the count of all the ordered pairs ( x , y ) sumxy is the sum of all of the individual x y products from each ordered pair sumxx is the sum of all of the x squared values from the ordered pairs • The least squares line is then output to a sequential file mp5.out . • The correlation coefficient r is then computed to give us an idea of the " goodness of fit " of the least squares line. r = (count * sumxy – sumx * sumy) / sqrt((count * sumxx – sumx * sumx)*(count * sumyy – sumy * sumy)) • Finally the integral or area under the line of m x + b over the first x to the last x is computed using the following computation. m * xn * xn / 2.0 + b * xn - (m * x0 * x0 / 2.0 + b * x0) © Copyright 2013 by P.E.P. 1 CSC 170 Introduction to Numerical Methods MP5 Instructions / Hints Student Name ________________________________________ Section ___ Date _______ • To calculate the integral, a function named integrate() is utilized. The function receives four parameters m the slope of the least squares line b the y - intercept of the least squares line x0 the first, i.e. the minimal x value from all of the ( x , y ) points xn the last, i.e. the maximal x value from all of the ( x , y ) points and returns the area under the line between x 0 and x n . Part 2 Navigation and File Construction Here are some steps to complete MP5 : (1) Logon your Linux account using your Oakstar login name and your Bxxxxxxxx as your password. server: (2) csc.oakton.edu Use the following command to travel to the mps directory that you created for MP0: cd mps (3) Use the following command to create a new folder for MP 5 : mkdir 5 (4) Use the following command to travel to the 4 directory that you just created for M P5 : cd 5 (5) Use the following command to copy the existing skeletal code for MP 5 : cp /samples/csc/171/mps/5/* . CSC 171 command cp /samples/csc/172/mps/5/* . CSC 172 command cp /samples/csc/173/mps/5/* . CSC 173 command (6) use the following command to open the MP 5 file using the pico text editor: pico mp5.cpp CSC 171 command pico mp5.f90 CSC 172 command pico mp5.java CSC 173 command (7) Modify the initial MP 5 code within pico to complete the program. (8) Save your M P 5 file within pico by using this pico command: Ctrl + o © Copyright 2013 by P.E.P. 2 CSC 170 Introduction to Numerical Methods MP5 Instructions / Hints Student Name ________________________________________ Section ___ Date _______ (9) Exit pico by using this pico command: Ctrl + x (10) Compile your file with this command: (11) (12) g++ mp5.cpp -o mp5 CSC 171 command f90 mp5.f90 -o mp5 CSC 172 command javac mp5.java CSC 173 command Execute ( run ) the program with this command: mp5 CSC 171 command mp5 CSC 172 command java mp5 CSC 173 command List the contents of the output file of your program. cat mp5.out (13) Observe the output of your program. (14) For Spring and Fall Semester students: Submit the file with this command ( where 0c1 is the course section number ) : submit csc1710c1 5 CSC 171 command submit csc1720c1 5 CSC 172 command submit csc1730c1 5 CSC 173 command For Summer Semester students: Submit the file with this command ( where 7c1 is the course section number ) : submit csc1717c1 5 CSC 171 command submit csc1727c1 5 CSC 172 command submit csc1737c1 5 CSC 173 command © Copyright 2013 by P.E.P. 3 CSC 170 Introduction to Numerical Methods MP5 Instructions / Hints Student Name ________________________________________ Section ___ Date _______ Part 3 Starter Code CSC 171 ( C ++ ) A Version of MP5 /************************************************************************ * Name: Your name CSC 171 * Date: Today's date MP 5 ************************************************************************* * Statement: Find the least squares line for the data in mp5.inp and * integrate it from the first x value to the last x value. * Specifications: * Input - a sequence of (x, y) values from a sequential file * Output - to a sequential file * - the least squares line y = m*x + b * - the correlation coefficient * - the integral of m*x+b over the first x to the last ************************************************************************/ #include <fstream> #include <cmath> using namespace std; // prototype of function integrate double integrate(double, double, double, double); int main() { // numeric variable declarations double x, y, sumx, sumy, sumxy, sumxx, sumyy, m, b, firstx, lastx, r; int count; // file variable declarations and initialization ifstream fin; ofstream fout; // 1) file variable initializations fin.open("mp5.inp"); fout.open("mp5.out"); // 2) reduction variable initialization sumx = sumy = sumxy = sumxx = sumyy = 0.0; count = 0; // 3) loop over the input file for (;;) { // 4) attempt to input an ordered pair fin >> x >> y; // 5) test for end of file if (fin.eof()) // 6) leave when true © Copyright 2013 by P.E.P. 4 CSC 170 Introduction to Numerical Methods MP5 Instructions / Hints Student Name ________________________________________ Section ___ Date _______ break; // 7) test for first pass if ( count == 0 ) // 8) save lower limit of integration firstx = x; else // 9) save upper limit of integration lastx = x; // 10) update reduction variables //place additional code here } // 11) calculate slope, y intercept and correlation coefficient m = ________________________________________________________ ; b = ________________________________________________________; r = ________________________________________________________; // 12) display linear equation, correlation coefficient and area fout << "y = " << m << " * x + " << b<< "\n"; fout << "Correlation Coefficient = " << r << endl; fout << "Integral = " << integrate(m, b, firstx, lastx) << endl; // 13) disconnect from files fin.close(); fout.close(); } /*Function integrate * *receives - Slope m of least squares line * - y intercept of least squares line * - limit of integration *returns - the antiderivative m*x^2/2 + b*x evaluated at x *************************************************************************/ // Define function integrate below double integrate(double m, double b, double x0, double xn) { //place additional code here return m * xn * xn / 2.0 + b * xn - (m * x0 * x0 / 2.0 + b * x0); } © Copyright 2013 by P.E.P. 5 CSC 170 Introduction to Numerical Methods MP5 Instructions / Hints Student Name ________________________________________ Section ___ Date _______ CSC 172 ( FORTRAN ) A Version of MP5 !************************************************************************ ! Name: Your name CSC 172 ! Date: Today's date MP 5 !************************************************************************ ! Statement: Find the least squares line for the data in mp5.inp and ! integrate it from the first x value to the last x value. ! Specifications: ! Input - a sequence of (x, y) values from a sequential file ! Output - to a sequential file ! - the least squares line y = m*x + b ! - the correlation coefficient ! - the integral of m*x+b over the first x to the last !***********************************************************************/ program mp5 ! function integrate declaration real, external :: integrate ! numeric variable declarations real :: x, y, sumx, sumy, sumxy, sumxx, sumyy, m, b, firstx, lastx, r integer :: count, ierror ! file variable declarations and initialization integer :: fin = 3, fout = 4 ! 1) file variable initializations open(unit = fin, file = "mp5.inp", action = "read") open(unit = fout, file = "mp5.out", action = "write") ! 2) reduction variable initialization sumx = 0.0 sumy = 0.0 sumxy = 0.0 sumxx = 0.0 sumyy = 0.0 count = 0 ! 3) loop over the input file do ! 4) attempt to input an ordered pair read(fin, *, iostat = ierror) x, y ! 5) test for end of file if (ierror /= 0) then ! 6) leave if true exit endif ! 7) test for first pass if ( count == 0 ) then ! 8) save lower limit of integration firstx = x © Copyright 2013 by P.E.P. 6 CSC 170 Introduction to Numerical Methods MP5 Instructions / Hints Student Name ________________________________________ Section ___ Date _______ else ! 9) save upper limit of integration lastx = x endif ! 10) update reduction variables //place additional code here enddo ! m b r 11) calculate slope, y intercept and correlation coefficient = ______________________________________________________________ = ______________________________________________________________ = ______________________________________________________________ ! 12) display linear equation, correlation coefficient and area write(fout, *) "y = ", m, " * x + ", b write(fout, *) "Correlation Coefficient = ", r write(fout, *) "Integral = ", integrate(m,b,firstx,lastx) ! 13) disconnect from files close(fin) close(fout) end program mp5 ! Function integrate ! ! receives - Slope m of least squares line ! - y intercept of least squares line ! - limit of integration ! returns - the antiderivative m*x^2/2 + b*x evaluated at x !*************************************************************************/ ! Define function integrate below real function integrate( m, b, x0, xn) real, intent(in) :: m, b, x0, xn integrate = m * xn * xn / 2.0 + b * xn - (m * x0 * x0 / 2.0 + b * x0) end function integrate © Copyright 2013 by P.E.P. 7 CSC 170 Introduction to Numerical Methods MP5 Instructions / Hints Student Name ________________________________________ Section ___ Date _______ CSC 173 ( Java ) A Version of MP5 /************************************************************************ * Name: Your name CSC 173 * Date: Today's date MP 5 ************************************************************************* * Statement: Find the least squares line for the data in mp5.inp and * integrate it from the first x value to the last x value. * Specifications: * Input - a sequence of (x, y) values from a sequential file * Output - to a sequential file * - the least squares line y = m*x + b * - the correlation coefficient * - the integral of m*x+b over the first x to the last ************************************************************************/ /* Sample output for mp5.inp y = 2.00115 * x + -2.85169 Correlation Coefficient = 0.997368 Integral = 72.2013 */ import java.io.*; public class mp5 { public static void main(String[] args) { mp5 MP5 = new mp5(); // numeric variable declarations double x, y, sumx, sumy, sumxy, sumxx, sumyy, m, b, firstx = 0, lastx = 0, r; int count; // 1) reduction variable initialization sumx = sumy = sumxy = sumxx = sumyy = 0.0; count = 0; try { // 2) file variable declarations and initializations BufferedReader fin = new BufferedReader( new FileReader("mp5.inp")); PrintWriter fout = new PrintWriter( new BufferedWriter( new FileWriter("mp5.out"))); StreamTokenizer st = new StreamTokenizer(fin); // 3) loop over the input file for (;;) © Copyright 2013 by P.E.P. 8 CSC 170 Introduction to Numerical Methods MP5 Instructions / Hints Student Name ________________________________________ Section ___ Date _______ { // 4) attempt to input an x value st.nextToken(); // 5) test for end of file if (st.ttype == st.TT_EOF) // 6) leave when true break; // 7) input an x value x = st.nval; // 8) attempt to input and input a y value st.nextToken(); y = st.nval; // 9) test for first pass if ( count == 0 ) // 10) save lower limit of integration firstx = x; else // 11) save upper limit of integration lastx = x; // 12) update reduction variables //place additional code here }// for // 13) calculate slope, y intercept and correlation coefficient m = ________________________________________________________ ; b = ________________________________________________________; r = ________________________________________________________; // 14) display linear equation, correlation coefficient and area fout.println("y = " + m + " * x + " + b ); fout.println("Correlation Coefficient = " + r); fout.println("Integral = " + MP5.integrate(m,b,firstx,lastx)); // 15) disconnect from files fin.close(); fout.close(); }// try // 16) catch I/O exceptions catch (FileNotFoundException e) { System.err.println("FileNotFound\n"); } catch (IOException e) © Copyright 2013 by P.E.P. 9 CSC 170 Introduction to Numerical Methods MP5 Instructions / Hints Student Name ________________________________________ Section ___ Date _______ { System.err.println("IOException\n"); } }// main /*Method integrate * *receives - Slope m of least squares line * - y intercept of least squares line * - limit of integration *returns - the antiderivative m*x^2/2 + b*x evaluated at x *************************************************************************/ // Define method integrate below public double integrate(double m, double b, double x0, double xn) { return m * xn * xn / 2.0 + b * xn - (m * x0 * x0 / 2.0 + b * x0); } }// mp5 Contents of the companion data file mp5.inp 0.33 0.63 0.82 0.92 0.99 1.21 1.21 1.27 1.42 1.48 1.6 1.81 1.88 1.99 2.1 2.18 2.19 2.51 2.53 2.58 2.62 2.85 3.05 3.19 -1.93 -1.63 -1.5 -1.02 -0.71 -0.22 -0.76 -0.21 0 0.3 -0.05 0.45 0.58 1.9 2.12 1.06 1.21 1.99 2.04 2.38 2.91 2.63 3.56 3.56 © Copyright 2013 by P.E.P. 10 CSC 170 Introduction to Numerical Methods MP5 Instructions / Hints Student Name ________________________________________ Section ___ Date _______ 3.37 3.59 3.61 3.62 3.85 3.95 4.45 4.54 4.54 4.74 4.82 4.89 5.03 5.12 5.12 5.23 5.26 5.27 5.44 5.63 5.66 5.75 3.64 3.87 4.69 4.98 4.43 5.0 6.12 5.64 5.89 6.99 6.91 7.07 6.62 7.03 7.41 7.73 7.86 7.73 8.25 8.58 8.53 8.68 5.99 6.08 6.19 6.31 6.47 6.55 6.58 7.14 7.31 7.34 7.39 7.46 7.49 7.5 7.62 8.05 8.13 8.91 8.93 8.94 8.99 9.06 9.07 9.59 9.63 9.68 9.75 9.92 9.21 9.62 10.09 9.83 9.92 9.8 11.92 11.73 11.48 12.17 12.94 11.74 12.32 12.97 13.05 11.88 15.94 15.12 15.27 14.85 15.0 15.5 15.82 17.01 16.24 16.91 © Copyright 2013 by P.E.P. 11 CSC 170 Introduction to Numerical Methods MP5 Instructions / Hints Student Name ________________________________________ Section ___ Date _______ 9.98 9.99 17.25 16.67 Part 4 Sample Program Run Here is a sample run of the MP5 problem. Notice that when you run the mp5 executable, the program lacks the usual console input and output. © Copyright 2013 by P.E.P. 12