CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2010
    Posts
    3

    Exclamation 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

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    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.

  3. #3
    Join Date
    May 2010
    Posts
    3

    Question 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.

  4. #4
    Join Date
    Feb 2008
    Posts
    966

    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?

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: conversion from matlab to java

    Quote Originally Posted by researchws View Post
    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.

  6. #6
    Join Date
    May 2010
    Posts
    3

    Re: conversion from matlab to java

    can i send the complete code?

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: conversion from matlab to java

    Quote Originally Posted by researchws View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured