using the method of least square code: To find the parameters a and b of a funct
ID: 3579173 • Letter: U
Question
using the method of least square code:
To find the parameters a and b of a function y(x) = a x + b that best fits a set of n-data points given by (xi , yi) for i = 1, …, n
The parameters a and b (the slope and the y intercept) are obtained by analytically finding the values of a and b that minimize the error function
E2 = ?i [yi - (a•xi + b) ]2
This results in the following formulas
a = (<x•y> - <x>•<y>) / (<x•x> - <x>•<x>)
b = <y> - a•<x>
where
<x> = 1/n ?i xi
<y> = 1/n ?i yi
Note that n must be at least 2
INPUT number of data points n, arrays with data x[] and y[]
OUTPUT slope a, intercept b
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
int leastSquares(int, double [], double [], double &, double &);
const int MAXSIZE = 5000;
int main()
{
// Step 1 Read arrays x[] and y[] with ordered pairs of data points (xi , yi)
// Read data file name
string fileName;
cout << "Enter file name containing data arranged in two columns ";
cout << "Maximum number of data points = "<<MAXSIZE<<endl;
cout << "file name = ";
cin >> fileName;
// open data file and check for error
ifstream inFile(fileName);
if(!inFile){
cout << "Error: cannot open "<<fileName<<" ";
return 1;
}
// Read data from file
double x[MAXSIZE], y[MAXSIZE]; // data arrays
int n = 0; // data points
while(n<MAXSIZE && inFile>>x[n]>>y[n])
++n;
inFile.close();
// Check for insufficient data error
if(n<=1){
cout << "Error: at least two pairs of data points required ";
return 1;
}
cout << n << " data points read ";
// Steps 2-9 in leastSquares function
// Call leastSquare function
double a=0, b=0;
if(leastSquares(n, x, y, a, b)){
cout << "Error: at least two pairs of data points required ";
return 1;
}
//Step 10 OUTPUT a and b
cout << " Linear fit: y = a x + b ";
cout << "a = "<<a<<endl;
cout << "b = "<<b<<endl;
return 0;
}
//
// LEAST SQUARES FIT
//
int leastSquares(int n, double x[], double y[], double &a, double &b)
{
// Double check for input errors
if(n<=1)
return 1; // error flag
// Step 2 Set Sx, Sy, Sxy, Sxx equal to 0
double Sx=0, Sy=0, Sxy=0, Sxx=0;
// Step 3 For i = 0 … n-1, do Steps 4-7
for(int i=0; i<n; ++i){
Sx += x[i];
Sy += y[i];
Sxy += x[i] * y[i];
Sxx += x[i] * x[i];
}
// Step 8 Set a = (n*Sxy - Sx*Sy)/(Sxx - Sx*Sx) (slope)
a = (n*Sxy - Sx*Sy)/(n*Sxx - Sx*Sx);
// Step 9 Set b = (Sy - a * Sx)/n (intercept)
b = (Sy - a * Sx)/n;
return 0; // no errors
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
void main()
produce I2C bus
int file;
char *bus = "/dev/i2c-1";
if((file = open(bus, O_RDWR)) < 0)
the information to 20-bits
float cTemp = (((data[0] & 0x0F) * 65536) + ((data[1] * 256) + data[2])) / a hundred.0;
float fTemp = (cTemp * one.8) + 32;
float pressure = (((data[3] & 0x0F) * 65536) + (data[4] * 256 + data[5])) / a hundred.0;
// Output information to screen
printf("Pressue: the troubles.2f Pa ", pressure);
printf("Temperature in stargazer : the troubles.2f C ", cTemp);
printf("Temperature in physicist : the troubles.2f F ", fTemp);
}
// Send OSR and channel setting command(0x44 | 0x01)
config[0] = 0x44 | 0x01;
write(file, config, 1);
sleep(1);
// scan three bytes of information from register(0x31)
// altitude savings bank, altitude csb, altitude lsb
reg[0] = 0x31;
write(file, reg, 1);
if(read(file, data, 3) != 3)
the information to 20-bits
float altitude = (((data[0] & 0x0F) * 65536) + (data[1] * 256+ data[2])) / a hundred.0;
// Output information to screen
printf("Altitude: the troubles.2f meters ", altitude);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.