Hi all,
I’m new on Arduino so I’d like to ask some help the expert like you.
The problem is Converting C++ to Arduino.
I couldn’t find out what I’m missing or wrong.
Please check my code and share your knowledge.
This is my Arduino code and original C++ code is attached.
(Program is about Curve fitting, find out curve function from data)
#define maxnpts 13 /*Maximum data pairs - increase if desired */
#define nterms 3 /*Number of parameters to be fit*/
int param, iteration, nloops, n, cycle, nfree;
int npts; /* Number of data pairs */
double x[maxnpts], y[maxnpts], sigmay[maxnpts]; /*x,y,y, uncertainty*/
double weight[maxnpts]; /*Weighting factor*/
double yfit[maxnpts]; /*calculated values of y*/
double a[nterms], sigmaa[nterms], b[nterms]; /*a[i]=c[i] params*/
double beta[nterms], c[nterms]; /*To be fit by program*/
double finala[nterms], lastsigmaa[nterms];
double alpha[nterms][nterms], arry[nterms][nterms];
double aug[nterms][nterms*2]; /*For matrix inversion */
double deriv[maxnpts][nterms]; /*Derivatives */
double flambda; /*Proportion of gradient search(=0.001 at start)*/
double chisq; /*Variance of residuals in curve fit*/
double chisq1, fchisq, sy;
void curve(void) /*the main routine*/
{
int i;
float a1, a2, a3;
readdata();
for (i=0; i<nterms; i++){
a[i] = 1;
}
flambda = 0.001; iteration = 0; cycle = 0;
curvefit(npts);
iteration++;
Serial.println("A1");
Serial.println(a[1]);
Serial.println("A2");
Serial.println(a[2]);
Serial.println("A3");
Serial.println(a[3]);
// displays();
}
/******************************/
/*The function you are fitting*/
/******************************/
float func(int i)
{
int loops; float value;
if (param==1){
for(loops=0; loops<nterms; loops++) c[loops] = b[loops];
} else {
for(loops=0; loops<nterms; loops++) c[loops] = a[loops];
}
value = c[2]*x[i] + c[1]/x[i] + c[0]; /*van deemter equation*/
return(value);
}
void readdata(void)
{
int n=0;
for(n=0; n<=maxnpts ; n++){
x[n] = Latti[n];
y[n] = Long[n];
Serial.println(Latti[n]);
Serial.println("Xxxxxxxxxxxx");
Serial.println(x[n]);
}
npts = n - 1;
update_data();
}
void chisquare(void)
{
int i;
fchisq = 0;
for (i=0; i<npts; i++) {
fchisq += weight[i] * (y[i] - yfit[i])*(y[i]-yfit[i]);
}
fchisq /= nfree;
}
void calcderivative(void)
{
int i, m; float atemp, delta;
for (m=0; m<nterms; m++){
atemp = a[m]; delta = fabs(a[m]/100000); a[m] = atemp+delta;
for (i=0; i<npts; i++) deriv[i][m] = (func(i) - yfit[i]) / delta;
a[m] = atemp;
}
}
void matrixinvert(void)
{
int i,j,k,ik[nterms], jk[nterms]; float rsave, amax;
for (k=0; k<nterms; k++) {
amax = 0.0;
for (i=k; i<nterms; i++) {
for (j=k; j<nterms; j++) {
if (abs(amax) <= abs(arry[i][j])) {
amax = arry[i][j];
ik[k] = i; jk[k] = j;
}
}
}
i = ik[k];
if (i>k) {
for (j=0; j<nterms; j++){
rsave = arry[k][j];
arry[k][j] = arry[i][j]; arry[i][j]=-1*rsave;
}
}
j = jk[k];
if (j>k) {
for (i=0; i<nterms; i++){
rsave = arry[i][k];
arry[i][k] = arry[i][j]; arry[i][j]=-1*rsave;
}
}
for (i=0; i<nterms; i++) {
if (i !=k) {
arry[i][k] = -1*arry[i][k] / amax;
}
}
for (i=0; i<nterms; i++) {
for (j=0; j<nterms; j++) {
if (j !=k && i !=k) {
arry[i][j] = arry[i][j] + arry[i][k]*arry[k][j];
}
}
}
for (j=0; j<nterms; j++) {
if (j !=k) {
arry[k][j] = arry[k][j] / amax;
}
}
arry[k][k] = 1/amax;
}
for (k=nterms-1; k>-1; k--) {
j = ik[k];
if (j>k) {
for (i=0; i<nterms; i++){
rsave = arry[i][k];
arry[i][k] = -1*arry[i][j]; arry[i][j] = rsave;
}
}
i = jk[k];
if (i>k) {
for (j=0; j<nterms; j++){
rsave = arry[k][j];
arry[k][j] = -1*arry[i][j]; arry[i][j] = rsave;
}
}
}
}
void curvefit(int npoints)
{
int i, j, k;
nfree = npoints - nterms;
for (j=0; j<nterms; j++){
b[j] = beta[j] = 0;
for (k=0;k<+j; k++) alpha[j][k] = 0;
}
param = 0;
for (i=0; i<npoints; i++) yfit[i] = func(i);
chisquare();
chisq1 = fchisq;
calcderivative();
for (i=0; i<npoints; i++) {
for (j=0; j<npoints; j++) {
beta[j] += weight[i]*(y[i]-yfit[i])*deriv[i][j];
for (k=0; k<=j; k++)
alpha[j][k] += (weight[i]*deriv[i][j]*deriv[i][k]);
}
}
for (j=0; j<nterms; j++) {
for (k=0; k<=j; k++) alpha[k][j] = alpha[j][k];
}
nloops = 0;
do {
param = 1;
for (j=0; j<nterms; j++) {
for (k=0; k<nterms; k++)
arry[j][k] = alpha[j][k] / sqrt(alpha[j][j]*alpha[k][k]);
arry[j][j] = flambda +1;
}
matrixinvert();
for (j=0; j<nterms; j++) {
b[j] = a[j];
for (k=0; k<nterms; k++)
b[j] += beta[k]*arry[j][k] / sqrt(alpha[j][j]*alpha[k][k]);
}
for (i=0; i<npoints; i++) yfit[i] = func(i);
chisquare();
if((chisq1 -fchisq)<0) flambda *= 10;
nloops++;
} while (fchisq > chisq1);
for (j=0; j<nterms; j++) {
a[j] = b[j];
sigmaa[j] = sqrt(arry[j][j] / alpha[j][j]);
}
flambda /= 10;
}
void update_data(void)
{
int i;[
for (i=0; i<npts; i++)weight[i] = 1 / (sigmay[i]*sigmay[i]);
}
}
curve_fit_modi.c (5.62 KB)