Results 1 to 3 of 3
Thread: NewtonRaphson java code help
 06192013, 02:50 AM #1Member
 Join Date
 Jun 2013
 Posts
 1
 Rep Power
 0
NewtonRaphson java code help
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class NewtonRaphsonMethod
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
DecimalFormat fmt = new DecimalFormat ("0.###############");
String degreeCount, funcCoefficient, coefficentCount, userGuess, notEnough;
int degree, coefficient, a = 1, b = 0;
double guess, equation = 0, again = 1, x, fX, fPrimeX, rTotal, x1;
double[] xn = new double[1000];
double[] xn1 = new double[1000];
do
{
degreeCount = JOptionPane.showInputDialog("Please enter the degree of your polynomial below");
degree = Integer.parseInt(degreeCount);
funcCoefficient = "Please enter the " + (degree + 1) + " coefficents from highest degree to lowest degree in order";
JOptionPane.showMessageDialog(null, funcCoefficient);
int[] coefficientIndex = new int[degree + 1];
for (int count = 0; count < coefficientIndex.length; count++)
{
coefficentCount = JOptionPane.showInputDialog("Enter the coefficient below:");
coefficient = Integer.parseInt(coefficentCount);
coefficientIndex[count] = coefficient;
}
userGuess = JOptionPane.showInputDialog("Enter your initial guess (within a 1 decimal point threshold of the answer): ");
guess = Double.parseDouble(userGuess);
xn[0] = guess;
if (coefficientIndex.length < (degree + 1))
{
notEnough = "The number of coefficients you entered does not match the degree of the polynomial you gave.";
JOptionPane.showMessageDialog(null, notEnough);
}
int degree2 = degree;
System.out.println("Your polynomial function is f(x) = ");
for (int polynomial = 0; polynomial < (degree+1); polynomial++)
{
if (polynomial < degree)
{
System.out.print(coefficientIndex[polynomial] + "x^" + degree2 + " + ");
}
else
{
System.out.print(coefficientIndex[polynomial] + "x^" + degree2);
}
degree2;
}
int degree3 = degree;
double addTot = 0;
for (int fX1 = 0; fX1 < (degree+1); fX1++)
{
fX = coefficientIndex[fX1] * Math.pow(guess, degree3);
degree3;
addTot = addTot + fX;
}
int degree4 = degree;
double addTot2 = 0;
for (int fPrimeX1 = 0; fPrimeX1 < (degree+1); fPrimeX1++)
{
fPrimeX = (degree4 * coefficientIndex[fPrimeX1]) * Math.pow(guess, (degree4  1));
degree4;
addTot2 = addTot2 + fPrimeX;
}
rTotal = addTot/addTot2;
x1 = guess  rTotal;
while (guess != x1)
{
guess = x1;
xn[a] = guess;
a++;
degree3 = degree;
addTot = 0;
for (int fX1 = 0; fX1 < (degree+1); fX1++)
{
fX = coefficientIndex[fX1] * Math.pow(guess, degree3);
degree3;
addTot = addTot + fX;
}
degree4 = degree;
addTot2 = 0;
for (int fPrimeX1 = 0; fPrimeX1 < (degree+1); fPrimeX1++)
{
fPrimeX = (degree4 * coefficientIndex[fPrimeX1]) * Math.pow(guess, (degree4  1));
degree4;
addTot2 = addTot2 + fPrimeX;
}
rTotal = addTot/addTot2;
x1 = guess  rTotal;
xn1[b] = x1;
b++;
}
xn1[b++] = x1;
System.out.println("\r");
System.out.println("xn values");
for (int i = 0; i < a; i++)
{
System.out.println(fmt.format(xn[i]));
}
System.out.println("\n\n");
System.out.println("xn+1 values");
for (int n = 0; n < b; n++)
{
System.out.println(fmt.format(xn1[n]));
}
}
while (again == JOptionPane.YES_OPTION);
System.out.println(" ");
System.out.println("The root of the polynomial is: " + xn1[b  1]);
}
}
This is a code implementing the Newton Raphson method (for polynomials) for a school project. This method helps you find the zero(es) of a function and in the code you need to know the zero beforehand as the zero that you enter as an initial guess in this program is supposed to be in a 0.1 decimal point threshold. The polynomial for which our teacher checked us where the program did not fully work is: 4x^4  2x^3 92x^2 + x + 45. The program worked when 4.5, one zero of the function was entered, but the program did not work when 0.7 was entered for the function although it was within a 0.1 threshold of the actual zero (0.701....). I know the error is on line 95 which I palpably pointed out above since this is the error I keep getting when I enter 0.7: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1000 at NewtonRaphsonMethod.main(NewtonRaphsonMethod.java: 95). Please no hints or sarcastic replies, I just need a full answer to how to solve this, I understand that the problem is related to the array but I still can't wrap my head around why 4.5 would work but 0.7 did not even though I entered the correct zero near 0.7 for the function and it still didn't work.
 06192013, 06:05 AM #2
Re: NewtonRaphson java code help
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
 06192013, 01:06 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,128
 Blog Entries
 7
 Rep Power
 24
Re: NewtonRaphson java code help
4.5 isn't a root of that polynomial (f(4.5) == 364.5); My Jenkins Traub implementation gives the following roots:
5.0107051936
4.4879589227
0.7187625169
0.696016246
Try a first approximation to the left of the smallest root and to the right of the largest root.
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
Similar Threads

Compiling another java code from our code
By madhav.dubey9 in forum New To JavaReplies: 18Last Post: 03112013, 05:54 PM 
I want the source code of DUK's Bank , the example code in Java EE 5 Tutorial 2010
By zahra in forum New To JavaReplies: 16Last Post: 01312012, 09:36 PM 
C server code  Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02212011, 12:50 PM 
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08122009, 11:14 AM 
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08092009, 01:21 PM
Bookmarks