How to return the correct value of M2 in the Matrix Multiply function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Winton
    New Member
    • Jan 2011
    • 22

    How to return the correct value of M2 in the Matrix Multiply function?

    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.
    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);
    }
    Sorry also forgot to mention that at
    MatrixMult(x, A, ax,6,6);
    this should be
    MatrixMult(x, A, ax,1,6);
    Last edited by Niheel; Feb 10 '11, 01:08 PM.
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    There are two problems that I see off the top, from just looking at your code:
    1. In MatrixMult, lines 17 and 18, you use the same limit variable, m, for both loops.
    2. In StateEstimate, line 50, your program should start printing garbage in the second pass through the i loop (i = 1).


    Cheers!
    Oralloy

    Comment

    • Chris Winton
      New Member
      • Jan 2011
      • 22

      #3
      okay i understand what you mean about the first prroblem but not sure on;

      In StateEstimate, line 50, your program should start printing garbage in the second pass through the i loop (i = 1).

      Ta.

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Chris,

        Well, the line 50 loop runs across six array rows. The array you're sending to the function StateEstimate only has one row (your array x at line 59). The remaining five row displays will pick up garbage from the stack.

        Luck!
        Oralloy

        Comment

        Working...