Index: branch/moubtassime/sp4a3/sp4a3_kalman.c
===================================================================
--- branch/moubtassime/sp4a3/sp4a3_kalman.c	(revision 511)
+++ branch/moubtassime/sp4a3/sp4a3_kalman.c	(revision 512)
@@ -7,12 +7,31 @@
 #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;
+    if((na == nb)&&(ma == mb)){
+        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]){
+    int i,j;
+    double I[2][2];
+
+    I[0][0] = A[1][1];
+    I[0][1] = -A[0][1];
+    I[1][0] = -A[1][0];
+    I[1][1] = A[0][0];
+
+    for(i= 0; i < n; i++){
+        for(j=0;j < m;j++){
+            I[i][j] = 1/(A[0][0]*A[1][1]-A[0][1]*A[1][0])*I[i][j];
+            }
+    }
 }
 
 void Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
@@ -22,11 +41,29 @@
 			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;
+    if((na == nb)&&(ma == mb)){
+        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;
+    if (na==mb) {
+        for (i=0, i<na,i++){
+            for(j=0,j<mb,j++){
+                for (k=0,k<nb,k++){
+                    R[i][j]=A[i][k]*B[k][j];
+                }
+            }
+        }
+    }
 
 }
 
@@ -137,7 +174,23 @@
 						 {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 FX[4][1];
+	double FP[4][4];
+	double FPFtrans[4][4];
+	double PHtrans[4][2];
+	double HP[4][4];
+	double HPHtrans[4][4];
+	double HPHtransR[2][2];
+	double invHPHtransR[2][2];
+    double KH[4][4];
+    double KHP[4][4];
+    double HX[2][1];
+    double HXxy[2][1];
+    double KHXxy [4][1];
+    double XplusKHXxy [4][1];
 
 	while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0){
 		printf("-------------%04d--------------\n",cpt);
@@ -152,27 +205,82 @@
 		}
 		else
 		{
-			t -= t0;xobs -= x0;yobs -= y0;
+			t -= t0;xobs -= x0;yobs -= y0;
 
-			debug=0; ///Mettre à 1 pour afficher les matrices.
+			debug=1; ///Mettre à 1 pour afficher les matrices.
 			///Ajouter votre code ci-dessous///
-			// Kalman
-
-			// X = F*X
+			// Kalman
+
+
+
+
+			// X = F*X
+			Mul_Mat_Mat(4,4,F,4,1,X,FX);
+			for (i=0,i<4,i++){
+                X[i][0]=FX[i][0];
+			}
+
 			Plot_Mat(X," X(k+1|k) = ");
 
-			//P = F*P*F'+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,FPFtrans);
+			Add_Mat_Mat(4,4,FPFtrans,4,4,Q,P);
+
 			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,P,4,2,HT,PHtrans);
+			Mul_Mat_Mat(2,4,H,4,4,P,HP);
+			Mul_Mat_Mat(2,4,HP,4,2,HT,HPHtrans);
+            Add_Mat_Mat(2,2,HPHtrans,2,2,R, HPHtransR);
+			Mul_Mat_Mat(2,4,HP,4,2,HT, HPHtrans);
+            Inverse_Mat_22(2,2,HPHtransR, invHPHtransR);
+            Mul_Mat_Mat(4,2,PHtrans,2,2,invHPHtransR,K);
+
+
 			Plot_Mat(K,"K = ");
 
-			//X = X + K*([xobs(i);yobs(i)]-H*X);
-			//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
+
+
+
+
+
+			//X = X + K*([xobs(i);yobs(i)]-H*X);
+			Mul_Mat_Mat(2,4,H,4,1,X, HX);
+            double xy[2][1] = {{xobs},{yobs}};
+            Sub_Mat_Mat(2,1,xy,2,1,HX, HXxy);
+            Mul_Mat_Mat(4,2,K,2,1,HXxy, KHXxy);
+            Add_Mat_Mat(4,1,X,4,1,KHXxy, XplusKHXxy);
+
+            for(i=0;i<4;i++){
+                X[i][0] = XplusKHXxy[i][0];
+
+            }
+			//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;
+
+			Mul_Mat_Mat(4,2,K,2,4,H, KH);
+			Mul_Mat_Mat(4,4,KH,4,4,P, KHP);
+			Sub_Mat_Mat(4,4,P,4,4,KHP,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 ///
 		}
