Index: branch/Morales/sp4a3/sp4a3_kalman.c
===================================================================
--- branch/Morales/sp4a3/sp4a3_kalman.c	(revision 254)
+++ branch/Morales/sp4a3/sp4a3_kalman.c	(revision 255)
@@ -4,30 +4,108 @@
 #include <stdio.h>
 #include <math.h>
 
-#include "sp4a3_kalman_extra.h"
+#include "sp4a3_kalman_extra.h"
 
+void init(int n, int m, double R22 [n][m])
+{
+short i=0,j=0;
+
+for (i=0;i<n;i++)
+    {
+        for (j=0;j<m;j++)
+        {
+            R22[i][j]=0;
+        }
+    }
+}
 
-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])
+{
+
+short i=0,j=0;
+    init (na,ma,OUT); // initialisation Resultat
+
+    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 OUT[n][m])
+{
+short i=0,j=0;
+    init (n,m,OUT); // initialisation Resultat
+
+    if ((A[0][0]*A[1][1]-A[0][1]*A[1][0])==0)
+    {
+        printf ("Erreur d'inversion : determinant de la matrice nul");
+        exit (-1);
+    }
+    for (i=0;i<n;i++)
+    {
+        for (j=0;j<m;j++)
+        {
+            if (i!=j)
+            {
+                OUT[i][j]= -(1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]) * A[i][j]);
+                OUT[i][i]= (1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]) * A[j][j]);
+            }
+        }
+    }
 }
 
-void Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
-	int i,j;
-	for (i=0;i<n;i++)
-		for (j=0;j<m;j++)
-			R[j][i]=A[i][j];
+void Transpose_Mat(int n,int m,double A[n][m],double R[m][n])
+{
+short i=0,j=0;
+    init (m,n,R); // initialisation Resultat
+
+    for (i=0;i<m;i++)
+    {
+        for (j=0;j<n;j++)
+        {
+            R[i][j]=A[j][i];
+        }
+    }
 }
 
-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])
+{
+short i=0,j=0;
+    init (na,ma,OUT); // initialisation Resultat
+
+    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]){
-
+void Mul_Mat_Mat(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb], double OUT[na][mb])
+{
+short i=0,j=0,k=0;
+    init (na,mb,OUT); // initialisation Resultat
+
+    if (ma!=nb)
+    {
+        printf ("Erreur dimension matrice multiplication.");
+        exit (-1);
+    }
+
+    for (i=0;i<na;i++)
+    {
+        for (j=0;j<mb;j++)
+        {
+            for (k=0;k<ma;k++)
+            {
+                OUT[i][j]= OUT[i][j] + (A[i][k]*B[k][j]);
+            }
+        }
+    }
 }
 
 
@@ -85,7 +163,8 @@
 	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");
+	printf("Test unitaires OK.\n");
+    getchar();
 }
 
 int main(int argc,char **argv){
@@ -141,8 +220,9 @@
 
 	while(fscanf(fichier, "%lf %lf %lf", &t, &x, &y)>0){
 		printf("-------------%04d--------------\n",cpt);
-
-		if (cpt ==0)
+
+
+		if (cpt ==0)
 		{
 			t0=t;x0=x;y0=y;
 			x=x-x0;y=y-y0;
@@ -154,27 +234,85 @@
 		{
 			t -= t0;x -= x0;y -= y0;
 
-			debug=0; ///Mettre à 1 pour afficher les matrices.
+			debug=1; ///Mettre à 1 pour afficher les matrices.
 			///Ajouter votre code ci-dessous///
-			// Kalman
+			// Kalman
+        //Matrices temporaires
+            double Temp44[4][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
+            double Temp24[2][4]={{0,0,0,0},{0,0,0,0}};
+            double Temp42[4][2]={{0,0},{0,0},{0,0},{0,0}};
+            double Temp22[2][2]={{0,0},{0,0}};
+            double Temp21[2][1]={{0},{0}};
+            double Temp41[4][1]={{0},{0},{0},{0}};
+            double Res0[4][1];
+            double Res1[4][4];
+            double Res2[2][2];
+            double Res3[4][1];
+            double Res4[4][4];
+
+        //Prediction
+            Plot_Mat(X," X(k|k) = ");
+            //X=F*X
+            Mul_Mat_Mat(4,4,F,4,1,X,Res0);
+            Add_Mat_Mat(4,1,Res0,4,1,Temp41,X);   // Temp41 est initialisée à 0 donc grace à ca on affecte à X la matrice Res0
+            Plot_Mat(X,"X(k+1|k) = F*X(k|k) = ");
+
+            Plot_Mat(P,"P(k|k) = ");
+            //P=F*P*F'+Q
+            Mul_Mat_Mat(4,4,F,4,4,P,Temp44);
+            Mul_Mat_Mat(4,4,Temp44,4,4,FT,Res1);
+            Add_Mat_Mat(4,4,Res1,4,4,Q,P);
+            Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
+
+        //Gain
+            //K=P*H'/(H*P*H'+R)
+            Mul_Mat_Mat(4,4,P,4,2,HT,Temp42);
+            Plot_Mat(Temp42,"P(k+1|k).HT = ");
+
+            Mul_Mat_Mat(2,4,H,4,2,Temp42,Temp22);
+            Add_Mat_Mat(2,2,Temp22,2,2,R,Res2);
+            Plot_Mat(Res2,"H.P(k+1|k).HT + R = ");
+
+            Inverse_Mat_22(2,2,Res2,Temp22);
+            Plot_Mat(Temp22,"INV(H.P(k+1|k).HT + R) = ");
+
+            Mul_Mat_Mat(4,2,HT,2,2,Temp22,Temp42);
+            Mul_Mat_Mat(4,4,P,4,2,Temp42,K);
+
+            Plot_Mat(K,"K = ");
+        //MaJ
+
+            double VECT [2][1]={{x},{y}};
+            Plot_Mat(VECT,"obs = ");
+            //DELTA = Obs - H.X(k+1|k)
+            double Delta [2][1];
+
+            Mul_Mat_Mat(2,4,H,4,1,X,Temp21);
+            Sub_Mat_Mat(2,1,VECT,2,1,Temp21,Delta);
+            Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k) = ");
+
+            //X = X + K*([xb(i);yb(i)]-H*X);
+            Mul_Mat_Mat(4,2,K,2,1,Delta,Temp41);
+            Add_Mat_Mat(4,1,X,4,1,Temp41,Res3);
+            init(4,1,Temp41);                   //Temp41 = 0
+            Add_Mat_Mat(4,1,Temp41,4,1,Res3,X);
 
-			// X = F*X
-			Plot_Mat(X," X(k+1|k) = ");
-
-			//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);
-			Plot_Mat(K,"K = ");
-
-			//X = X + K*([xb(i);yb(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(X," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
+
+            init(2,4,Temp24);
+            // P = P - K*H*P;
+            Mul_Mat_Mat(2,4,H,4,4,P,Temp24);
+            Mul_Mat_Mat(4,2,K,2,4,Temp24,Temp44);
+            Sub_Mat_Mat(4,4,P,4,4,Temp44,Res4);
+
+            init(4,4,Temp44);   //Temp44 est égale à 0.
+            Add_Mat_Mat(4,4,Res4,4,4,Temp44,P); // P=Res3+0 donc on affecte à P la matrice Res3
 
-			// P = P - K*H*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 ///
+			/// La matrice X doit contenir la position filtrée ///
+
 		}
 		t = cpt * dt;
 		dx = (x - oldx)/dt;
