Index: branch/CLEMENT/sp4a3/sp4a3_kalman.c
===================================================================
--- branch/CLEMENT/sp4a3/sp4a3_kalman.c	(revision 422)
+++ branch/CLEMENT/sp4a3/sp4a3_kalman.c	(revision 423)
@@ -7,12 +7,23 @@
 #include "sp4a3_kalman_extra.h"
 
 
-void Add_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double b[nb][mb], double out[na][ma]){
-
+void Add_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double b[nb][mb], double out[na][ma]){
+
+    int i;
+    int j;
+    for(i = 0; i < na ; i++){
+        for(j = 0; j < ma; j++){
+            out[i][j] = A[i][j] + b[i][j];
+        }
+    }
 }
 
-void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
-
+void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
+    double determinant = A[0][0] * A[1][1] - A[0][1] * A[1][0];
+    B[0][0] = A[1][1]/determinant;
+    B[0][1] = -A[0][1] / determinant;
+    B[1][0] = -A[1][0] / determinant;
+    B[1][1] = A[0][0] / determinant;
 }
 
 void Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
@@ -22,12 +33,42 @@
 			R[j][i]=A[i][j];
 }
 
-void Sub_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double b[nb][mb], double out[na][ma]){
+void Sub_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double b[nb][mb], double out[na][ma]){
 
+    int i;
+    int j;
+    for(i = 0; i < na ; i++){
+        for(j = 0; j < ma; j++){
+            out[i][j] = A[i][j] - b[i][j];
+        }
+    }
+
 }
 
 void Mul_Mat_Mat(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb], double out[na][mb]){
-
+    int i;
+    int j;
+    int k ;
+    for(i = 0; i < na; i++){
+        for(j = 0; j < mb; j++){
+            out[i][j] = 0;
+            for(k = 0; k < ma; k++){
+                out[i][j] = out[i][j] + A[i][k] * B[k][j];
+            }
+        }
+    }
+}
+
+void Copie(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb]){
+
+    int i;
+    int j;
+    for(i = 0; i < na ; i++){
+        for(j = 0; j < ma; j++){
+            B[i][j] = A[i][j];
+        }
+    }
+
 }
 
 
@@ -154,24 +195,76 @@
 		{
 			t -= t0;x -= x0;y -= y0;
 
-			debug=0; ///Mettre à 1 pour afficher les matrices.
-			///Ajouter votre code ci-dessous///
+			debug=1; //Mettre à 1 pour afficher les matrices.
+
 			// Kalman
 
-			// X = F*X
+			// X = F*X
+			Plot_Mat(X," X(k|k) = ");
+            double Temp_X[4][1];
+            Copie(4,1, X, 4,1,Temp_X);
+			Mul_Mat_Mat(4, 4, F, 4, 1, Temp_X, X);
 			Plot_Mat(X," X(k+1|k) = ");
 
-			//P = F*P*F'+Q;
+			//P = F*P*F'+Q;
+			Plot_Mat(P,"P(k|k) = ");
+			double Temp[4][4];
+			double Temp2[4][4];
+			Mul_Mat_Mat(4, 4, F, 4, 4, P, Temp);
+			Mul_Mat_Mat(4,4,Temp, 4, 4, FT, Temp2);
+			Add_Mat_Mat(4,4,Temp2, 4,4,Q, P);
 			Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
 
-			// K = P*H' / ( H*P*H' + R);
+			// K = P*H' / ( H*P*H' + R);
+			double Temp18[2][4];
+			double Temp19[4][2];
+			double Temp20[2][2];
+			double Temp21[4][2];
+			double Temp22[2][2];
+
+            Mul_Mat_Mat(2,4,H,4,4,P,Temp18);
+            Mul_Mat_Mat(2,4,Temp18,4,2,HT,Temp19);
+            Plot_Mat(Temp19,"H.P(k+1|k).HT + R = [4][2]");
+            Add_Mat_Mat(2,2,Temp19, 2, 2, R, Temp20);
+            Plot_Mat(Temp20,"H.P(k+1|k).HT + R = [2][2]");
+            Inverse_Mat_22(2,2,Temp20, Temp22);
+            Plot_Mat(Temp22,"INV(H.P(k+1|k).HT + R) = [2][2]");
+
+            Mul_Mat_Mat(4,4,P, 4,2,HT, Temp21);
+
+            Mul_Mat_Mat(4,2,Temp21, 2,2,Temp22, K);
 			Plot_Mat(K,"K = ");
 
-			//X = X + K*([xb(i);yb(i)]-H*X);
-			//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
+			//X = X + K*([xb(i);yb(i)]-H*X);
+
+			double Temp3[4][1];
+			double Temp4[4][1];
+			double Obs[2][1] = {{x}, {y}};
+			double Temp5[2][1];
+
+            Mul_Mat_Mat(2,4,H, 4, 1, X, Temp3);
+            Sub_Mat_Mat(4,1,Obs, 4, 1, Temp3, Temp4);
+
+            Mul_Mat_Mat(4, 2, K, 2, 1, Temp4, Temp5);
+            Add_Mat_Mat(4, 1, X, 4, 1, Temp5, X);
+
+
+			// Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
             Plot_Mat(X," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
 
-			// P = P - K*H*P;
+			// P = P - K*H*P;
+            double Temp7[4][4];
+            double Temp8[4][4];
+            double Temp30[4][4];
+
+            Mul_Mat_Mat(4, 2, K, 2, 4, H, Temp7);
+            Mul_Mat_Mat(4, 4, Temp7, 4, 4, P, Temp8);
+
+
+            Sub_Mat_Mat(4, 4, P, 4, 4, Temp8, Temp30);
+
+            Copie(4,4,Temp30, 4,4, 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 ///
