# Thread: NewtonRaphson java code help

1. Member 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;
double[] xn1 = new double;

do
{
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 = 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;

for (int fX1 = 0; fX1 < (degree+1); fX1++)
{
fX = coefficientIndex[fX1] * Math.pow(guess, degree3);
degree3--;
}

int degree4 = degree;
for (int fPrimeX1 = 0; fPrimeX1 < (degree+1); fPrimeX1++)
{
fPrimeX = (degree4 * coefficientIndex[fPrimeX1]) * Math.pow(guess, (degree4 - 1));
degree4--;
}

x1 = guess - rTotal;

while (guess != x1)
{
guess = x1;
xn[a] = guess;
a++;
degree3 = degree;

for (int fX1 = 0; fX1 < (degree+1); fX1++)
{
fX = coefficientIndex[fX1] * Math.pow(guess, degree3);
degree3--;
}

degree4 = degree;

for (int fPrimeX1 = 0; fPrimeX1 < (degree+1); fPrimeX1++)
{
fPrimeX = (degree4 * coefficientIndex[fPrimeX1]) * Math.pow(guess, (degree4 - 1));
degree4--;
}

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.  Reply With Quote

2. ## Re: NewtonRaphson java code help  Reply With Quote

3. ## Re: NewtonRaphson java code help Originally Posted by Humraj 4x^4 - 2x^3 -92x^2 + x + 45. The program worked when -4.5, one zero of the function was entered
-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,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•