Saturday, April 5, 2014

Java Program to Implement N-R (NewtonRaphson) METHOD

import java.util.Scanner;
public class NewtonRaphson2 {
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
       
        System.out.println("guess the initial value:");
        float x= input.nextFloat();
        float err=0;
        System.out.println("enter the no: of iteration:");  
        int time= input.nextInt();
        System.out.println("\n Iteration \t\t X \t\t\t Error(%)");
       
        for(int i =1;i<=time;i++)
        {
            float temp =x;
            x= x-(((float)Math.exp(-x))-x)/((-(float)Math.exp(-x))-1);
            err =Math.abs(((x-temp)/x)*100);
                   
            if(err<0.0001)
            {
                System.out.println(i-1 + "\t\t" + x + "\t\t" +err+ "%" /*+ "\n This is done....."*/);
                break;
            }
           
            else if (i<=2)
                System.out.println(i-1 + "\t\t" + x + "\t\t" + err +"%");
            else
                System.out.println(i + "\t\t" + x + "\t\t" + err+ "%");
       
        if(err<0.0001)
        {
            System.out.println(i + "\t\t" + x + "\t\t" + err +"%");
        }
        else
             System.out.println(i-1 + "\t\t" + x + "\t\t" + err +"%");
       
    }
}
}

No comments:

Post a Comment