Cosinus-Arcustangens

Teolypto

Mitglied
Hallo Leute!
Brauche eure Tipps/Hilfe beim Cosinus/Arcustangens. Ich hoffe ihr hilft mir. Danke !

Der Cosinus von x (wobei x <= 2*; in Bogenmaß 1 Rad = 180/n Grad) kann durch folgende Näherungsformel berechnet werden.

Schreibe ein Programm, das einen Wert für x liest und den Cosinus nach obiger Formel berechnet. Breche die Berechnung nach 6 Iterationsschritten ab.
Teste dein Programm für die Werte 0, Pi/4, Pi /2, pi 3/4 , , pi3/2 ,
Vergleiche das Ergbnis mit der Cosinus-Funktion aus der Javabibliothek Math.cosinus(x).
(Tipp: Die Potenzfunktion und die Faktoriellefunktion muss nicht für jeden Summanden neu berechnet werden, sondern kann aus dem letzten Summanden berechnet werden.)

Formel :cos(x)= 1-x^2/2!+x^4/4!-x^6/6!+.......

Ich habe das Problem das Erstens die Werte nicht stimmen und zweitens das die Java-Bibliothek immer mir die selben Werte ausgibt egal was ich für x nehme.



Der Arcustangens von x (wobei |x| <= 1) kann durch folgende Näherungsformel berechnet werden.

Schreibe ein Programm, das einen Wert für x liest und den Arcustangens nach obiger Formel berechnet. Breche die Berechnung erst ab, wenn die Absolut-Differenz zwischen alten und neu berechneten Wert kleiner gleich 0.000000001 ist;
Teste dein Programm für die Werte 0, ¼, ½, 3/4
Vergleiche das Ergbnis mit der Arcustangens-Funktion aus der Javabibliothek Math.atan(x).
(Tipp: Die Potenzfunktion und die Faktoriellefunktion muss nicht für jeden Summanden neu berechnet werden, sondern kann aus dem letzten Summanden berechnet werden.)


Formel :arctan(x)= 1- x^3/3!+x^5/5!-x^7/7!+.......

Beim Arcustangens weiß ich das dieser ziemlich gleich aufgebaut ist, jedoch weiß ich nicht welche Zahlen und Formeln man ändern muss.





Java:
public class Cosinus
{	
	public static void main (String args[])
	{
		Out.print("Geben Sie ein Gradmass ein: ");
		double x = In.readDouble();
		
		double i =-Math.PI;
		
		while(i <= Math.PI)
		{
		
			testcos(i);
			i+=1.0;
		
		}
		
		
	}
	
	public static void testcos(double x)
	{
		//boolean debug = true;
		double mycos = cos(x); 
		double mathcos = Math.cos(x);
		double diff = mycos-mathcos;
		Out.println("cos ("+ x +") = "+ mycos +"\t Math.cos()= "+ mathcos +"\t\tdiff= "+ diff);
	
	}
		
	public static double cos(double x)
	{
	
		double sign = -1;
		
		double a = 1.0;
		double sum = 1.0;
		for(int i = 1; i <= 10; i+=2)
		{

			a=(double)pow(x, i+1) / (double)fak(i+1);
			sum = sum + sign * a;
			sign = -1* sign;
		}
		
		
		return sum;
	
	
	
	}
	public static double fak(int n)
	{
		double fak = 1;
		for(int i = 1 ; i<=n ; i++)
		{
			fak*=i;
		
		}
		
	
		return fak;
	}
	public static double pow(double x , int n)
	{
		double pow =1;
		for(int i = 1 ; i<=n ; i++)
		{
			pow*=x;
		
		}
	
	
		return pow;
	
	}
	
	public static double cos1(double x)
	{
		//cos(x) = 1 + x / 1! + x^2 / 2! + x^3 / 3! + x^4 / 4! + x^5 / 5! + x^6 / 6! . . . .


		double sum = 0;
		double oldSum = sum-1;
		double a = 1.0;
		int i = 1;
		while(Math.abs(sum-oldSum) > 0.000000001 )
		// oder while (i <= 20)
		{
			oldSum = sum;
			sum = sum + a;
			a = -a * (x*x / (i+(i+1)));
		
			/*if (debug) 
			{ 
			Out.println("i= " + i + " sum= " + sum + " diff= " + Math.abs(sum - oldSum)); 
			} 
			i++; 
			*/
		} 
		return sum; 
	
	} 
	
	
	
}

Ist eigentlich das Gleiche.


Java:
public class Arctang
{	
	public static void main (String args[])
	{
		Out.print("Geben Sie ein Gradmass ein: ");
		double x = In.readDouble();
		
		double i =-Math.PI;
		
		while(i <=Math.PI)
		{
		
			testatan(i);
			i+=1.0;
		
		}
		
		
	}
	
	public static void testatan(double x)
	{
	
		double myatan = atan(x); //
		double mathatan = Math.atan(x);
		double diff = myatan-mathatan;
		Out.println("atan ("+ x +") = "+ myatan +"\t Math.atan()= "+ mathatan +"\t\tdiff= "+ diff);
		
	}
		
	public static double atan(double x)
	{
		
		double sign = -1;
		
		double a = 1.0;
		double sum = 1.0;
		for(int i = 1; i <= 10; i++)
		{

			a=(double)pow(x, i+1) / (double)fak(i+1);
			sum = sum + sign * a;
			sign = -1* sign;
		}
		
		
		return sum;
	
	
	
	}
	public static double fak(int n)
	{
		double fak = 1;
		for(int i = 1 ; i<=n ; i++)
		{
			fak*=i;
		
		}
		
	
		return fak;
	}
	public static double pow(double x , int n)
	{
		double pow =1;
		for(int i = 1 ; i<=n ; i++)
		{
			pow*=x;
		
		}
	
	
		return pow;
	
	}

	public static double atan1(double x)
	{
		//cos(x) = 1 + x / 1! + x^2 / 2! + x^3 / 3! + x^4 / 4! + x^5 / 5! + x^6 / 6! . . . .


		double sum = 0;
		double oldSum = sum-1;
		double a = 1.0;
		int i = 1;
		while(Math.abs(sum-oldSum) > 0.000000001 )
		// oder while (i <= 20)
		{
			oldSum = sum;
			sum = sum + a;
			a = -a * (x*x / (i+(i+1)));
		
			/*if (debug) 
			{ 
			Out.println("i= " + i + " sum= " + sum + " diff= " + Math.abs(sum - oldSum)); 
			} 
			i++; 
			*/
		} 
		return sum; 
	} 
	
	
	
}
 

Ähnliche Java Themen

Neue Themen


Oben