11
11
12
12
void printMat (const char *s, double *m, int n){
13
13
Serial.println (s);
14
+ char buf[40 ];
14
15
for (int i = 0 ; i < n; i++) {
15
16
for (int j = 0 ; j < n; j++) {
16
- Serial.printf (" %30.2f\t " , m[i*n+j]);
17
+ snprintf (buf, 40 , " %30.4f\t " , m[i*n+j]);
18
+ Serial.print (buf);
17
19
}
18
20
Serial.println ();
19
21
}
20
22
}
21
23
22
24
void showmat (const char *s, double **m, int n){
23
25
Serial.println (s);
26
+ char buf[40 ];
24
27
for (int i = 0 ; i < n; i++) {
25
28
for (int j = 0 ; j < n; j++){
26
- Serial.printf (" %30.2f\t " , m[i][j]);
29
+ snprintf (buf, 40 , " %30.4f\t " , m[i][j]);
30
+ Serial.print (buf);
27
31
}
28
32
Serial.println ();
29
33
}
@@ -101,14 +105,14 @@ double curveFitPower(double base, int exponent){
101
105
102
106
int fitCurve (int order, int nPoints, double py[], int nCoeffs, double *coeffs) {
103
107
uint8_t maxOrder = MAX_ORDER;
104
- if (nCoeffs != order + 1 ) return ORDER_AND_NCOEFFS_DOES_NOT_MATCH ; // no of coefficients is one larger than the order of the equation
108
+ if (nCoeffs != order + 1 ) return ORDER_AND_NCOEFFS_DO_NOT_MATCH ; // no of coefficients is one larger than the order of the equation
105
109
if (nCoeffs > maxOrder || nCoeffs < 2 ) return ORDER_INCORRECT; // matrix memory hard coded for max of 20 order, which is huge
106
110
if (nPoints < 1 ) return NPOINTS_INCORRECT; // Npoints needs to be positive and nonzero
107
111
int i, j;
108
112
double T[maxOrder] = {0 }; // Values to generate RHS of linear equation
109
113
double S[maxOrder*2 +1 ] = {0 }; // Values for LHS and RHS of linear equation
110
114
double denom; // denominator for Cramer's rule, determinant of LHS linear equation
111
- int x, y;
115
+ double x, y;
112
116
113
117
double px[nPoints]; // Generate X values, from 0 to n
114
118
for (i=0 ; i<nPoints; i++){
@@ -135,28 +139,28 @@ int fitCurve (int order, int nPoints, double py[], int nCoeffs, double *coeffs)
135
139
136
140
double mat[nCoeffs*nCoeffs]; // Temp matrix as det() method alters the matrix given
137
141
cpyArray (masterMat, mat, nCoeffs);
138
- denom = det (mat, nCoeffs, 0 );
142
+ denom = det (mat, nCoeffs, CURVE_FIT_DEBUG );
139
143
cpyArray (masterMat, mat, nCoeffs);
140
144
141
145
// Generate cramers rule mats
142
146
for (i = 0 ; i < nCoeffs; i++){ // Temporary matrix to substitute RHS of linear equation as per Cramer's rule
143
147
subCol (mat, T, i, nCoeffs);
144
- coeffs[nCoeffs-i-1 ] = det (mat, nCoeffs, 0 )/denom; // Coefficients are det(M_i)/det(Master)
148
+ coeffs[nCoeffs-i-1 ] = det (mat, nCoeffs, CURVE_FIT_DEBUG )/denom; // Coefficients are det(M_i)/det(Master)
145
149
cpyArray (masterMat, mat, nCoeffs);
146
150
}
147
151
return 0 ;
148
152
}
149
153
150
154
int fitCurve (int order, int nPoints, double px[], double py[], int nCoeffs, double *coeffs) {
151
155
uint8_t maxOrder = MAX_ORDER;
152
- if (nCoeffs != order + 1 ) return ORDER_AND_NCOEFFS_DOES_NOT_MATCH ; // Number of coefficients is one larger than the order of the equation
156
+ if (nCoeffs != order + 1 ) return ORDER_AND_NCOEFFS_DO_NOT_MATCH ; // Number of coefficients is one larger than the order of the equation
153
157
if (nCoeffs > maxOrder || nCoeffs < 2 ) return ORDER_INCORRECT; // Matrix memory hard coded for max of 20 order, which is huge
154
158
if (nPoints < 1 ) return NPOINTS_INCORRECT; // Npoints needs to be positive and nonzero
155
159
int i, j;
156
160
double T[maxOrder] = {0 }; // Values to generate RHS of linear equation
157
161
double S[maxOrder*2 +1 ] = {0 }; // Values for LHS and RHS of linear equation
158
162
double denom; // denominator for Cramer's rule, determinant of LHS linear equation
159
- int x, y;
163
+ double x, y;
160
164
161
165
for (i=0 ; i<nPoints; i++) {// Generate matrix elements
162
166
x = px[i];
@@ -178,14 +182,14 @@ int fitCurve (int order, int nPoints, double px[], double py[], int nCoeffs, dou
178
182
179
183
double mat[nCoeffs*nCoeffs]; // Temp matrix as det() method alters the matrix given
180
184
cpyArray (masterMat, mat, nCoeffs);
181
- denom = det (mat, nCoeffs, 0 );
185
+ denom = det (mat, nCoeffs, CURVE_FIT_DEBUG );
182
186
cpyArray (masterMat, mat, nCoeffs);
183
187
184
188
// Generate cramers rule mats
185
189
for (i = 0 ; i < nCoeffs; i++){ // Temporary matrix to substitute RHS of linear equation as per Cramer's rule
186
190
subCol (mat, T, i, nCoeffs);
187
- coeffs[nCoeffs-i-1 ] = det (mat, nCoeffs, 0 )/denom; // Coefficients are det(M_i)/det(Master)
191
+ coeffs[nCoeffs-i-1 ] = det (mat, nCoeffs, CURVE_FIT_DEBUG )/denom; // Coefficients are det(M_i)/det(Master)
188
192
cpyArray (masterMat, mat, nCoeffs);
189
193
}
190
194
return 0 ;
191
- }
195
+ }
0 commit comments