Click to See Complete Forum and Search --> : conversion from matlab to java


researchws
May 19th, 2010, 05:39 AM
I have converted matlab code to java

My java code: (all the variables are already computed)
for(int i=1;i<=h2len;i++){
m = zd.from[zd.ppi[i]];
for(int j=1;j<=bpq.nbus;j++){
double t1 = zd.v[m]*zd.v[j];
double cosmj = Math.cos(zd.del[m]-zd.del[j]);
double sinmj = Math.sin(zd.del[m]-zd.del[j]);
double t2 = realybus[m][j]*cosmj + imagybus[m][j]*sinmj;
double t3 = t1 * t2;
double d1 = h2[i];
double d2 = t3;
double d3 = d1 + d2;
h2[i] = d3;
}
}

Matlab code:
for i = 1:npi
m = fbus(ppi(i));
for k = 1:nbus
h2(i) = h2(i) + V(m)*V(k)*(G(m,k)*cos(del(m)-del(k)) + B(m,k)*sin(del(m)-del(k)));
end
end

Problem:
upto 4 iterations i'm getting the same value for "h2" in java as matlab.
after that the value in java and matlab differs?

is there any conversion of double data type between java and matlab.

Please help me in this regards.

Thanks in advance

ProgramThis
May 19th, 2010, 07:42 AM
Yes, doubles in Java are notoriously inaccurate and have issues with rounding. If you need to perform Matlab style mathematics with Matlab like accuracy (maybe machine epsilon) you need to use the BigDecimal class instead of doubles.

researchws
May 19th, 2010, 07:45 PM
thanks for the reply.
i tried with BigDecimal too. It makes the system to get hang and I'm not getting the result.

ProgramThis
May 20th, 2010, 09:27 AM
Can you post the Java code that you have (inside of the code tags please) so we can see it?

nuzzle
May 20th, 2010, 11:07 AM
double cosmj = Math.cos(zd.del[m]-zd.del[j]);
double sinmj = Math.sin(zd.del[m]-zd.del[j]);


Instead of the Math class, try StrictMath instead.

researchws
May 20th, 2010, 10:07 PM
can i send the complete code?

nuzzle
May 21st, 2010, 01:57 AM
can i send the complete code?

Have you tried my suggestion to replace Math with StrictMath?