2

Please help me any help will be appreciated. I wrote a code in C to solve equations, but I don't know why some equations that I input into this calculator, give me wrong values, the algorithm is correct because I execute it in C and give me the correct answers but when I use it on the arduino sometimes it doesn't give me the correct answer, well here's the full code

and any help will be appreciated.

.
.
.
.

.
.
.
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;
 }

Try the code with this:

 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.

I first run the code in c on the pc, and the algorithm work perfect but when I copy to the arduino it doesn't work as expected the most of the time give the right answer to the linear system but somtimes fail

any help will be appreciated.

user2461687
  • 173
  • 1
  • 13
  • 1
    Can you give us an example of incorrect output, along with what the correct output should be? – baum Jun 12 '15 at 01:13
  • 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: R1 -3.333, R2 3.2, R3 6.33, R4 5.33 but this is a wrong answer becuase this system doesn't have solution – user2461687 Jun 12 '15 at 01:17
  • Please can you help I'm been stuck with this code 2 days :c – user2461687 Jun 12 '15 at 01:35
  • 1
    Why don't you also post the entirety of your code. Even if the parts you removed aren't important to your problem, I can't effectively run (and replicate) your errors without the full code. – baum Jun 12 '15 at 01:36
  • 1
    the full code is in the link, is a notepage, here´s the link, you can modify the code if you want, https://shrib.com/7uymq2yi in the page – user2461687 Jun 12 '15 at 01:43
  • Do you want the code here in the page? – user2461687 Jun 12 '15 at 01:52
  • 2
    Might be easier for other readers. Also, it won't be easily accidentally editable. – baum Jun 12 '15 at 01:52
  • 1
    I agree. Please use proper indentation too. Also, translating important comments from Spanish to english would probably be good. – John M Jun 12 '15 at 01:53
  • Please note that code with good comments and proper indentation will always get more help than code with bad comments and poor indentation :) – John M Jun 12 '15 at 01:56
  • I've made all the changes thanks so much – user2461687 Jun 12 '15 at 02:02

2 Answers2

2

It's a floating point error, the final value you are getting is very close to zero. Demo.

Add a small epsilon value to your final test to allow for floating point inaccuracies:

if(fabs(a[cant-1][cant-1]) < 0.000001){
    lcd.print("No solucion"); /* if there is no solution print this*/
samgak
  • 23,944
  • 4
  • 60
  • 82
1

Without much review - going out on a limb as I've had this error too.

Use floating point function rather than int

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

Other issues may exist.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256