Question
Implement a Java program that applies the Newton-Raphson's method xn+1 = xn f(xn) / f '(xn) to search the roots for this polynomial function 9x6
Implement a Java program that applies the Newton-Raphson's method xn+1 = xn f(xn) / f '(xn) to search the roots for this polynomial function 9x6 8x5 + 7x4 6x3+ 5x2 4x + 3 = 0. The program terminates when the difference between the new solution and the previous one is smaller than 0.00001 within 2000 iterations. Otherwise, it shows Not Found as the final solution. Your teams goal is to find as many roots as possible. For the Newton-Raphsons method, you will need to start with a guessed x0 value which can be randomly generated. List out each guessed x0 value, the derived root, the number of iteration for finding the root, and the value of the polynomial function by plugging in the root.
I have this and I'm not getting an output:
import java.util.*; import java.math.*; class newtonR { static final double difference = 0.00001; // Find the value of function F(x)= 9x^6 8x^5 + 7x^4 6x^3+ 5x^2 4x + 3 static double function(double x) { double ans = 9 * Math.pow(x, 6) - 8 * Math.pow(x, 5) + 7 * Math.pow(x, 4) - 6 * Math.pow(x, 3) + 5 * Math.pow(x, 2) - 4 * x + 3; return ans; } // Calculate derivative of the above function // which is 48x^5 40x^4 + 28x^3 18x^2+ 10x 4 static double derivativeOfFunction(double x) { double ans = 48 * Math.pow(x, 5) - 40 * Math.pow(x, 4) + 28 * Math.pow(x, 3) - 18 * Math.pow(x, 2) + 10 * x - 4; return ans; } // Function to find the root static boolean newtonRaphson(double x) { double h = function(x) / derivativeOfFunction(x); int iterations = 0; while (Math.abs(h) >= difference) { h = function(x) / derivativeOfFunction(x); // x(i+1) = x(i) - F(x) / F'(x) x = x - h; iterations++; // Solution not found after 2000 iterations return if (iterations >= 2000) { return false; } } // Solution is found print root and number of iterations System.out.println("The value of the root is : " + x); System.out.println("The total Number of iterations : " + iterations); return true; } public static void main (String[] args) { Random rand = new Random(); Scanner sc = new Scanner(System.in); int trials = sc.nextInt(); // Search for root trials times. for (int i = 0; i < trials; i++) { // Assign random values between -1000 and 1000 to x0 double x0 = -1000 + rand.nextInt(2000); boolean isFound = newtonRaphson(x0); if (isFound == true) { System.out.println("Intial value of x0 : " + x0); } else { System.out.println("Solution Not found "); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started