Index: branch/best/sp4a3/sp4a3_kalman.c
===================================================================
--- branch/best/sp4a3/sp4a3_kalman.c	(revision 277)
+++ branch/best/sp4a3/sp4a3_kalman.c	(revision 278)
@@ -10,7 +10,7 @@
 void Add_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double B[nb][mb], double out[na][ma]){
     if ((na == nb) && (ma == mb)){
         for (int i=0;i<na;i++){
-            for (int j=0;j<ma;i++){
+            for (int j=0;j<ma;j++){
                 out[i][j] = A[i][j] + B[i][j];
             }
         }
@@ -18,7 +18,7 @@
 }
 
 void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
-    if (n == m && n == 2){
+    if (n == m && n == 2 && A[0][0]*A[1][1]-A[1][0]*A[0][1] != 0){
         B[0][0] = (1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]))*A[1][1];
         B[0][1] = (1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]))*(-A[0][1]);
         B[1][0] = (1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]))*(-A[1][0]);
@@ -37,7 +37,7 @@
 void Sub_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double B[nb][mb], double out[na][ma]){
     if (na == nb && ma == mb){
         for (int i=0;i<na;i++){
-            for (int j=0;j<ma;i++){
+            for (int j=0;j<ma;j++){
                 out[i][j] = A[i][j] - B[i][j];
             }
         }
@@ -47,17 +47,25 @@
 void Mul_Mat_Mat(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb], double out[na][mb]){
     if (ma == nb){
         for (int i=0;i<na;i++){
-            for (int j = 0;j<mb;i++){
+            for (int j=0;j<mb;j++){
+                out[i][j] = 0; //On initialise un coef de la matrice à 0
                 for (int k=0;k<ma;k++){
-                    out[i][j] += A[i][k]*B[k][j];
+                    out[i][j] += (A[i][k])*(B[k][j]);
                 }
             }
         }
     }
 }
+
+void Cop_Mat(int n,int m, double A[n][m], double out[n][m]){
+    for (int i =0;i<n;i++){
+        for (int j=0;j<m;j++){
+            out[i][j] = A[i][j];
+        }
+    }
+}
 
 
-
 void tests_unitaires(void){
 	//Matrices d'entrée
 	double T21a[2][1]={{7},{-5}};
@@ -98,19 +106,19 @@
 	Transpose_Mat(4,4,T44a,R44);	if (!Equal_Mat_Mat(RTT44,R44)) 		error("Erreur calcul Transposition 4x4");
 	Inverse_Mat_22(2,2,T22a,R22);	if (!Equal_Mat_Mat(RInvT22,R22)) 	error("Erreur calcul Inversion 2x2");
 	Add_Mat_Mat(2,2,T22a,2,2,T22b,R22);	if (!Equal_Mat_Mat(RAT22,R22)) 		error("Erreur calcul Addition 2x2");
-	//Add_Mat_Mat(4,4,T44a,4,4,T44b,R44);	if (!Equal_Mat_Mat(RAT44,R44)) 		error("Erreur calcul Addition 4x4");
-	//Add_Mat_Mat(4,1,T41a,4,1,T41b,R41);	if (!Equal_Mat_Mat(RAT41,R41)) 		error("Erreur calcul Addition 4x1");
-	//Sub_Mat_Mat(2,1,T21a,2,1,T21b,R21);	if (!Equal_Mat_Mat(RST21,R21)) 		error("Erreur calcul Soustraction 2x1");
-	//Sub_Mat_Mat(4,4,T44a,4,4,T44b,R44);	if (!Equal_Mat_Mat(RST44,R44)) 		error("Erreur calcul Soustraction 4x4");
-	//Mul_Mat_Mat(4,4,T44a,4,4,T44b,R44);	if (!Equal_Mat_Mat(RMT44T44,R44)) 	error("Erreur calcul Multiplication 4x4 4x4");
-	//Mul_Mat_Mat(4,4,T44a,4,1,T41a,R41);	if (!Equal_Mat_Mat(RMT44T41,R41)) 	error("Erreur calcul Multiplication 4x4 4x1");
-	//Mul_Mat_Mat(4,4,T44a,4,2,T42a,R42);	if (!Equal_Mat_Mat(RMT44T42,R42)) 	error("Erreur calcul Multiplication 4x4 4x2");
-	//Mul_Mat_Mat(4,2,T42a,2,1,T21a,R41);	if (!Equal_Mat_Mat(RMT42T21,R41)) 	error("Erreur calcul Multiplication 4x2 2x1");
-	//Mul_Mat_Mat(4,2,T42a,2,2,T22a,R42);	if (!Equal_Mat_Mat(RMT42T22,R42)) 	error("Erreur calcul Multiplication 4x2 2x2");
-	//Mul_Mat_Mat(4,2,T42a,2,4,T24,R44);	if (!Equal_Mat_Mat(RMT42T24,R44)) 	error("Erreur calcul Multiplication 4x2 2x4");
-	//Mul_Mat_Mat(2,4,T24,4,1,T41a,R21);	if (!Equal_Mat_Mat(RMT24T41,R21)) 	error("Erreur calcul Multiplication 2x4 4x1");
-	//Mul_Mat_Mat(2,4,T24,4,2,T42a,R22);	if (!Equal_Mat_Mat(RMT24T42,R22)) 	error("Erreur calcul Multiplication 2x4 4x2");
-	//Mul_Mat_Mat(2,4,T24,4,4,T44a,R24);	if (!Equal_Mat_Mat(RMT24T44,R24)) 	error("Erreur calcul Multiplication 2x4 4x4");
+	Add_Mat_Mat(4,4,T44a,4,4,T44b,R44);	if (!Equal_Mat_Mat(RAT44,R44)) 		error("Erreur calcul Addition 4x4");
+	Add_Mat_Mat(4,1,T41a,4,1,T41b,R41);	if (!Equal_Mat_Mat(RAT41,R41)) 		error("Erreur calcul Addition 4x1");
+	Sub_Mat_Mat(2,1,T21a,2,1,T21b,R21);	if (!Equal_Mat_Mat(RST21,R21)) 		error("Erreur calcul Soustraction 2x1");
+	Sub_Mat_Mat(4,4,T44a,4,4,T44b,R44);	if (!Equal_Mat_Mat(RST44,R44)) 		error("Erreur calcul Soustraction 4x4");
+	Mul_Mat_Mat(4,4,T44a,4,4,T44b,R44);	if (!Equal_Mat_Mat(RMT44T44,R44)) 	error("Erreur calcul Multiplication 4x4 4x4");
+	Mul_Mat_Mat(4,4,T44a,4,1,T41a,R41);	if (!Equal_Mat_Mat(RMT44T41,R41)) 	error("Erreur calcul Multiplication 4x4 4x1");
+	Mul_Mat_Mat(4,4,T44a,4,2,T42a,R42);	if (!Equal_Mat_Mat(RMT44T42,R42)) 	error("Erreur calcul Multiplication 4x4 4x2");
+	Mul_Mat_Mat(4,2,T42a,2,1,T21a,R41);	if (!Equal_Mat_Mat(RMT42T21,R41)) 	error("Erreur calcul Multiplication 4x2 2x1");
+	Mul_Mat_Mat(4,2,T42a,2,2,T22a,R42);	if (!Equal_Mat_Mat(RMT42T22,R42)) 	error("Erreur calcul Multiplication 4x2 2x2");
+	Mul_Mat_Mat(4,2,T42a,2,4,T24,R44);	if (!Equal_Mat_Mat(RMT42T24,R44)) 	error("Erreur calcul Multiplication 4x2 2x4");
+	Mul_Mat_Mat(2,4,T24,4,1,T41a,R21);	if (!Equal_Mat_Mat(RMT24T41,R21)) 	error("Erreur calcul Multiplication 2x4 4x1");
+	Mul_Mat_Mat(2,4,T24,4,2,T42a,R22);	if (!Equal_Mat_Mat(RMT24T42,R22)) 	error("Erreur calcul Multiplication 2x4 4x2");
+	Mul_Mat_Mat(2,4,T24,4,4,T44a,R24);	if (!Equal_Mat_Mat(RMT24T44,R24)) 	error("Erreur calcul Multiplication 2x4 4x4");
 	printf("Test unitaires OK.\n");
 }
 
@@ -163,8 +171,53 @@
 						 {0, 0, 1, 0},
 						 {0, 0, 0, 1}};
 	double FT[4][4];
-	Transpose_Mat(4,4,F,FT);
+	Transpose_Mat(4,4,F,FT);
+	double tampon22[2][2] = {{0, 0},
+                             {0, 0}};
+    double tampon22bis[2][2] = {{0, 0},
+                                {0, 0}};
+    double tampon22bisbis[2][2] = {{0, 0},
+                                   {0, 0}};
+    double tampon24[2][4] = {{0, 0, 0, 0},
+                             {0, 0, 0, 0}};
+    double tampon42[4][2] = {{0, 0},
+                             {0, 0},
+                             {0, 0},
+                             {0, 0}};
+    double tampon44[4][4] = {{0, 0, 0, 0},
+                             {0, 0, 0, 0},
+                             {0, 0, 0, 0},
+                             {0, 0, 0, 0}};
+    double tampon44bis[4][4] = {{0, 0, 0, 0},
+                                {0, 0, 0, 0},
+                                {0, 0, 0, 0},
+                                {0, 0, 0, 0}};
+    double tampon44bisbis[4][4] = {{0, 0, 0, 0},
+                                {0, 0, 0, 0},
+                                {0, 0, 0, 0},
+                                {0, 0, 0, 0}};
+    double tampon44bisbisbis[4][4] = {{0, 0, 0, 0},
+                                {0, 0, 0, 0},
+                                {0, 0, 0, 0},
+                                {0, 0, 0, 0}};
+    double tampon44bisbisbisbis[4][4] = {{0, 0, 0, 0},
+                                {0, 0, 0, 0},
+                                {0, 0, 0, 0},
+                                {0, 0, 0, 0}};
+    double tampon21[2][1] = {{0},
+                             {0}};
+    double tampon41[4][1] = {{0},
+                             {0},
+                             {0},
+                             {0}};
+    double tampon41bis[4][1] = {{0},
+                             {0},
+                             {0},
+                             {0}};
+    double obs[2][1];
+    double Delta[2][1];
 
+
 	while(fscanf(fichier, "%lf %lf %lf", &t, &x, &y)>0){
 		printf("-------------%04d--------------\n",cpt);
 
@@ -183,21 +236,38 @@
 			debug=0; ///Mettre à 1 pour afficher les matrices.
 			///Ajouter votre code ci-dessous///
 			// Kalman
-
-			// X = F*X
-			Plot_Mat(X," X(k+1|k) = ");
-
-			//P = F*P*F'+Q;
+			// X = F*X
+            Mul_Mat_Mat(4,4,F,4,1,X,tampon41);//tampon41 vaut X(k+1/k)
+			Plot_Mat(tampon41," X(k+1|k) = ");
+			//P = F*P*F'+Q;
+			Mul_Mat_Mat(4,4,F,4,4,P,tampon44);//P=F*P
+            Mul_Mat_Mat(4,4,tampon44,4,4,FT,tampon44bis);//*F'
+            Add_Mat_Mat(4,4,tampon44bis,4,4,Q,P);//+Q
 			Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
-
 			// K = P*H' / ( H*P*H' + R);
-			Plot_Mat(K,"K = ");
-
-			//X = X + K*([xb(i);yb(i)]-H*X);
-			//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
+            Mul_Mat_Mat(2,4,H,4,4,P,tampon24);//H*P
+            Mul_Mat_Mat(2,4,tampon24,4,2,HT,tampon22);//*H'
+            Add_Mat_Mat(2,2,tampon22,2,2,R,tampon22bis);//+R
+            Inverse_Mat_22(2,2,tampon22bis,tampon22bisbis);//Inverse
+            Mul_Mat_Mat(4,2,HT,2,2,tampon22bisbis,tampon42);//H'*
+            Mul_Mat_Mat(4,4,P,4,2,tampon42,K);//P*
+            Plot_Mat(K,"K = ");
+			//X = X + K*([xb(i);yb(i)]-H*X);
+			obs[0][0] = x;
+			obs[1][0] = y;
+			Plot_Mat(obs," Obs = ");
+			Mul_Mat_Mat(2,4,H,4,1,tampon41,tampon21);
+			Sub_Mat_Mat(2,1,obs,2,1,tampon21,Delta);
+			Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
+			// X = X + K.Delta
+            Mul_Mat_Mat(4,2,K,2,1,Delta,tampon41bis);
+            Add_Mat_Mat(4,1,tampon41,4,1,tampon41bis,X);
             Plot_Mat(X," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
-
-			// P = P - K*H*P;
+			// P = P - K*H*P;
+			Mul_Mat_Mat(4,2,K,2,4,H,tampon44bisbis);
+			Mul_Mat_Mat(4,4,tampon44bisbis,4,4,P,tampon44bisbisbis);
+            Sub_Mat_Mat(4,4,P,4,4,tampon44bisbisbis,tampon44bisbisbisbis);
+            Cop_Mat(4,4,tampon44bisbisbisbis,P);
             Plot_Mat(P," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
 
 			/// La matrice X doit contenir la position filtrée ///
