Do not use pointers or vectors Goals: Developing problem-solving skills, declari
ID: 3705690 • Letter: D
Question
Do not use pointers or vectors
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- (2x), n ?(x)2 ?y-slope * ?? 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"- (ym-y)2 y value and ?? 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 calledExplanation / Answer
/*******************************************************************
***
Program reads a file containing a series of x and y coordinates
and does a linear regression to find the best fit of a line
********************************************************************
**/
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
#define SIZE 200
float slope;
float y_intercept;
float goodness_of_fit;
int input_size;
void readFile(char filename[],float x[],float y[]);
void calc_slope_yintercept(float x[], float y[]);
void calc_good_fit(float x[],float y[]);
int getIndex(float x[], float val);
int main()
{
/****** Coordinates *******/
float x[SIZE];
float y[SIZE];
float input = 0.0f;
char filename[] = "coordinates.txt";
readFile(filename,x,y);
calc_slope_yintercept(x,y);
cout<<" Formula : " << "y = (" << slope << " * x)" << " + " <<
y_intercept <<endl;
calc_good_fit(x,y);
cout<<"Goodness of fit : " << goodness_of_fit <<endl;
cout<<"Enter the value of x value : ";
cin>> input;
int index = getIndex(x,input);
if(index == -1){
cout<<"No such values found in the input" <<endl;
}
else
cout << "Y value : " << y[index] <<endl;
return 0;
}
/**************************************************
*Input : filename - File containing coordinates
*Output : x & y - Arrays containing x and y
coordinates
*Function : Read the input file and store it in
x & y array,
**********************************************/
void readFile(char filename[], float x[], float y[])
{
float x_val, y_val;
int index = 0;
ifstream in;
in.open(filename);
if(!in.is_open())
exit(-1);
while(in >> x_val >>y_val){
x[index] = x_val;
y[index] = y_val;
index++;
}
input_size = index ;
in.close();
}
/**************************************************
*Input : x & y - Arrays containing x and y
coordinates
*Output : slope and y_intercept
*Function : Calculates slope and y_intercept
from the x and y coordinate array
values.
**********************************************/
void calc_slope_yintercept(float x[], float y[])
{
int i =0, N = input_size;
float sum_x = 0.0f, sum_y = 0.0f, sum_xy = 0.0f, sum_x2 = 0.0f;
float num = 0.0f, denom = 0.0f;
for(i=0; i< N ;i++)
{
sum_x += x[i];
sum_y += y[i];
sum_xy += x[i] * y[i];
sum_x2 += x[i] * x[i];
}
num = (sum_x sum_y) - (N (sum_xy));
denom = (sum_x sum_x) - (N sum_x2);
slope = num /denom;
y_intercept = (sum_y - (slope * sum_x)) / N;
}
/**************************************************
*Input : x & y - Arrays containing x and y
coordinates
*Output : goodness of fit
*Function : Calculates goodness of fit
from the x and y coordinate array
values.
**********************************************/
void calc_good_fit(float x[],float y[])
{
float Ym = 0.0f, Yc = 0.0f;
int i=0, N = input_size;
for(i=0; i<N; i++){
Ym = y[i];
Yc = (slope * x[i]) + y_intercept;
goodness_of_fit += (Ym - Yc) * (Ym - Yc);
}
}
/**************************************************
*Input : x - Array containing x coordinates
val - Input value to be searched in 'x'
*Output : index of x_coordinate matching val
*Function : Scans the x coordinate array to find
index of "val".
**********************************************/
int getIndex(float x[], float val)
{
int i = 0;
for(i=0; i< input_size ;i++)
{
if(x[i] == val)
return i;
}
return -1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.