Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: calculate sin(x)

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question calculate sin(x)

    How can I transform this code to calculate the sin(x) in the method?

    public static double getCos(double degrees, int n) {
    double cos = 0;
    double numerator = 0;
    int denominator = 0;
    for (int i = 0; i <= n; i++, i++) {
    numerator = getNumerator(degrees, i);
    denominator = getFactorial(2 * i + 1);
    cos += numerator / denominator;


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: calculate sin(x)

    Can you use the Math class's method?

    Otherwise what is the algorithm for the computation? Where are you having problems coding it?


    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: calculate sin(x)

    package cosx;
     
    import java.util.Scanner;
     
    public class cosx2 {
     
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.print("Enter the degree: ");
    		double degree = input.nextDouble(); 
     
    		double radian = getRadian(degree);
     
    		calculateCos(input, radian,degree);
     
    	}
     
     
    	public static double getRadian(double x) {		
    		return x = (Math.PI/180)*x;		
    	}
     
    	private static void calculateCos(Scanner std, double rad, double deg) {
    		System.out.print("How many terms do you want to calculate the cos: ");
    		int terms = std.nextInt();
    		double top;
    		double bottom;
    		double sum=0;
     
    		for(int i = 0; i<= terms; ++i){
    			top = Math.pow(-1, i);			
    			bottom =1;
    			int repeat = 2*i+1;
    			for(int j = 1; j <= repeat; ++j){				
    				bottom *= j; 
    			}
    			if(i%2 == 0)
    				sum += (top/bottom);
    			else
    				sum -= (top/bottom);
    		}
    		System.out.printf("The sin (%.1f", deg);
    		System.out.printf(") is %.6f", sum);		
    	}
    }

    I cannot figure out how to input the function to calculate the cosine?

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: calculate sin(x)

    how to input the function to calculate the cosine?
    What do you mean by "input the function"?
    Are you trying to type code for a method into an editor?
    Are you trying to pass data as input to a method?
    Please explain.

    The attachment is useless. Too small.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: calculate sin(x)

    If you click the image it will expand....
    import java.util.Scanner;
     
    public class cosx2 {
     
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.print("Enter the degree please: ");
    		double degree = input.nextDouble(); 
     
    		double radian = getRadian(degree);
     
    		calculateCos(input, radian,degree);
     
    	}
     
     
    	public static double getRadian(double x) {		
    		return x = (Math.PI/180)*x;		
    	}
     
    	private static void calculateCos(Scanner std, double rad, double deg) {
    		System.out.print("How many terms do you want to calculate the cos: ");
    		int terms = std.nextInt();
    		double num;
    		double den;
    		double sum=0;
     
    		for(int i = 0; i<= terms; ++i){
    			num = Math.pow(-1, i);			
    			den = factorial(2*i);
    			int repeat = 2*i+1;
    			for(int j = 1; j <= repeat; ++j){				
    				den *= j; 
    			}
    			if(i%2 == 0)
    				sum += (num/den);
    			else
    				sum -= (num/den);
    		}
    		System.out.printf("The cos (%.1f", deg);
    		System.out.printf(") is %.6f", sum);
    		System.out.println("The calcuated value of cos was " +sum+ 
    				" Howver the real value of cose was " + Math.cos(deg));
     
    	}
    }
    Screen Shot 2013-11-06 at 11.11.44 AM.jpg
    Attached Images Attached Images

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: calculate sin(x)

    How are you debugging the code? I use println() statements to print out intermediate results to validate the code. Does the code compute each term in the series correctly? Print out the first few and manually verify that they are correct.

    The code should include comments showing the series that is being computed.

    It is impossible to copy the series shown in an image to be able to add it as comments to the code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. cos & sin 2d movement
    By Johnharrisanglia in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 10th, 2013, 04:23 PM
  2. what is the way to Calculate
    By ohad in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 8th, 2013, 11:29 AM
  3. Need some ideas on how to calculate this
    By derekxec in forum Java Theory & Questions
    Replies: 3
    Last Post: July 2nd, 2012, 06:50 PM
  4. Cannot calculate. Please help me....
    By safarina02 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2011, 01:11 PM
  5. NEED SIN OF ANGLE
    By CLAYAROBINSON in forum Java ME (Mobile Edition)
    Replies: 4
    Last Post: June 2nd, 2011, 10:14 PM