Results 1 to 12 of 12
Thread: Create Math.sin without math.sin
 12072010, 02:50 AM #1Member
 Join Date
 Dec 2010
 Posts
 6
 Rep Power
 0
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:
Java 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]); // } //} }
Java 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
 12072010, 02:58 AM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 23
The error pointed that you've missing one closing bracket here.
Java Code:return x (Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6]  (Math.pow(x,a[8]/a[8]);
Java Code:return x (Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6]))  (Math.pow(x,a[8]/a[8]));
Java Code:return (x (Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6]))  (Math.pow(x,a[8]/a[8])));
 12072010, 03:05 AM #3Member
 Join Date
 Dec 2010
 Posts
 6
 Rep Power
 0
It seems like I brought out a hellstorm.
Java 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
 12072010, 03:10 AM #4
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 23
Could you post the corrected code over here to see, the complete one?
 12072010, 03:12 AM #5Member
 Join Date
 Dec 2010
 Posts
 6
 Rep Power
 0
Java 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]))); } }
 12072010, 03:18 AM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 23
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.
Java 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]))); } }
 12072010, 03:26 AM #7Member
 Join Date
 Dec 2010
 Posts
 6
 Rep Power
 0
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?
Java Code:3 5 7 maxe x x x x x   +    + ....... (+/)  3! 5! 7! maxe!
 12072010, 03:35 AM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 23
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?
 12072010, 04:07 AM #9Member
 Join Date
 Dec 2010
 Posts
 6
 Rep Power
 0
I am not sure what you mean by separate them in a numerator and denominator.
Java Code:(Math.pow(x, a[4]) / a[4]) ^ ^ Numerator Denominators
 12072010, 04:31 AM #10Member
 Join Date
 Dec 2010
 Posts
 6
 Rep Power
 0
Would this be correct to represent the equation above?
Java 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])));
 12072010, 07:16 AM #11
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 23
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.
 12072010, 07:23 AM #12
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 23
Similar Threads

Math in java
By blug in forum New To JavaReplies: 2Last Post: 11222010, 03:21 PM 
Help with java math
By gabrielfirestorm in forum New To JavaReplies: 6Last Post: 10252010, 02:54 PM 
Math Class
By ritwik07 in forum New To JavaReplies: 2Last Post: 09142009, 05:06 PM 
Help with math in java
By fernando in forum New To JavaReplies: 1Last Post: 08062007, 07:05 AM 
Date math
By orchid in forum New To JavaReplies: 2Last Post: 04182007, 08:01 AM
Bookmarks