Results 1 to 2 of 2
  1. #1
    mcgillstudent is offline Member
    Join Date
    Jan 2013
    Rep Power

    Default Creating Taylor Series (Different from one in search) Having so much trouble!

    I'm having trouble and seem to have hit a roadblock.

    I'm going to copy and paste my question so you know exactly what I'm trying to do and then, I will post my code as of now below that:

    Writing the sin(x) method (50 points)
    Write a method called sin that takes as input a double and returns a double. Should approximate using taylor series.
    You may not use any library method (such as Math.pow) for this.
    You must use at least one loop in your method.

    Note: The approximation will only be close to the actual value with a=10 for small values of x (between -1 and 1).

    You should not use Scanner or have print statements in any of the required methods with the exception of System.out.println statements being allowed inside of the main method of part 2a.

    Here is my code:

    Java Code:
    //x = User Input Number
    //Function: x - (x^3/3!) + (x^5/5!) - (x^7/7!) ..-->.. + (x^21/(21!)
    import java.util.Scanner;
    public class TaylorSin {
    	public static final double a = 10.0;
    	public static void main(String[] args) {
    	Scanner UserInput = new Scanner(;
    	System.out.println("Please enter a number between -1 and +1");
    	double x = UserInput.nextDouble();
    	System.out.println("Your inputed number was" + x + ".");
    	System.out.println(Numerator() / Factorial() );
    	public static double Numerator(double x){  //The Numerator formula
    	double power = 1.0;
    	double sum = 0.0;
    	double individualNumerator = (x^power);
    		sum += individualNumerator;
    	while (power <= 21);
    	power += 2;
    	return sum;
    public static double factorial(){     //The Denominator formula
        double result = 1;
        for(int i = 1; i <= x; ++2)
            result *= i;
        return result;
    Now that I look at it, here are specific questions.

    1. How to I do the ADD (1st element of function ie. x) SUBTRACT (2nd element of function ie. x^3/3!) ... and so forth until I reach 21.

    2. Should I calculate each function element as (numerator/denominator) and how would I go about this followed by (the addition/subtraction) of each element

    3. Am I doing anything blatantly wrong or misguided towards my final answer of creating the sin function?

    NOTE: I'm not comfortable with arrays as we just started them, but if it would make it easier let me know.

    NOTE: Any help is gratefully appreciated as my assignment is due tonight at 11:59 PM EST.

    Thank you so much,
    I'm here to learn so tips are appreciated, I don't want you to solve my assignment for me.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Creating Taylor Series (Different from one in search) Having so much trouble!

    Well, as far as alternate addition and subtraction, you could write your own method to raise a value to a specific power. Then use that method in conjunction with the fact that -1 raised to consecutive integral powers alternates its sign.

    Edit: I was looking over this again and spotted a major error. The caret symbol (^) is not exponentiation. It is an eXclusive OR operation. To do exponentiation you must use the Math.pow() method which your professor has declared off limits.

    Last edited by jim829; 03-05-2013 at 06:16 PM. Reason: noticed exponentiation problem
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 17
    Last Post: 03-28-2012, 08:29 AM
  2. trouble creating stack using array
    By shashankc in forum New To Java
    Replies: 5
    Last Post: 01-20-2011, 12:49 PM
  3. Trouble creating object that includes array
    By Desdenova in forum New To Java
    Replies: 7
    Last Post: 05-18-2010, 07:33 PM
  4. Trouble with creating TreeSet
    By MrKP in forum New To Java
    Replies: 1
    Last Post: 12-27-2009, 02:22 AM
  5. Trouble with a search hw problem.
    By frasifrasi in forum New To Java
    Replies: 3
    Last Post: 07-10-2008, 09:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts