【问题标题】:Linear equation solver Arduino,printing wrong answers线性方程求解器 Arduino,打印错误答案
【发布时间】:2015-06-12 01:03:01
【问题描述】:

请帮助我,我们将不胜感激。 我用 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;
 }

用这个试试代码:

 input:
    # size
    4
    elements:
    1 -2 1 1 2
    3 0 2 -2 -8
    0 4 -1 -1 1
    5 0 3 -1 -3 
 output:
 some numbers

 it should print without solution.

我首先在 pc 上运行 c 中的代码,算法运行完美,但是当我复制到 arduino 时,它大部分时间都无法按预期工作,但有时会失败

我们将不胜感激。

【问题讨论】:

  • 你能给我们举一个错误输出的例子,以及正确的输出应该是什么?
  • 你为什么不也发布你的全部代码。即使您删除的部分对您的问题并不重要,如果没有完整的代码,我也无法有效地运行(和复制)您的错误。
  • 完整代码在链接中,是一个notepage,这里是链接,你可以修改代码,shrib.com/7uymq2yi在页面中
  • 对其他读者来说可能更容易。此外,它不会轻易被意外编辑。
  • 我同意。请也使用适当的缩进。此外,将重要的 cmets 从西班牙语翻译成英语可能会很好。

标签: c matrix arduino calculator linear-algebra


【解决方案1】:

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

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

if(fabs(a[cant-1][cant-1]) < 0.000001){
    lcd.print("No solucion"); /* if there is no solution print this*/

【讨论】:

  • 非常感谢,现在它按预期工作了:D thaaaanks
【解决方案2】:

没有太多审查 - 因为我也有这个错误,所以四肢走动。

使用浮点函数而不是int

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

可能存在其他问题。

【讨论】:

  • 我正在做更改以在 arduino 上运行,现在完整的代码就在那里,非常感谢
  • 我进行了更改,但我得到了结果:/
  • @user2461687 你是否在 3 个地方更改了 abs() --> fabs()
  • String num=""; ,,, num=(String)numero; lgtd = num.length(); 暗示代码是标准的而不是 C - 也许是 C++ 或 arduino 扩展。如果是这样,那abs() 可能是错误的线索。祝你好运。
  • 我用来测量字符串的长度以放置点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多