Hello my code below is returning the wrong value when the MatrixMult function is called in StateEstimate for the value M2. When the code is debugged, with a break point at void MatrixMult() and i step through, M2 returns only the first line of A {1, 0, 0, T, 0, 0} where it should return the complete array which i believe is the reason why x gives out garbage and segmentation error at the end instead of {0,0,0,5,10,5}?
Any help would be gratefully aprreciated.
Sorry also forgot to mention that at
MatrixMult(x, A, ax,6,6);
this should be
MatrixMult(x, A, ax,1,6);
Any help would be gratefully aprreciated.
Code:
#include <stdio.h> #include <stdlib.h> #include <math.h> #define T 10 /*********** global variables which remain constant ****/ static double A[6][6] = {{1, 0, 0, T, 0, 0}, {0, 1, 0, 0, T, 0}, {0, 0, 1, 0, 0, T}, {0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 1}}; static double B[6][6] = {{50, 0, 0, 0, 0, 0}, {0, 50, 0, 0, 0, 0}, {0, 0, 50, 0, 0, 0}, {10, 0, 0, 0, 0, 0}, {0, 10, 0, 0, 0, 0}, {0, 0, 10, 0, 0, 0}}; static double U[1][6] = {0, -9.81, 0, 0, 0, 0 }; /************* matrix multiplication function **********/ void MatrixMult(double M1[][6], double M2[][6], double M3[][6],int m, int n) { int i, j, k; for(i=0;i<m;i++) for(j=0;j<m;j++) { M3[i][j]=0; for(k=0;k<n;k++) M3[i][j] = M3[i][j]+M1[i][k]*M2[k][j]; } } /************** vector addition funciton **************/ void VectAdd(double M1[1][6],double M2[1][6], double M3[1][6],int m, int n) { int i,j,k; for(i=0;i<m;i++) { for(j=0;j<n;j++) M3[i][j]= M1[i][j] + M2[i][j]; } } /*********** predicted state equation x = A*xi + B*U ******/ double StateEstimate(double x[][6]) { int i,j,k; double ax [1][6]; double bu [1][6]; MatrixMult(x, A, ax,6,6); MatrixMult (U,B,bu,1,6); VectAdd(ax,bu,x,1,6); for(i=0;i<6;i++) { printf("\n"); for(j=0;j<6;j++) { printf(" x[%d][%d]= %.2f" ,i,j, x[i][j]); } } printf("\n \n"); } /******************* Main Function *********************/ int main(void) { int i,j; double x[1][6] = {0, 0, 0,5, 10, 5}; StateEstimate(x); }
MatrixMult(x, A, ax,6,6);
this should be
MatrixMult(x, A, ax,1,6);
Comment