# Create Math.sin without math.sin

• 12-07-2010, 02:50 AM
vudoo
Create Math.sin without math.sin
I have a homework assignment where it asks me to do this: Java has a "sine"-computing function: Math.sin(#)

I am new to java so please be patient with me and teach me with babysteps. :(

This is what I have:
Code:

```mport sdsu.io.*; public class hw7 { public static void main(String args[]) {  double x;  System.out.println("Please enter a value for x.");  x = Console.readInt();  double maxe;  System.out.println("Please enter an odd exponent");   double[]a =  new double[10];  maxe = Console.readInt();   for (int i = 0; i <= maxe; i++)         {         a[i] = i;         if ( i %2 !=0);  System.out.println(+a[i]);         } } //public static double f(double x) //I'm not sure how to do a summation to represent the sine function. This is as close as I got. //        { //        return  x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6] - //        (Math.pow(x,a[8]/a[8]); //        } //} }```
I keep getting these errors:
Code:

```hw7.java:26: ')' expected         (Math.pow(x,a[8]/a[8]);                               ^ hw7.java:26: ')' expected         (Math.pow(x,a[8]/a[8]);                               ^ hw7.java:27: ';' expected         }         ^ hw7.java:29: reached end of file while parsing }  ^ 5 errors```
• 12-07-2010, 02:58 AM
Eranga
The error pointed that you've missing one closing bracket here.

Code:

`return  x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6] - (Math.pow(x,a[8]/a[8]);`
It should be something like this.

Code:

`return  x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) - (Math.pow(x,a[8]/a[8]));`
And more clearly,

Code:

`return  (x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) - (Math.pow(x,a[8]/a[8])));`
• 12-07-2010, 03:05 AM
vudoo
It seems like I brought out a hellstorm.
Code:

```symbol  : variable a location: class hw7         return  (x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) -                                 ^ hw7.java:25: cannot find symbol symbol  : variable a location: class hw7         return  (x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) -                                       ^ hw7.java:25: cannot find symbol symbol  : variable a location: class hw7         return  (x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) -                                                           ^ hw7.java:25: cannot find symbol symbol  : variable a location: class hw7         return  (x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) -                                                               ^ hw7.java:26: cannot find symbol symbol  : variable a location: class hw7         (Math.pow(x,a[8]/a[8])));                     ^ hw7.java:26: cannot find symbol symbol  : variable a location: class hw7         (Math.pow(x,a[8]/a[8])));                         ^ 6 errors```
Did I not code my array correctly?
• 12-07-2010, 03:10 AM
Eranga
Could you post the corrected code over here to see, the complete one?
• 12-07-2010, 03:12 AM
vudoo
Code:

```import sdsu.io.*; public class hw7 { public static void main(String args[]) {  double x;  System.out.println("Please enter a value for x.");  x = Console.readInt();  double maxe;  System.out.println("Please enter an odd exponent");   double[]a =  new double[10];  maxe = Console.readInt();   for (int i = 0; i <= maxe; i++)         {         a[i] = i;         if ( i %2 !=0);  System.out.println(+a[i]);         } } public static double f(double x)         {         return  (x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) -         (Math.pow(x,a[8]/a[8])));                 } }```
• 12-07-2010, 03:18 AM
Eranga
You have define the array a in main method. So it's not possible to access it from another method. It's an invalid scope to access. Do you know about variable scope?

If you want to use the same array then you've define them globally. Something like this.

Code:

```public class hw7{     private static double[] a = new double[10];     public static void main(String args[]) {         double x;         System.out.println("Please enter a value for x.");         x = Console.readInt();         double maxe;         System.out.println("Please enter an odd exponent");         maxe = Console.readInt();         for (int i = 0; i <= maxe; i++) {             a[i] = i;             if (i % 2 != 0);             System.out.println(+a[i]);         }     }     public static double f(double x) {         return (x - (Math.pow(x, a[4]) / a[4]) + (Math.pow(x, a[6] / a[6]))                 - (Math.pow(x, a[8] / a[8])));     } }```
• 12-07-2010, 03:26 AM
vudoo
No, I do not what a variable scope is, but from what you did, I think I understand what you mean by defining it globally.

Could you please show me how I would be able to do this equation in Java?
Code:

```            3        5        7                          maxe           x        x        x                          x   x  -  ---  +  ---  -  ---  +  .......  (+/-)  ---           3!        5!        7!                          maxe!```
(it is in my pastebin link for more clarification, my current equation does not work.)
• 12-07-2010, 03:35 AM
Eranga
First of all separate those into to two, numerator and denominator. Just forget about the sign and all. So think how to calculate the term (Y^7 / X!). Did you get my point?

By the way did you know what this equation is, any special kind of name?
• 12-07-2010, 04:07 AM
vudoo
I am not sure what you mean by separate them in a numerator and denominator.
Code:

``` (Math.pow(x, a[4]) / a[4])         ^              ^ Numerator        Denominators```
Is that not correct? Also is it called a summation right?
• 12-07-2010, 04:31 AM
vudoo
Would this be correct to represent the equation above?
Code:

```(x - (Math.pow(x, a[4]) / a[4]*a[3]*a[2]*a[1]) + (Math.pow(x, a[6] /                 a[6]*a[5]*a[4]*a[3]*a[2]*a[1]))                 - (Math.pow(x, a[8] / a[8]*a[7]*a[6]*a[5]*a[4]*a[3]*a[2]*a[1])));```
• 12-07-2010, 07:16 AM
Eranga
Quote:

Originally Posted by vudoo
I am not sure what you mean by separate them in a numerator and denominator.
Code:

``` (Math.pow(x, a[4]) / a[4])         ^              ^ Numerator        Denominators```
Is that not correct? Also is it called a summation right?

What I mean, numerator is simply power of a digit. That's could be narrow down into multiplication.

eg: x ^ 5 = x * x * x * x * x;

Denominator is factorial of a number. That is x! = x * (x -1) * ..... * 1;

Actually what you are going to deals with is a standard number manipulation. The following article explain it, if you could try to understand it. So it may helpful to you, to solve the problem.
• 12-07-2010, 07:23 AM
Eranga
Quote:

Originally Posted by vudoo
Would this be correct to represent the equation above?
Code:

```(x - (Math.pow(x, a[4]) / a[4]*a[3]*a[2]*a[1]) + (Math.pow(x, a[6] /                 a[6]*a[5]*a[4]*a[3]*a[2]*a[1]))                 - (Math.pow(x, a[8] / a[8]*a[7]*a[6]*a[5]*a[4]*a[3]*a[2]*a[1])));```

Looks to me this is too complex. And I'm sure you are in trouble when you look at this again at a later time.

Don't implement such complex calculations in a single statement. And also don't write complex statements in return. Step-down it. Look at my previous post.