Index: branch/DESFRICHES-DORIA/sp4a3/sp4a3_kalman.c
===================================================================
--- branch/DESFRICHES-DORIA/sp4a3/sp4a3_kalman.c	(revision 574)
+++ branch/DESFRICHES-DORIA/sp4a3/sp4a3_kalman.c	(revision 575)
@@ -8,11 +8,26 @@
 
 
 void Add_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double B[nb][mb], double R[na][ma]){
-
+    int i;
+    int j;
+    for(i=0; i<na; i++) //on parcourt les lignes
+    {
+        for(j=0; j<ma; j++) // on parcourt les colonnes
+        {
+            R[i][j] = A[i][j]+B[i][j];
+        }
+    }
 }
 
 void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
-
+    float det;
+    det = (A[0][0]*A[1][1]-A[1][0]*A[0][1]);
+    if(det !=0){
+    B[0][0]= (A[1][1])/det;
+    B[0][1]= -(A[0][1])/det;
+    B[1][0]= -(A[1][0])/det;
+    B[1][1]= (A[0][0])/det;
+    }
 }
 
 void Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
@@ -23,15 +38,35 @@
 }
 
 void Sub_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double B[nb][mb], double R[na][ma]){
-
+    int i;
+    int j;
+     for(i=0; i<na; i++) //on parcourt les lignes
+    {
+        for(j=0; j<ma; j++) // on parcourt les colonnes
+        {
+            R[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 R[na][mb]){
-
+    int i;
+    int j;
+    int k;
+
+    for(i=0; i<na; i++)
+    {
+        for(j=0; j<mb; j++)
+        {
+            R[i][j] = 0;
+            for(k=0; k<ma; k++)
+                R[i][j] += A[i][k]*B[k][j];
+        }
+    }
 }
 
 
-
 void tests_unitaires(void){
 	//Matrices d'entrée
 	double T21a[2][1]={{7},{-5}};
@@ -136,12 +171,29 @@
 						 {0, 1, 0, dt},
 						 {0, 0, 1, 0},
 						 {0, 0, 0, 1}};
-	double FT[4][4];
+	double FT[4][4];
+	double XA[4][1];
+	double FxP[4][4];
+	double FxPxFT[4][4];
+	double PA[4][4];
+	double PAxHT[4][2];
+	double HxPA[2][4];
+	double HxPAxHT[2][2];
+	double HxPAxHTplusR[2][2];
+	double invHxPAxHTplusR[2][2];
+	double HxXA[2][1];
+	double DELTA[2][1];
+	double KxDELTA[4][1];
+	double XB[4][1];
+	double KxH[4][4];
+	double KxHxPA[4][4];
+	double PB[4][4];
 	Transpose_Mat(4,4,F,FT);
 
 	while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0){
 		printf("-------------%04d--------------\n",cpt);
-
+
+        double Obs[2][1] ={{xobs}, {yobs}};
 		if (cpt ==0)
 		{
 			t0=t;x0=xobs;y0=yobs;
@@ -157,24 +209,46 @@
 			debug=0; ///Mettre à 1 pour afficher les matrices.
 			///Ajouter votre code ci-dessous///
 			// Kalman
+            double Obs[2][1] ={{xobs}, {yobs}};
+			Mul_Mat_Mat(4, 4, F, 4, 1, X, XA);// X = F*X
+			Plot_Mat(XA," X(k+1|k) = ");
 
-			// X = F*X
-			Plot_Mat(X," X(k+1|k) = ");
+            Mul_Mat_Mat(4, 4, F, 4, 4, P, FxP);
+            Mul_Mat_Mat(4, 4, FxP, 4, 4, FT, FxPxFT);
+            Add_Mat_Mat(4, 4, FxPxFT, 4, 4, Q, PA);//P = F*P*F'+Q;
+			Plot_Mat(PA,"P(k+1|k) = F.P(k|k).FT + Q = ");
 
-			//P = F*P*F'+Q;
-			Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
-
-			// K = P*H' / ( H*P*H' + R);
+			Mul_Mat_Mat(4, 4, PA, 4, 2, HT, PAxHT);
+			Mul_Mat_Mat(2, 4, H, 4, 4, PA, HxPA);
+			Mul_Mat_Mat(2, 4, HxPA, 4, 2, HT, HxPAxHT);
+			Add_Mat_Mat(2, 2, HxPAxHT, 2, 2, R, HxPAxHTplusR);
+			Inverse_Mat_22(2, 2, HxPAxHTplusR, invHxPAxHTplusR);
+			Mul_Mat_Mat(4, 2, PAxHT, 2, 2, invHxPAxHTplusR, K);// K = P*H' / ( H*P*H' + R);
 			Plot_Mat(K,"K = ");
 
-			//X = X + K*([xobs(i);yobs(i)]-H*X);
+			Mul_Mat_Mat(2, 4, H, 4, 1, XA, HxXA);
+			Sub_Mat_Mat(2, 1, Obs, 2, 1, HxXA, DELTA);
+			Mul_Mat_Mat(4, 2, K, 2, 1, DELTA, KxDELTA);
+			Add_Mat_Mat(4, 1, XA, 4, 1, KxDELTA, XB);//X = X + K*([xobs(i);yobs(i)]-H*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 = ");
+            Plot_Mat(XB," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
 
-			// P = P - K*H*P;
-            Plot_Mat(P," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
+			Mul_Mat_Mat(4, 2, K, 2, 4, H, KxH);
+			Mul_Mat_Mat(4, 4, KxH, 4, 4, PA, KxHxPA);
+			Sub_Mat_Mat(4, 4, PA, 4, 4, KxHxPA, PB);// P = P - K*H*P;
+            Plot_Mat(PB," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
 
-			/// La matrice X doit contenir la position filtrée ///
+			/// La matrice X doit contenir la position filtrée ///
+			int i;
+			int j;
+            for(i=0; i<4; i++)
+            {
+                X[i][0] = XB[i][0];
+                for(j=0; j<4; j++)
+                {
+                    P[i][j]=PB[i][j];
+                }
+            }
 		}
 		t = cpt * dt;
 		dx = (xobs - oldx)/dt;
