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

• 03-05-2013, 05:23 PM
mcgillstudent
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:

QUESTION:
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:

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.in);         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);         do{                 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.
• 03-05-2013, 05:39 PM
jim829
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.

Regards,
Jim