By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,320 Members | 2,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,320 IT Pros & Developers. It's quick & easy.

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

P: 22
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.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define T 10
  5. /*********** global variables which remain constant ****/
  6. 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}};
  7. 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}};
  8. static double U[1][6] = {0, -9.81, 0, 0, 0, 0 };
  9.  
  10. /************* matrix multiplication function **********/
  11.  
  12. void MatrixMult(double M1[][6], double M2[][6], double M3[][6],int m, int n)
  13. {
  14.  
  15. int i, j, k;
  16.  
  17. for(i=0;i<m;i++)
  18. for(j=0;j<m;j++)
  19. {  M3[i][j]=0;
  20. for(k=0;k<n;k++)
  21. M3[i][j] = M3[i][j]+M1[i][k]*M2[k][j];
  22. }
  23. }
  24. /************** vector addition funciton **************/
  25. void VectAdd(double M1[1][6],double M2[1][6], double M3[1][6],int m, int n)
  26. {
  27. int i,j,k;
  28. for(i=0;i<m;i++)
  29. {
  30. for(j=0;j<n;j++)
  31. M3[i][j]= M1[i][j] + M2[i][j];
  32. }
  33. }
  34. /*********** predicted state equation x = A*xi + B*U ******/
  35. double StateEstimate(double x[][6])
  36. {
  37. int i,j,k;
  38. double ax [1][6];
  39. double bu [1][6];
  40.  
  41. MatrixMult(x, A, ax,6,6);
  42. MatrixMult (U,B,bu,1,6);
  43. VectAdd(ax,bu,x,1,6);
  44.  
  45. for(i=0;i<6;i++)
  46. {
  47. printf("\n");
  48. for(j=0;j<6;j++)
  49. {    
  50. printf(" x[%d][%d]= %.2f" ,i,j, x[i][j]);
  51. }
  52. }
  53. printf("\n \n");
  54. /******************* Main Function *********************/
  55. int main(void)
  56. {
  57. int i,j;
  58. double x[1][6] = {0, 0, 0,5, 10, 5};
  59.  
  60. StateEstimate(x);
  61. }
Sorry also forgot to mention that at
MatrixMult(x, A, ax,6,6);
this should be
MatrixMult(x, A, ax,1,6);
Feb 10 '11 #1

✓ answered by Oralloy

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

Share this Question
Share on Google+
3 Replies


Oralloy
Expert 100+
P: 983
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
Feb 11 '11 #2

P: 22
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.
Feb 11 '11 #3

Oralloy
Expert 100+
P: 983
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
Feb 14 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.