conversion from matlab to java
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
Re: conversion from matlab to java
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.
Re: conversion from matlab to java
thanks for the reply.
i tried with BigDecimal too. It makes the system to get hang and I'm not getting the result.
Re: conversion from matlab to java
Can you post the Java code that you have (inside of the code tags please) so we can see it?
Re: conversion from matlab to java
Quote:
Originally Posted by
researchws
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.
Re: conversion from matlab to java
can i send the complete code?
Re: conversion from matlab to java
Quote:
Originally Posted by
researchws
can i send the complete code?
Have you tried my suggestion to replace Math with StrictMath?