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