Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

DONT USE POINTERS OR VECTORS x y 0 4.835 0.04 4.92 0.1 5.143 0.12 5.227 0.22 5.5

ID: 3700308 • Letter: D

Question

DONT USE POINTERS OR VECTORS x y 0 4.835 0.04 4.92 0.1 5.143 0.12 5.227 0.22 5.591 0.24 5.638 0.26 5.704 0.32 5.906 0.34 6.023 0.36 6.026 0.42 6.262 0.44 6.331 0.46 6.399 0.52 6.614 0.58 6.86 0.6 6.872 0.62 6.98 0.64 7.025 0.7 7.3 0.72 7.323 0.8 7.631 0.82 7.654 0.84 7.77 0.9 7.91 1 8.256 1.08 8.556 1.1 8.615 1.12 8.715 1.14 8.777 1.16 8.873 1.22 9.106 1.24 9.119 1.34 9.476 1.48 9.958 1.5 10.032 1.52 10.083 1.54 10.171 1.58 10.337 1.6 10.366 1.62 10.468 1.7 10.751 1.72 10.8 1.74 10.845 1.76 10.955 1.8 11.051 1.88 11.361 1.9 11.433 1.92 11.475 1.98 11.768 2.04 11.973 2.2 12.536 2.22 12.57 2.24 12.665 2.3 12.891 2.32 12.917 2.38 13.09 2.42 13.223 2.48 13.49 2.5 13.512 2.6 13.91 2.62 13.996 2.64 14.062 2.7 14.271 2.76 14.457 2.82 14.688 2.84 14.773 2.9 14.967 2.92 15.034 2.98 15.26 3 15.34 3.18 15.915 3.22 16.034 3.28 16.305 3.3 16.339 3.32 16.4 3.38 16.66 3.4 16.655 3.42 16.763 3.44 16.877 3.5 17.045 2.3 12.891 2.32 12.917 2.38 13.09 2.42 13.223 2.48 13.49 2.5 13.512 2.6 13.91 2.62 13.996 2.64 14.062 2.7 14.271 2.76 14.457 2.82 14.688 2.84 14.773 2.9 14.967 2.92 15.034 2.98 15.26 3 15.34 3.18 15.915 3.22 16.034 3.28 16.305 3.3 16.339 3.32 16.4 3.38 16.66 3.4 16.655 3.42 16.763 3.44 16.877 3.5 17.045
Goals: Developing problem-solving skills, declaring variables, reading data from a file, using loops, and using arrays and functions. Problem: Many engineering and scientific relationships can be manipulated to a format of a straight line, y- mx +b, where m is the slope of the line and b is the y intercept. However, because most measurements have some inherent error, the formula for the line may not be obvious. Therefore, linear regression may be used to determine the best fit of a line to the measured data. Linear regression uses the following two equations for calculations of slope and y-intercept. Slope- 2y-nx y) (2x) -n2(x) 2y -slope Ex y-intercept- where n is the number of (x,y) data sets that were measured. The "goodness of fit" of this straight line can be estimated by summing the square of the residuals. The residual is defined as the difference between the measured y value and the calculated y value (using the calculated slope and y-intercept) for a given x. The equation is shown below where ym is the measured “goodness of fit"-Zym-y)2 y value and yc is the calculated y value. A lower"goodness of fit" value indicates a better match of the line to the data. For this assignment you write the C++ source code that uses a set of data to determine the slope and y-intercept of the line the best fits this data. The data is stored in a file called

Explanation / Answer

#include <iostream>>
#include <fstream>
#include <tuple>
using namespace std;

tuple<double[], double[]> readData(){
    ifstream infile;
    infile.open("linear.txt");
    if(!infile){
    cerr<<"Unable to open file linear.txt";
    exit(1);   // call system to stop
    }
    int number_of_lines = 0;
    string line;

    while(getline(myfile, line)){
        number_of_lines++;
    }
    infile.close();
    double x[number_of_lines];
    double y[number_of_lines];
    infile.open("linear.txt");

    for(int i=0; infile>>x[i]&&i<number_of_lines; i++){
        infile>>y[i];
    }
    infile.close();
    return make_tuple(x,y);
}

tuple<double, double> slopeIntercept(double[] x, double[] y){
    n = sizeof(x)/sizeof(*x);
    double sumX = 0;
    double sumY = 0;
    double sumX2 = 0;
    double sumXY = 0;
    for(int i=0;i<n;i++){
        sumX = sumX + x[i];
        sumY = sumY + y[i];
        sumX2 = sumX2 + x[i]*x[i];
        sumXY = sumXY + x[i]*y[i];
    }
    double slope = (sumX*sumY-n*sumXY)/(sumX*sumX-n*sumX2);

    double intercept = (sumY-slope*sumX)/n;

    return make_tuple(slope, intercept);

}

double goodness(double[] x, double[] y, double slope, double intercept){
    n = sizeof(x)/sizeof(*x);
    double goodnessFit = 0;
    double ym[n];
    for(int i=0;i<n;i++){
        ym[i] = slope*x[i]+intercept;

        goodnessFit = goodnessFit + (ym[i]-y[i])*(ym[i]-y[i]);
    }
    return goodnessFit;
}

int index(double[] x, double xvalue){
    n = sizeof(x)/sizeof(*x);
    double ind;
    for(int i=0;i<n;i++){
        if(x[i]==xvalue){
            ind = i;
            return ind;
        }
    }
    return 0;
}

void main(){
    auto data = readData();
    double[] x = get<0>data;
    double[] y = get<1>data;

    auto equation = slopeIntercept(x,y);

    cout<<"Equation of the line is: y = "<<(get<0>equation)<<"x + "<<(get<0>equation)<<endl;

    double goodnessFit = goodness(x,y,(get<0>equation),(get<0>equation));

    cout<<"Goodness of Fit = "<<goodnessFit<<endl;

    int xvalue;
    cout<<"Please input a x value for searching: ";
    cin>>xvalue;

    cout<<"Corresponding y value is: "<<y[index(x,xvalue)]<<endl;

}