|
-
May 19th, 2010, 05:39 AM
#1
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
-
May 19th, 2010, 07:42 AM
#2
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.
-
May 19th, 2010, 07:45 PM
#3
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.
-
May 20th, 2010, 09:27 AM
#4
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?
-
May 20th, 2010, 11:07 AM
#5
Re: conversion from matlab to java
 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.
-
May 20th, 2010, 10:07 PM
#6
Re: conversion from matlab to java
can i send the complete code?
-
May 21st, 2010, 01:57 AM
#7
Re: conversion from matlab to java
 Originally Posted by researchws
can i send the complete code?
Have you tried my suggestion to replace Math with StrictMath?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|