473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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.
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

3 1799
Oralloy
985 Expert 512MB
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
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
985 Expert 512MB
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

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

Similar topics

3
by: learning_C++ | last post by:
I hope to use template. in my code I hope to sum the vector<int> and vector<double>. But there are several errors: sumtemplate.cpp:6: error: ISO C++ forbids declaration of `sum' with no type...
15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
2
by: eight02645999 | last post by:
hi i use odbc to update a table in a database but i always get return value of -1 even though i tried to return an integer. the table is updated though .... sql = """ update table set column =...
6
by: Hardy Wang | last post by:
Hi all, I have the following codes, but SCOPE_IDENTITY() just returns NULL to me. If I comment out SCOPE_IDENTITY() line and run @@IDENTITY line, it works fine!! Since I have a trigger on the...
8
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers...
2
by: moondaddy | last post by:
I'm working with c# 3.0 and am wondering if I can make a class return a value like a function. For example, I would like to do something like this: Point pt; pt=SomeClass someclass=new...
2
by: ELINTPimp | last post by:
Hello all, Have a really interesting problem (at least to me) with my upload_file() function. I have it working now, with a bit of a work around, but would like to know what everyone thinks in...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
5
by: shanfeng | last post by:
such as a function: f(double v, int a) could this function return both a integer and array? Thank you very much. I have confused on this for a long time~
3
by: raras | last post by:
could somebody help me to write the script of program to multiply 2D matrix using function and pointer? please ... thanx for helping me!
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.