Index: branch/SENOUCI/sp4a3/sp4a3_kalman.c
===================================================================
--- branch/SENOUCI/sp4a3/sp4a3_kalman.c	(revision 539)
+++ branch/SENOUCI/sp4a3/sp4a3_kalman.c	(revision 540)
@@ -7,12 +7,25 @@
 #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 R[na][ma]){
-
+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,j;
+    for(i=0;i<na;i++)
+    {
+        for(j=0;j<ma;j++)
+        {
+            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]){
-
+void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m])
+{
+    double det=1/((A[0][0]*A[1][1])-(A[0][1]*A[1][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]){
@@ -22,12 +35,33 @@
 			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 R[na][ma]){
-
+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,j;
+    for(i=0;i<na;i++)
+    {
+        for(j=0;j<ma;j++)
+        {
+            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]){
 
+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,j,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];
+            }
+        }
+    }
 }
 
 
@@ -106,7 +140,7 @@
 	double xobs,yobs;
 	double oldx,oldy;
 	double dx=0,dy=0,dt=0.1;
-	int cpt = 0;
+	int cpt = 0,i,j;
 
 	// kalman param
 	double sigma_etat = 10.0;
@@ -137,7 +171,31 @@
 						 {0, 0, 1, 0},
 						 {0, 0, 0, 1}};
 	double FT[4][4];
-	Transpose_Mat(4,4,F,FT);
+	Transpose_Mat(4,4,F,FT);
+
+	//matrice intermediaire pour calcul
+	double X1[4][1];
+	double FP[4][4];
+	double FPFT[4][4];
+	double P1[4][4]={{sigma_etat*sigma_etat, 0, 0, 0},
+                    {0, sigma_etat*sigma_etat, 0, 0},
+                    {0, 0, 0, 0},
+                    {0, 0, 0, 0}};
+	double PHT[4][2];
+	double HP1[2][4];
+	double HPHT[2][2];
+	double HPHT_R[2][2];
+	double HPHT_R_inv[2][2];
+	double HX1[2][1];
+	double Delta[2][1];
+	double KXY_HX1[4][1];
+    double X2[4][1];
+	double KH[4][4];
+	double KHP1[4][4];
+	double P2[4][4]={{sigma_etat*sigma_etat, 0, 0, 0},
+                    {0, sigma_etat*sigma_etat, 0, 0},
+                    {0, 0, 0, 0},
+                    {0, 0, 0, 0}};
 
 	while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0){
 		printf("-------------%04d--------------\n",cpt);
@@ -156,24 +214,65 @@
 
 			debug=0; ///Mettre à 1 pour afficher les matrices.
 			///Ajouter votre code ci-dessous///
-			// Kalman
+			// Kalman
+
+			double Obs[2][1]={{xobs},{yobs}};
 
-			// X = F*X
-			Plot_Mat(X," X(k+1|k) = ");
+			// X = F*X
+			Mul_Mat_Mat(4,4,F,4,1,X,X1);
+			Plot_Mat(X1," X(k+1|k) = ");
 
-			//P = F*P*F'+Q;
-			Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
+			//P = F*P*F'+Q;
+			Mul_Mat_Mat(4,4,F,4,4,P,FP);
+			Mul_Mat_Mat(4,4,FP,4,4,FT,FPFT);
+			Add_Mat_Mat(4,4,FPFT,4,4,Q,P1);
 
-			// K = P*H' / ( H*P*H' + R);
+			Plot_Mat(P1,"P(k+1|k) = F.P(k|k).FT + Q = ");
+
+			// K = P*H' / ( H*P*H' + R);
+			Mul_Mat_Mat(4,4,P1,4,2,HT,PHT);
+			Mul_Mat_Mat(2,4,H,4,4,P1,HP1);
+			Mul_Mat_Mat(2,4,HP1,4,2,HT,HPHT);
+			Add_Mat_Mat(2,2,HPHT,2,2,R,HPHT_R);
+			Inverse_Mat_22(2,2,HPHT_R,HPHT_R_inv);
+			Mul_Mat_Mat(4,2,PHT,2,2,HPHT_R_inv,K);
+
 			Plot_Mat(K,"K = ");
 
-			//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 = ");
+			//X = X + K*([xobs(i);yobs(i)]-H*X);
+			Mul_Mat_Mat(2,4,H,4,1,X1,HX1);
+			Sub_Mat_Mat(2,1,Obs,2,1,HX1,Delta);
+			Mul_Mat_Mat(4,2,K,2,1,Delta,KXY_HX1);
+			Add_Mat_Mat(4,1,X1,4,1,KXY_HX1,X2);
 
-			// P = P - K*H*P;
-            Plot_Mat(P," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
+			Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
+            Plot_Mat(X2," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
 
+			// P = P - K*H*P;
+			Mul_Mat_Mat(4,2,K,2,4,H,KH);
+			Mul_Mat_Mat(4,4,KH,4,4,P1,KHP1);
+			Sub_Mat_Mat(4,4,P1,4,4,KHP1,P2);
+
+            Plot_Mat(P2," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
+
+            //la position change donc : X(k+1|k) prend la valeur de X(k+1|k+1)
+            for(i=0;i<4;i++)
+            {
+                for(j=0;j<1;j++)
+                {
+                    X[i][j]=X2[i][j];
+                }
+            }
+
+            //la variance du vecteur d'état change à chaque iteration de X : P(k+1|k) prend la valeur de P(k+1|k+1)
+            for(i=0;i<4;i++)
+            {
+                for(j=0;j<4;j++)
+                {
+                    P[i][j]=P2[i][j];
+                }
+            }
+
 			/// La matrice X doit contenir la position filtrée ///
 		}
 		t = cpt * dt;
