Results 1 to 3 of 3
  1. #1
    Humraj is offline Member
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default 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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,936
    Rep Power
    20

    Default Re: NewtonRaphson java code help

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default Re: NewtonRaphson java code help

    Quote Originally Posted by Humraj View Post
    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
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Compiling another java code from our code
    By madhav.dubey9 in forum New To Java
    Replies: 18
    Last Post: 03-11-2013, 05:54 PM
  2. Replies: 16
    Last Post: 01-31-2012, 09:36 PM
  3. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  4. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  5. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM

Posting Permissions

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