Iterations, matrix replacement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Francogrex

    Iterations, matrix replacement

    Hi again, with a new question (beginner have a lot to learn). I am
    doing an updating algorithm where I have to do 10 iterations and each
    time need to replace the values of the matrix theta by the new
    estimated thetaNew matrix, but it doesn't seem to be working, there is
    no interative replacement, the values should end up being
    0.6971233
    0.09863044
    0.1357830
    0.06846318

    but they're stuck at:
    0.611544
    0.0858034
    0.146646
    0.0733229

    Thanks for any suggestion correction. The code below:

    #include <iostream>
    using namespace std;

    int main(){
    double xa[3][3]={{392.0,55.0}, {76.0,38.0}};
    double xb[3][3]={{0,0},{33,9}} ;
    double xc[3][3]={{31.0,7.0},{0 ,0}};
    double theta[3][3]={{0.25,0.25},{ 0.25,0.25}};
    double n=641.0;
    double thetaNew[3][3]={{0.25,0.25},{ 0.25,0.25}};
    int k=0;
    int i=0,j=0;
    while (k<10){
    for (i=0;i<2;i++){
    for (j=0;j<2;j++){
    thetaNew[i][j]=((xa[i][j])+(((xb[i][1]))*theta[i][j]/(theta[i]
    [1]+theta[i][2]))+
    (((xc[1][j]))*theta[i][j]/(theta[1][j]+theta[2][j])))/n;
    }
    }
    for (i=0;i<2;i++){
    for (j=0;j<2;j++){
    theta[i][j]=thetaNew[i][j];
    }
    }
    k=k+1;
    cout<<"iteratio n: "<<k<<"\n";
    }
    for (i=0;i<2;i++){
    for (j=0;j<2;j++){
    cout <<"" <<thetaNew[i][j]<<"\n";
    }
    }
    system("PAUSE") ;
    return 0;
    }
  • Francogrex

    #2
    Re: Iterations, matrix replacement

    On Apr 8, 12:18 am, Francogrex <fra...@grex.or gwrote:
    Hi again, with a new question (beginner have a lot to learn). I am
    doing an updating algorithm where I have to do 10 iterations and each
    time need to replace the values of the matrix theta by the new
    estimated thetaNew matrix, but it doesn't seem to be working...
    OK, again, after some trial and error I figured it out, it works fine
    now (see code below). The problem was that I am still confused about
    the 0 subscript eg xa[0]. In S+ it doesn't exist. Now I know better.

    The good code:

    #include <iostream>
    using namespace std;

    int main(){
    double xa[3][3]={{392.0,55.0}, {76.0,38.0}};
    double xb[3][3]={{33,33},{9,9} };
    double xc[3][3]={{31,7},{31,7} };
    double theta[3][3]={{0.25,0.25},{ 0.25,0.25}};
    double n=641.0;
    double thetaNew[3][3]={{0.25,0.25},{ 0.25,0.25}};
    int k=0;
    int i=0,j=0;
    while (k<10){
    k=k+1;
    for (i=0;i<2;i++){
    for (j=0;j<2;j++){
    thetaNew[i][j]=((xa[i][j])+(((xb[i][0]))*theta[i][j]/(theta[i]
    [0]+theta[i][1]))+
    + (((xc[0][j]))*theta[i][j]/(theta[0][j]+theta[1][j])))/n;
    }
    }
    for (i=0;i<2;i++){
    for (j=0;j<2;j++){
    theta[i][j]=thetaNew[i][j];
    }
    }
    cout<<"iteratio n: "<<k<<"\n";
    for (i=0;i<2;i++){
    for (j=0;j<2;j++){
    cout<<"iteratio n: "<<theta[i][j]<<"\n";
    }
    }
    }
    system("PAUSE") ;
    return 0;
    }

    Comment

    • Jim Langston

      #3
      Re: Iterations, matrix replacement

      Francogrex wrote:
      On Apr 8, 12:18 am, Francogrex <fra...@grex.or gwrote:
      >Hi again, with a new question (beginner have a lot to learn). I am
      >doing an updating algorithm where I have to do 10 iterations and each
      >time need to replace the values of the matrix theta by the new
      >estimated thetaNew matrix, but it doesn't seem to be working...
      >
      OK, again, after some trial and error I figured it out, it works fine
      now (see code below). The problem was that I am still confused about
      the 0 subscript eg xa[0]. In S+ it doesn't exist. Now I know better.
      >
      The good code:
      I haven't bothered to test this code, but some comments
      #include <iostream>
      using namespace std;
      >
      int main(){
      double xa[3][3]={{392.0,55.0}, {76.0,38.0}};
      The data is 2x2, yet you are declaring an array 3x3. Why? Why aren't you
      using
      double xa[2][2] = {{392.0,55.0},{ 76.0,38.0}};
      The elements are xa[0][0], xa[0][1], xa[1][0] and xa[1][1]

      Arrays in C and C++ are 0 bound.
      double xb[3][3]={{33,33},{9,9} };
      double xc[3][3]={{31,7},{31,7} };
      double theta[3][3]={{0.25,0.25},{ 0.25,0.25}};
      double n=641.0;
      double thetaNew[3][3]={{0.25,0.25},{ 0.25,0.25}};
      int k=0;
      int i=0,j=0;
      while (k<10){
      k=k+1;
      for (i=0;i<2;i++){
      for (j=0;j<2;j++){
      thetaNew[i][j]=((xa[i][j])+(((xb[i][0]))*theta[i][j]/(theta[i]
      [0]+theta[i][1]))+
      + (((xc[0][j]))*theta[i][j]/(theta[0][j]+theta[1][j])))/n;
      }
      }
      Here your counters are correctly going over the array as if it was 2x2. I
      still don't know why you made them 3x3
      for (i=0;i<2;i++){
      for (j=0;j<2;j++){
      theta[i][j]=thetaNew[i][j];
      }
      }
      cout<<"iteratio n: "<<k<<"\n";
      for (i=0;i<2;i++){
      for (j=0;j<2;j++){
      cout<<"iteratio n: "<<theta[i][j]<<"\n";
      }
      }
      }
      system("PAUSE") ;
      return 0;
      }


      --
      Jim Langston
      tazmaster@rocke tmail.com


      Comment

      Working...