help with matrices

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wuzertheloser
    New Member
    • Oct 2006
    • 22

    help with matrices

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

    {x}(n+1) = [K] {x}(n), n = 0,1,2, ... ,8,9.

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

    Perform the matrix multiplication by using the function:

    void matrix_mult(dou ble a[4][4], double b[4], double c[4], int rows);

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

    [K] is a constant 4x4 matrix defined by:

    double K[4][4] = { { 2., -4., 1., 0.},
    {-4., 5., -2., 1.},
    { 1., -2., 6., -3.},
    { 0., 1., -3., 7.}};

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

    double x[4] = { 1./3., 2./3., 1./3., pow(3.,0.5)/3.};

    First, print the value of the initial vector {x}(0) on the screen.
    Then, perform forward iteration 10 times (for n=0 to 9). Each time
    after new vector is computed from [K]{x}, normalize that vector by
    using the function

    double unit_vec(double vec[4], int cols)

    which was discusseed in class (see Program w8-5.c; this function
    transforms a vector to a unit vector - of magnitude 1). For the
    matrix multiplication with the vector [K]{x}, see Program w8-3.c .

    Always use the normalized vector as the vector {x}(n) for the next
    iteration.

    For each iteration, print the new vector, its length, and the normalized
    new vector in main().


    That is the assignment. I don't even know where to start. I've constructed an outline but I don't know what to put in for the C statements.

    Code:
    #include <stdio.h>
    #include <math.h>
    double unit_norm(double vec[4], int cols);
    void matrix_mult(double a[4][4], double b[4], double c[4], int rows);
    main()
    {
     double K[4][4] = { { 2., -4.,  1.,  0.},
                        {-4.,  5., -2.,  1.},
                        { 1., -2.,  6., -3.},
                        { 0.,  1., -3.,  7.}};
     double y[4], x[4] = { 1./3., 2./3., 1./3., pow(3.,0.5)/3.};
    
    // Enter your C statements
    .........................
    .........................
    }
    
    void matrix_mult(double a[4][4], double b[4], double c[4], int rows)
    {
    // compute c[]=a[][]*b[]
    ..........................
    ..........................
    return;
    }
    
    double unit_norm(double vec[4], int cols)
    {
    double sum;
    // normalize a vector
    ...........................
    ...........................
    return sum;
    }
    My teacher says the output should look like:

    Initial vector:
    x[0] = [ 0.333333 0.666667 0.333333 0.577350]

    New vector:
    y[1] = [-1.666667 1.910684 -0.732051 3.708119]
    The length of this vector is: 4.551322
    Normalized new vector:
    x[1] = [-0.366194 0.419808 -0.160844 0.814734]

    New vector:
    y[10] = [-3.096892 5.315900 -6.556405 6.293838]
    The length of this vector is: 10.974897
    Normalized new vector:
    x[10] = [-0.282180 0.484369 -0.597400 0.573476]
  • wuzertheloser
    New Member
    • Oct 2006
    • 22

    #2
    Okay, here is my code now

    Code:
    #include <stdio.h>
    #include <math.h>
    double unit_norm(double vec[4], int cols);
    void matrix_mult(double a[4][4], double b[4], double c[4], int rows);
    main()
    {
     double K[4][4] = { { 2., -4.,  1.,  0.},
                        {-4.,  5., -2.,  1.},
                        { 1., -2.,  6., -3.},
                        { 0.,  1., -3.,  7.}};
     double y[4], x[4] = { 1./3., 2./3., 1./3., pow(3.,0.5)/3.};
     int k, rows, n;
     double dot_p;
    printf("\nInitial vector:\n");
    printf("x[0] = [" );
    for(rows=0; rows<4; rows++){
            printf(" %f", x[rows]);
            }
    printf("]\n");
    printf("\nNew Vector:\n");
    for(n=0; n<11; n++){
            printf("y[%d] = [", n);
            for(rows = 0; rows<4; rows++){
                    printf(" %f", matrix_mult(double a[4][4], double b[4], double c[4], int row));
            }
            printf("]\n");
    }
    }
    
    void matrix_mult(double a[4][4], double b[4], double c[4], int row)
    {
    /* compute c[]=a[][]*b[]*/
    int col;
    double sum;
    for(row=0;row<4; row++){
            for(col=0, sum=0.0;col<4; col++){
                    sum +=a[row][col]*b[col];
                    }
            c[row]=sum;
            }
    return;
    }
    
    double unit_norm(double vec[4], int cols)
    {
    double sum=0;
    /* normalize a vector */
    int i;
    for(i=0; i<cols; i++)
            sum += vec[i] * vec[i];
    sum = sqrt(sum);
    for(i=0; i<cols; i++)
    vec[i]=vec[i]/sum;
    return sum;
    }
    i can't get the matrix_mult function to show up when i'm trying to do the printf. anyone know how to fix this?

    Comment

    • REICC
      New Member
      • Nov 2007
      • 3

      #3
      for lines 30, why are you using a,b,c? You have not declared them yet.

      Comment

      Working...