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

how to add+multiply matrices

P: 11
Develop a program which computes the current value of the vector {x}
based on the following forward iteration:

{x}(n+1) = [K] {x}(n)+ {z}, n = 0,1,2,3,4.

In other words, the next vector {x} is equal to the product of [K] and
the current vector {x} + constant vector {z}.

Perform the matrix multiplication by using the function:

void matrix_mult(double a[3][3], double *b, double *c, int rows);

You should pass the matrix [K], and a pointer array {x}
to matrix mult(), which will pass back a vector {y}=[K]{x}.

[K] is a constant 3x3 matrix defined by:

double k[3][3] = { { 1.2, -3.0, 1.1},
{-4.1, 6.2, -2.2},
{ 3.4, -2.5, -3.3} };

The initial components of the vector {x}(0) are specified by:

double x[3] = { 1./sqrt(2.), 0., -1./sqrt(2.) },

while the constant vector {z} is:

double z[3] = { 1./sqrt(3.), -1./sqrt(3.), 1./sqrt(3.) }.

>>I've been trying for a week to finish this but i have only been able get the vector addition part done in [K] {x}(n)+ {z} so now it only computes [K] {x}(n). Can someone finish this off for me or help me in newbie language because it is due soon and my head hurts from trying. Thanks in advance!


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. void unit_norm(double *vec, int cols);
  4. void matrix_mult(double a[3][3], double *b, double *c, int rows);
  5. main()
  6. {
  7.  int row, i, j;
  8.  double k[3][3] = {{1.2, -3.0,  1.1},
  9.                   {-4.1, 6.2, -2.2},
  10.                   {3.4, -2.5, -3.3}};
  11.  double y[3], x[3] = { 1./sqrt(2.), 0., -1./sqrt(2.) };
  12.  
  13.  double w[3], z[3] = { 1./sqrt(3.), -1/sqrt(3.), 1./sqrt(3.) };
  14.  
  15.  printf("\nInitial vector:\n\n");
  16.  printf("x[0] = [");
  17.  for(row=0; row<3; row++)
  18.         printf(" %.6f ", x[row]);
  19.  printf("]\n\n");
  20.  
  21.   for(i=0; i<=4; i++)
  22.   {
  23.    matrix_mult(k,x,y,3);
  24.    printf("New Vector:\ny[%d] = [", i+1);
  25.    for(j=0; j<=2; j++)
  26.         printf(" %.6f ", y[j]);
  27.    printf("]\n");
  28.  
  29.    unit_norm(y,3);
  30.    printf("Normalized new vector:\nx[%d] = [", i+1);
  31.    for(j=0; j<=2; j++)
  32.         printf(" %.6f ", y[j]);
  33.    printf("]\n");
  34.    for(j=0; j<=2; j++)
  35.         x[j] = y[j];
  36.    printf("\n");
  37.   }
  38.   return(0);
  39. }
  40.  
  41. void matrix_mult(double a[3][3], double *b, double *c, int rows)
  42. {
  43.         int k, s;
  44.         double dot_p;
  45.         for(s=0; s<rows; s++)
  46.         {
  47.          for(k=0, dot_p=0.; k<3; k++)
  48.                 dot_p += a[s][k]*b[k];
  49.         c[s] = dot_p;
  50.         }
  51.         return;
  52. }
  53.  
  54. void unit_norm(double *vec, int cols)
  55. {
  56.         double norm;
  57.         double sum=0.;
  58.         int n;
  59.         for(n=0; n<cols; n++)
  60.          sum += vec[n] * vec[n];
  61.         sum = sqrt(sum);
  62.         for(n=0; n<cols; n++)
  63.             vec[n] = vec[n]/sum;
  64. return;
  65. }
  66.  
Feb 28 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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