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

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
}

Car Power Engine power required to maintain a car moving at a constant speed to overcome the forces of friction. There are several sources of friction: air resistance acting on the car's body, rolling resistance of the tires, and the internal friction in the engine, drive train, and wheel bearings. As the car speed increases this power requirement increases. This is why powerful engines are required to maintain higher speeds. A model for the car power Pvs speed vis The first term on the right depends on the internal forces f, including rolling friction. The last term is due to air resistance. This is modeled by the drag coefficient C, the car's frontal area A, and the air density p. The drag coefficient depends on the properties of the car's surface and is determined experimentally In a particular simulated test involving a sport car, the power is measured for several speeds. This car has a 240 hp (horse-power engine and can reach speeds of 150 mph (miles per hour). The test is performed at 72 F (20°C) on a level ground and at sea level. The value of the parameters for the car are determined to be f -250 lb and A 20 sq ft. The density of air under this conditions is p 30.075 lb-mass. The drag coefficient C is to be determined from the P and v data. The data is v(mph) P (hp) v(mph) P (hp) v(mph) P (hp) v (mph) P(hp) v (mph) P(hp) 15 11.6 45 33.6 75 67.5 105 119.8 135 183.6 20 13.9 50 39.9 80 73.7 110 131.8 140 205.2 25 18.1 55 44.4. 85 82.2 115 144.9 145 224.5 30 20.6 60 48.3 90 88.0 120 154.0 150 230.1 35 26, 65 54.1 95 102.4 125 159.6 40 29.8 70 58.2 100 102.8 130 168.3 this case, it can be done by letting x v 3 and y P- (2.667E-3) f v, where the constant is a unit conversion factor. With this transformation (which you do in code) the equation becomes The transformed data can be fitted to this equation by linear regression, and from the slope the value of C can be determined as slope CAD) (1.787 x 10 4) where the constant on the right is a unit conversion factor Problem 4.1 Determine C by transforming Pand vas described, fitting them to a straight line to determine the slope, and solving for Cto two decimal places in the slope equation above. How does the value of C for the car compares to that of a moving sphere with C 0.5?

Explanation / Answer

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;linux/i2c-dev.h&gt;
#include &lt;sys/ioctl.h&gt;
#include &lt;fcntl.h&gt;

void main()
produce I2C bus
   int file;
   char *bus = "/dev/i2c-1";
   if((file = open(bus, O_RDWR)) &lt; 0)
   the information to 20-bits
       float cTemp = (((data[0] &amp; 0x0F) * 65536) + ((data[1] * 256) + data[2])) / a hundred.0;
       float fTemp = (cTemp * one.8) + 32;
       float pressure = (((data[3] &amp; 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] &amp; 0x0F) * 65536) + (data[1] * 256+ data[2])) / a hundred.0;

       // Output information to screen
       printf("Altitude: the troubles.2f meters ", altitude);
   }
}