Java Newton's Method HELP PLEASE

Discussion in 'Java' started by nickcanada, Apr 6, 2010.

  1. nickcanada

    nickcanada New Member

    Joined:
    Apr 5, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hey I'm a total beginner to Java and I could really use some help with this program please!
    This is part of the code I wrote. I put a do-while loop but this only takes care of the rootEstimate value, the powerRider value is also changing. Basically I need to end up with my answer for the root Estimate then after I got the answer with the first power, switch to the second power. The powerRider variable is declared as an array. I was thinking of possibly having a loop with an increment encasing the do-while loop which would increase whenever the while part is not met, but I'm not 100% sure how to approach this and some help would be appreciated!
    Code:
     function = -n*powerRider + 0.5*(rho*cD*area*(Math.pow(initialRootEstimate,3))) + (cR*m*g*initialRootEstimate);
     derivedFunction = (3/2)*(rho*cD*area*(Math.pow(initialRootEstimate,2))) + (cR*m*g);
     do
     {
      rootEstimate = initialRootEstimate - (function/derivedFunction);
      absDifference = Math.abs(rootEstimate-initalRootEstimate);
      numLoops++;
      initialRootEstimate=rootEstimate;
      }
    
    Thank you!
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    post the mathematics problem you are trying to solve.
     
  3. tech_aks

    tech_aks New Member

    Joined:
    Apr 5, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    kolkata
    i m sending u a newton class..post ur sum...will let u know d solution...
    Code:
    class Newton  {
    
        static double f(double x) {
    	return Math.sin(x);
        }
    
        static double fprime(double x) {
            return Math.cos(x);
        }
    
        public static void main(String argv[]) {
    
    	  double tolerance = .000000001; // Our approximation of zero
    	  int max_count = 200; // Maximum number of Newton's method iterations
    
    /* x is our current guess. If no command line guess is given, 
       we take 0 as our starting point. */
    
    	  double x = 0;
    
    	  if(argv.length==1) {
    	    x= Double.valueOf(argv[0]).doubleValue();
    	  }
    
             for( int count=1;
                      (Math.abs(f(x)) > tolerance) && ( count < max_count);
                     count ++)  {
                x= x - f(x)/fprime(x);
    	    System.out.println("Step: "+count+" x:"+x+" Value:"+f(x));
    	  }            
    
    	  if( Math.abs(f(x)) <= tolerance) {
    	   System.out.println("Zero found at x="+x);
    	  }
    	  else {
    	   System.out.println("Failed to find a zero");
    	  }
         }
    
    }
     
    Last edited by a moderator: Apr 7, 2010
  4. nickcanada

    nickcanada New Member

    Joined:
    Apr 5, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hey sorry I've been working on it so haven't had a chance to check back. Thanks for the help! So far I've written this:
    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();
        }
    
    }
    It works but the only problem is that we end up with a lot of iterations. Is there any way we can reduce the amount of iterations?

    thanks again
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice