nte wurzel aus x

Fliegerli

Neues Mitglied
Hallo zusammen, habe das Problem dass ich die n-te Wurzel aus x über das Newton Verfahren lösen soll, nur fehlt mir der letzte schritt da die schleife aufhören soll nachdem der abstand von yneu und y ungefähr EPSILON erreicht, nur komm ich mit bestem willen nicht weiter, wie ich das lösen soll. Freue mich wenn ihr mir helfen könntet.

Java:
public class Newton {
	public static final double EPSILON = 0.000001;
    
    // computes x^n.
    // you may assume that n >= 0
    public static double power(double x, int n) {
                
        double e =x;
        for ( int i = 0; i < n ; i++){
            e = e* x;
        }
           
        return e;
    }
       
    // computes f(y) = y^n - x
    public static double f(int n, double x, double y) {
                 
       double f;          
      
                f=power(y,n)-x;
         
        return f;
        
    }
      
    // computes f'(y) = d/dy f(y)
    public static double fDiff(int n, double x, double y) {
                  
        double fI;
        	
        		fI=2*y;
         
        return fI;
         
    }
    // represents ONE iteration step of the Newton method
    public static double iter(int n, double x, double y) {
        
        double yNeu;
         
        yNeu = y-(f( n,x,y)/fDiff(n,x,y));
         
     
        return yNeu;
    }
   
    // computes x^(1/n) with precision EPSILON
    public static double rootNofX(int n, double x) {
   


    	double y=1;
    	double yNeu = 2;
    
    	
    	while(((y < yNeu) ? yNeu - y : y - yNeu)>EPSILON)
    	{ 	y=iter(n,x,y);
    	
    	
    	
    	}

    
    	
    	
    	
    	
        return y;
    }
   
    public static void main(String[] args) {
        boolean fail = false;
        int n = 0;
        double x = 0;
           
                try {
            n = Integer.parseInt(args[0]);
        } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
            fail = true;
        }
        try {
            x = Double.parseDouble(args[1]);
        } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
            fail = true;
        }
       
        if (n <= 1 || x <= 0 || args.length > 2 || fail) {
            System.out.println("Ihre Eingaben sind ungueltig!");
            System.out.println("Dieses Programm erwartet zwei Argumente n und x");
            System.out.println("  n: ganze Zahl und groesser oder gleich 2");
            System.out.println("  x: Gleitkommazahl, groesser als 0");
            if(x <= 0) {
                x = 1024;
            }
            System.out.println("Ich biete aber stattdessen an:");
            for (int i = 2; i <= 10; i++) {
                System.out.print("Die " + i + "-te Wurzel aus " + x + " ist ungefaehr ");
                System.out.println(rootNofX(i, x));
            }
        } else {
            System.out.print("Die " + n + "-te Wurzel aus " + x + " ist ungefaehr ");
            System.out.println(rootNofX(n, x));
        }
    }
}
 

Fliegerli

Neues Mitglied
Habe die Version nachmal überarbeitet jedoch teilt sie x noch durch n und zieht noch nicht die nte wurzel aus x
habe bereits foren und google druchscuht aber keine lösung gefunden, wichtig die methodenköpfe dürfen ja auch nicht veränfert werden...

Java:
public class Newton {
	public static final double EPSILON = 0.000001;
    
    // computes x^n.
    // you may assume that n >= 0
    public static double power(double x, int n) {
    	
    	  double e =x;
          for ( int i = 0; i < n ; i++){
              e = e* x;
          }
             
          return e;
       
    }
       
    // computes f(y) = y^n - x
    public static double f(int n, double x, double y) {
                 
       double f;          
      
                f=power(y,n)-x;
         
        return f;
        
    }
      
    // computes f'(y) = d/dy f(y)
    public static double fDiff(int n, double x, double y) {
                  
        double fI;
        	
        		fI=n*y;
         
        return fI;
         
    }
    // represents ONE iteration step of the Newton method
    public static double iter(int n, double x, double y) {
        
        double yNeu;
         
        yNeu = y-(f( n,x,y)/fDiff(n,x,y));
         
     
        return yNeu;
    }
   
    // computes x^(1/n) with precision EPSILON
    public static double rootNofX(int n, double x) {
   


    	double y;
    	double yNeu;
    	
    
    	
    	do {
    		yNeu = 1;
    		y = yNeu;
    	
    		yNeu =iter(n,x,y);
    		
    	}
    	while ((-yNeu-y)==EPSILON);    
    	
    	
    	
    	
        return yNeu;
    }
   
    public static void main(String[] args) {
        boolean fail = false;
        int n = 0;
        double x = 0;
           
                try {
            n = Integer.parseInt(args[0]);
        } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
            fail = true;
        }
        try {
            x = Double.parseDouble(args[1]);
        } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
            fail = true;
        }
       
        if (n <= 1 || x <= 0 || args.length > 2 || fail) {
            System.out.println("Ihre Eingaben sind ungueltig!");
            System.out.println("Dieses Programm erwartet zwei Argumente n und x");
            System.out.println("  n: ganze Zahl und groesser oder gleich 2");
            System.out.println("  x: Gleitkommazahl, groesser als 0");
            if(x <= 0) {
                x = 1024;
            }
            System.out.println("Ich biete aber stattdessen an:");
            for (int i = 2; i <= 10; i++) {
                System.out.print("Die " + i + "-te Wurzel aus " + x + " ist ungefaehr ");
                System.out.println(rootNofX(i, x));
            }
        } else {
            System.out.print("Die " + n + "-te Wurzel aus " + x + " ist ungefaehr ");
            System.out.println(rootNofX(n, x));
        }
    }
}
 

stg

Top Contributor
Ohne wirklich im Detail geguckt zu haben, was du machst, fallen mir dennoch zwei Sachen in deiner Methode
Code:
rootNofX(·)
auf:

  • Die Zuweisung
    Code:
     yNeu = 1;
    sollte noch VOR der do-while-Schleife geschehen. Sonst vergleichst du immer die gleichen Werte.
  • Die Abbruchbedingung
    Code:
    ==EPSILON
    ist quatsch. Die Differenz der beiden zuletzt berechneten Werte soll doch kleiner (oder kleiner gleich) deinem Epsilon sein.
 
Zuletzt bearbeitet:

Neue Themen


Oben