How would you implement the following in C or C with 6 and 1
How would you implement the following in C or C++ with 6 and 12 data points?
Source code would be very helpful.
Table 2. Sign definition of sensor raw measurements
Yb up
| Accelerometer (signed integer) | |||
| Stationary position | Ax | Ay | Az |
| Zb down | 0 | 0 | +1 g |
| Zb up | 0 | 0 | -1 g |
| Yb down | 0 | +1 g | 0 |
| Yb up | 0 | -1 g | 0 |
| Xb down | +1 g | 0 | 0 |
| Xb up | -1 g | 0 | 0 |
Solution
Below given code is used for finding least sqaure method. it is where the output would be stored into the file name given. here row nad coloun size is take as 4 and 3 which would be canged accordingly. The double *calcofcoeffs(double **data) funnction is used to calculate the co efficients
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double **datareading(char *filename);
void freeData(double **data);
double *calcofcoeffs(double **data);
int main()
{
char filename[100];
double **data;
double *result;
printf(\"Please Enter The Name Of The File You Would Like To Use:\ \");
scanf(\"%99s\", filename);
data = datareading(filename);
result = calcofcoeffs(data);
printf(\"poo\");
printf(\"\ A = %.20lf \ \ B = %.20lf \ \ Sigma A = %.20lf \ \ Sigma B = %.20lf\", result[0], result[1], result[2], result[3]);
freeData (data);
free (result);
return 0;
}
double **datareading(char *filename)
{
FILE *userfile; //declares the file.
double **data; //declares double array.
int i, j; //declares loop variables.
int nrows; //declares a variable that tracks the row number when reading in the file.
int ncolumns; //declares and initialises the number of columns of data and means the following loop starts at the correct point.
//allocating memory using malloc for the array to store the data from the file.
data = (double**)malloc(3*sizeof(double*));
for(i=0; i<3; i++)
{
data[i] = (double*)malloc(4*sizeof(double));
}
userfile = fopen(filename,\"r\");
//initialising all the array elements to zero
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
data[i][j] = 0;
}
}
if (userfile==NULL) //check to see if file was found.
{
printf(\"File not found!.\");
return data;
}
nrows=0;
ncolumns=4;
while (ncolumns==4 && nrows<3);
{
ncolumns = fscanf(userfile, \"%lf %lf %lf %lf\ \", &data[nrows][0], &data[nrows][1], &data[nrows][2], &data[nrows][3]);
nrows++;
}
fclose(userfile);
printf(\"File read\ \");
return data;
}
void freeData(double **data)
{
int i;
for (i=0; i<4; i++)
{
free(data[i]);
}
free (data);
return;
}
double *calcofcoeffs(double **data)
{
//declaring and initialising the variables used for summations
int i;
double x;
double y;
double xy;
double xx;
double one;
double sigmai2;
//declaring variables to be given as final results.
double A;
double B;
double sigmaA;
double sigmaB;
//declaring a pointer to be used in int main to display final results.
double *result;
result = (double*)malloc(4*sizeof(double));
i=0;
x=0;
y=0;
xy=0;
xx=0;
one=0;
//a while loop that reads through the data to calculate the summations
while(i<4 && data[i][3]!=0)
{
sigmai2 = data[i][3]*data[i][3];//making a more simple shorthand for calculation
// summations used in working out coeffs A and B.
y+=(data[i][1]/sigmai2);
xx+=((data[i][0]*data[i][0])/sigmai2);
x+=(data[i][0]/sigmai2);
xy+=((data[i][0]*data[i][1])/sigmai2);
one+=(1/sigmai2);
i++;
}
//working out coeffs A and B.
A = ((y*xx)-(x*xy))/((one*xx)-(x*x));
B = ((one*xy)-(x*y))/((one*xx)-(x*x));
//working out errors in coeffs.
sigmaA = sqrt((xx)/((one*xx)-(x*x)));
sigmaB = sqrt((one)/((one*xx)-(x*x)));
//output the values required.
result[0] = A;
result[1] = B;
result[2] = sigmaA;
result[3] = sigmaB;
return result;
}



