Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Java Newton's Method HELP PLEASE (http://www.go4expert.com/forums/java-newtons-method-help-t21688/)

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!

 virxen 7Apr2010 04:52

Re: Java Newton's Method HELP PLEASE

post the mathematics problem you are trying to solve.

 tech_aks 7Apr2010 12:01

Re: Java Newton's Method HELP PLEASE

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");           }     } }```

```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();     } }```