C 线性方程求解器Arduino,打印错误答案

C 线性方程求解器Arduino,打印错误答案,c,matrix,arduino,calculator,linear-algebra,C,Matrix,Arduino,Calculator,Linear Algebra,请帮助我,任何帮助都将不胜感激。 我用C写了一个代码来解方程,但我不知道为什么我输入到这个计算器中的一些方程,给了我错误的值,算法是正确的,因为我用C执行它,给了我正确的答案,但是当我在arduino上使用它时,有时它不会给我正确的答案,这是完整的代码 任何帮助都将不胜感激 . . . . . . . long jordan(){/*this do the gauss elimination for solve the equation*/ long FilaMax=0,k=0; long d

请帮助我,任何帮助都将不胜感激。 我用C写了一个代码来解方程,但我不知道为什么我输入到这个计算器中的一些方程,给了我错误的值,算法是正确的,因为我用C执行它,给了我正确的答案,但是当我在arduino上使用它时,有时它不会给我正确的答案,这是完整的代码

任何帮助都将不胜感激

.
.
.
.

.
.
.
long jordan(){/*this do the gauss elimination for solve the equation*/
long FilaMax=0,k=0;
long double maxEl=0,tmp=0,fracc=0;
   lcd.clear();
for (colum=0; colum<cant-1; colum++) {
    /* search the maximun colum*/
    maxEl = abs(a[colum][colum]);
    FilaMax = colum;
    for (k=colum+1; k<cant; k++) {
        if (abs(a[k][colum]) > maxEl) {
            maxEl = abs(a[k][colum]);
            FilaMax = k;
        }
    }
    /* change the maximunby the actual row*/
    for (k=colum; k<cant+1;k++) {
        tmp = a[FilaMax][k];
        a[FilaMax][k] = a[colum][k];
        a[colum][k] = tmp;
    }
    /*lower cero's triangular matrix it's done here*/
    for (k=colum+1;k<cant; k++) {
        fracc = -a[k][colum]/a[colum][colum];
        for (fila=colum; fila<cant+1; fila++) {
            if (colum==fila) {
                a[k][fila] = 0;
            }else{
                a[k][fila] += fracc * a[colum][fila];
            }
        }
             } 
}
    char sr=' ';
    lcd.setCursor(0,0);
if(a[cant-1][cant-1]==0){
    lcd.print("No solucion"); /* if there is no solution print this*/ 
    do{
    sr=keypad.waitForKey();
    }while(sr!='\n');
}else{                       /*is there values to print*/
for (colum=cant-1; colum>=0; colum--) {
    res[colum] = a[colum][cant]/a[colum][colum];
    for (k=colum-1;k>=0;k--) {
        a[k][cant] -= a[k][colum]*res[colum];
    }
}
colum=0;
    do{     
            lcd.setCursor(0,0);
            lcd.print("R");
            lcd.setCursor(1,0);
            lcd.print(colum+1);
            lcd.setCursor(0,1);
    lcd.print(res[colum],DEC);
            sr=keypad.waitForKey();
            if(sr=='#') colum++;
            if (colum==cant) colum=0;
            if(sr=='\n') break;
    }while(1);
   }
   colum=0;
   fila=0;
   cant=0;
   sr=' ';
  return 0;
 }
我首先在pc上用c语言运行代码,算法运行得很好,但是当我复制到arduino时,它并没有像预期的那样工作。大多数情况下,它给出了线性系统的正确答案,但有时失败了


任何帮助都将不胜感激。

无需多加评论,因为我也犯了这个错误,所以我只能孤注一掷

.
.
.
.

.
.
.
long jordan(){/*this do the gauss elimination for solve the equation*/
long FilaMax=0,k=0;
long double maxEl=0,tmp=0,fracc=0;
   lcd.clear();
for (colum=0; colum<cant-1; colum++) {
    /* search the maximun colum*/
    maxEl = abs(a[colum][colum]);
    FilaMax = colum;
    for (k=colum+1; k<cant; k++) {
        if (abs(a[k][colum]) > maxEl) {
            maxEl = abs(a[k][colum]);
            FilaMax = k;
        }
    }
    /* change the maximunby the actual row*/
    for (k=colum; k<cant+1;k++) {
        tmp = a[FilaMax][k];
        a[FilaMax][k] = a[colum][k];
        a[colum][k] = tmp;
    }
    /*lower cero's triangular matrix it's done here*/
    for (k=colum+1;k<cant; k++) {
        fracc = -a[k][colum]/a[colum][colum];
        for (fila=colum; fila<cant+1; fila++) {
            if (colum==fila) {
                a[k][fila] = 0;
            }else{
                a[k][fila] += fracc * a[colum][fila];
            }
        }
             } 
}
    char sr=' ';
    lcd.setCursor(0,0);
if(a[cant-1][cant-1]==0){
    lcd.print("No solucion"); /* if there is no solution print this*/ 
    do{
    sr=keypad.waitForKey();
    }while(sr!='\n');
}else{                       /*is there values to print*/
for (colum=cant-1; colum>=0; colum--) {
    res[colum] = a[colum][cant]/a[colum][colum];
    for (k=colum-1;k>=0;k--) {
        a[k][cant] -= a[k][colum]*res[colum];
    }
}
colum=0;
    do{     
            lcd.setCursor(0,0);
            lcd.print("R");
            lcd.setCursor(1,0);
            lcd.print(colum+1);
            lcd.setCursor(0,1);
    lcd.print(res[colum],DEC);
            sr=keypad.waitForKey();
            if(sr=='#') colum++;
            if (colum==cant) colum=0;
            if(sr=='\n') break;
    }while(1);
   }
   colum=0;
   fila=0;
   cant=0;
   sr=' ';
  return 0;
 }
使用浮点函数而不是
int

// maxEl = abs(a[colum][colum]);
maxEl = fabs(a[colum][colum]);
// other places too

可能存在其他问题。

这是一个浮点错误,您得到的最终值非常接近于零

在最终测试中添加一个小的epsilon值,以允许浮点不准确:

if(fabs(a[cant-1][cant-1]) < 0.000001){
    lcd.print("No solucion"); /* if there is no solution print this*/
if(fabs(a[cant-1][cant-1])<0.000001){
lcd.print(“无解”);/*如果没有解决方案,请打印此*/

您能给我们一个不正确输出的例子,以及正确的输出应该是什么吗?为什么不发布您的全部代码。即使您删除的部分对您的问题不重要,我也无法有效地运行(和复制)你的错误没有完整的代码。完整的代码在链接中,是一个注释页,这里是链接,你可以修改代码,如果你想的话,在页面中可能对其他读者更容易。而且,它不会很容易被意外地编辑。我同意。请使用适当的缩进。另外,将重要的评论从西班牙语翻译成英语也可能会有帮助很好。我正在做更改以在arduino上运行,现在完整的代码已经存在,谢谢muchI做了更改,但我还没有得到结果:/@user2461687您是否在3个位置更改了
abs()
-->
fabs()
String num=“”;,,num=(String)numero;lgtd=num.length()如果代码是标准的,而不是C——也许是C++或ARDUINO扩展。如果是这样,那<代码> Abess()/代码>可能是错误的引线。祝好运。我用它来测量字符串的长度,以便多多地感谢DoThanaKs。