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 2 of 2

Thread: Java Newton's Method HELP PLEASE

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Newton's Method HELP PLEASE

    Hey I'm pretty new to java and I was just wondering if there was a way that I can reduce the amount of iterations performed? I'm getting about 237 right now, and I'd like to know how to get less.

    Thanks so much here's my code:

    import java.io.*;
     
    public class Assignment1try2
     
    {
     
        public static void main(String[] args)
     
        {
            //Exports the data to an excel file
     
         PrintWriter outputFile = null;
           try
             {
               outputFile = new PrintWriter(new FileOutputStream("outputVelomobile.xls",false));
              }
                catch(FileNotFoundException e)
             {
               System.out.println("File error.  Program aborted.");
               System.exit(0);
              }
     
     
            //Variable declarations for the Quest Velomobile
     
        double n = 0.92;
        double rho = 1.20;
        double cR = 0.004;
        double m = 32+80;
        double g = 9.81;
        double cD = 0.24;
        double area = 0.46;
        double valueOne = rho*cD*area;
        double valueTwo = cR*m*g;
        double initialRootEstimate = 1;
        double function, absDifference;
        double[] rootEstimate = new double[8];
        int numLoops = 0;
        int[] powerRider = {25,50,75,100,150,200,300,400};
     
     
            //Start loop
        for (int index=0;index<powerRider.length;index++)
     
        {
     
             numLoops = 0;
     
            do
            {
     
            function = -n*powerRider[index] + 0.5*(valueOne*(Math.pow(initialRootEstimate,3))) + (valueTwo*initialRootEstimate);
     
            rootEstimate[index] = initialRootEstimate - (function/((3/2)*(valueOne*(Math.pow(initialRootEstimate,2))) + (valueTwo)));
     
            absDifference = Math.abs(rootEstimate[index] - initialRootEstimate);
     
            initialRootEstimate = rootEstimate[index];
            numLoops++;
     
             }
     
               while (absDifference >=1.0E-12);
     
                //Displays data on screen
                System.out.println();
                System.out.println("The Power is:"    +   powerRider[index]);
                System.out.println("The Root Estimate is:"    +   initialRootEstimate);
                System.out.println("The number of cycles is:"    +   numLoops);
                System.out.println();
     
                //Outputs results on screen
     
                outputFile.println();
                outputFile.println("The Power is:"    +   powerRider[index]);
                outputFile.println("The Root Estimate is:"    +   initialRootEstimate);
                outputFile.println("The number of cycles is:"    +   numLoops);
                outputFile.println();
     
     
            }
                outputFile.close();
        }
     
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java Newton's Method HELP PLEASE

    237 iterations doesn't seem that bad for an newton's root finding algorithm (I'm assuming that's what this is).

    There are three main methods for getting less iterations:

    1. Choose a different algorithm (Newton's method's probably the fastest, though not very reliable). I would recommend Brent's method for the best combination of stability and speed.
    2. Allow for a greater error tolerance (especially if you know there will be quite a bit of truncation error). Good values are ~1e-8 relative error.
    3. Limit the maximum number of iterations run (i.e. keep track of which iteration you're on and simply return the current value once the limit has been reached). Newton's method is likely to never converge, so this is definately a good idea to have. Good values are ~1000 to 10000, less or more depending on the application (smaller for less precision and faster, larger for more precision and slower)

Similar Threads

  1. Java Code Help - Calling Method
    By KevinGreen in forum Object Oriented Programming
    Replies: 5
    Last Post: September 18th, 2009, 01:55 AM
  2. Replies: 2
    Last Post: June 13th, 2009, 02:44 AM