Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Java Integration Help

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Java Integration Help

    Hey I am working on this program to aproximate the integral of the function sin(x)/x using Reimann sums (left hand). It is close, but whenever I try to check it in the web grader some of my results are way off. Any help is apreciated. Here is my code.

    public class Integrate {
    	public static void main(String[] args) {
    		if (args.length != 3) {
    			System.out.println("Error: must enter a, b, n");
    			System.exit(1);
    		}
    		double a=Double.parseDouble(args[0]);
    		double b=Double.parseDouble(args[1]);
    		double n=Double.parseDouble(args[2]);
    		double h = ((b-a)/n);
    		double result=0;
    		for (int i=0;i<n;i++) {
    			double x=(a+(i*h));
    			double aprox = ((Math.sin(x)/x)*h);
    			result=result+aprox;
    		}
    		System.out.printf("Sine =~ %.5f", result);
    	}
    }
    Last edited by CWalrus; October 3rd, 2014 at 02:53 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Integration Help

    Same thing. Post your code correctly.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Integration Help

    Sorry about posting my code incorrectly.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Integration Help

    Can you show the results of the web grader? Copy and paste them into a post, if possible.

    Oh, and what are 'a', 'b', and 'n'? Can you give some examples?

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Integration Help

    These web grader results are actually for a nearly identical program in C. Left is the expected result, right is what I get.
    integral of sin(x)/x on [-6.280000, 6.280000] ~= 2.836724 I got -nan
    integral of sin(x)/x on [3.150000, 6.000000] ~= -0.427175 I got -0.423824
    integral of sin(x)/x on [5.000000, 10.000000] ~= 0.105794 I got 0.105794
    integral of sin(x)/x on [-2.900000, -0.001000] ~= 1.840776 I got 1.605243
    integral of sin(x)/x on [0.000000, 1.000000] ~= 0.960932 I got -nan
    integral of sin(x)/x on [0.000000, 3.140000] ~= 1.852094 I got -nan

    And I made a mistake in the original post, i should go from zero to n-1, not from 1 to n. I am fixing that now.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Integration Help

    This doesn't look right:

    double a=Double.parseDouble(args[0]);
    double b=Double.parseDouble(args[0]);
    double n=Double.parseDouble(args[0]);

    Why are all args[0]?

  7. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Integration Help

    haha I don't know why that was like that. Obviously they were supposed to be args[0], args[1], and args[2]. That was not a problem in the original program, and I have fixed it now. Sorry about that.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Integration Help

    As Norm pointed out, your threads are primarily math based, probably meant to explore math concepts rather than Java concepts (we might have programmed them in MATLAB or Fortran), and you haven't provided us much info about what they're supposed to do and how they're supposed to do it. (Code comments would be nice.)

    Perhaps a math major will come by and find your topics interesting, but to be much help the rest of us would have to fill in our knowledge gaps that your subjects have revealed.

    Be patient for that excited math major to come by.

Similar Threads

  1. Java macintosh 10.8 integration
    By Cronus in forum Java Theory & Questions
    Replies: 4
    Last Post: July 13th, 2013, 12:48 PM
  2. JAVA TaskBar toolbar integration
    By WilyZ in forum Java Theory & Questions
    Replies: 1
    Last Post: October 27th, 2012, 04:15 PM
  3. Java/Excel integration, Reading in Dates
    By aussiemcgr in forum JDBC and Database Tutorials
    Replies: 0
    Last Post: July 16th, 2010, 08:38 AM
  4. Java/Excel integration, Reading in Dates
    By aussiemcgr in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: July 16th, 2010, 08:38 AM