Iterations, matrix replacement

F

Francogrex

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[j]=((xa[j])+(((xb[1]))*theta[j]/(theta
[1]+theta[2]))+
(((xc[1][j]))*theta[j]/(theta[1][j]+theta[2][j])))/n;
}
}
for (i=0;i<2;i++){
for (j=0;j<2;j++){
theta[j]=thetaNew[j];
}
}
k=k+1;
cout<<"iteration: "<<k<<"\n";
}
for (i=0;i<2;i++){
for (j=0;j<2;j++){
cout <<"" <<thetaNew[j]<<"\n";
}
}
system("PAUSE");
return 0;
}
 
F

Francogrex

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[j]=((xa[j])+(((xb[0]))*theta[j]/(theta
[0]+theta[1]))+
+ (((xc[0][j]))*theta[j]/(theta[0][j]+theta[1][j])))/n;
}
}
for (i=0;i<2;i++){
for (j=0;j<2;j++){
theta[j]=thetaNew[j];
}
}
cout<<"iteration: "<<k<<"\n";
for (i=0;i<2;i++){
for (j=0;j<2;j++){
cout<<"iteration: "<<theta[j]<<"\n";
}
}
}
system("PAUSE");
return 0;
}
 
J

Jim Langston

Francogrex said:
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[j]=((xa[j])+(((xb[0]))*theta[j]/(theta
[0]+theta[1]))+
+ (((xc[0][j]))*theta[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[j]=thetaNew[j];
}
}
cout<<"iteration: "<<k<<"\n";
for (i=0;i<2;i++){
for (j=0;j<2;j++){
cout<<"iteration: "<<theta[j]<<"\n";
}
}
}
system("PAUSE");
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top