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.