Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package NumericalComputing.NewtonRaphsonMethod;
- import java.util.Scanner;
- @interface DaudKakumirizi
- {
- String name() default " ";
- String ID() default " ";
- }
- @DaudKakumirizi(name = "Kakumirizi Daud", ID = "17/BSS/BU/R/0010")
- public class NewtonRaphson {
- public static void main(String[] args) {
- double x0,root,givenError,error;
- boolean flag=true;
- int i=1;
- Scanner sc = new Scanner(System.in);
- int highest_degree;
- System.out.println("What is the highest degree of your polynomial? ");
- Scanner input = new Scanner(System.in);
- highest_degree = input.nextInt();
- for (int j = highest_degree; j >= 0; j--) {
- int coeff_deg_j;
- coeff_deg_j = poly_input(j);
- /**System.out.println(coeff_deg_i); */
- }
- System.out.print("Enter value for x0 : ");
- x0 = sc.nextDouble();
- givenError=x0;
- while(flag){
- root = x0 - (F(x0)/Fprime_X(x0));
- error = Math.abs((root-x0)/root)*100;
- System.out.println("Iteration : "+ i++);
- System.out.println("Root:"+root);
- System.out.println("Error:"+error);
- x0 = root;
- if(givenError>error)
- {
- flag=false;
- }
- }
- }
- public static double F(double x){
- return Math.pow(x,3)-(0.165*Math.pow(x,2))+(3.993*Math.pow(10,-4));
- }
- public static double Fprime_X(double x){
- return 3*Math.pow(x,2)-0.33*x;
- }
- public static int poly_input(int degree) {
- System.out.println("Please enter coefficient for degree " + degree);
- Scanner input = new Scanner(System.in);
- int coefficient;
- coefficient = input.nextInt();
- return coefficient;
- }
- /**
- * This program has been written by KAKUMIRIZI DAUD
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement