Results 1 to 12 of 12
Thread: Create Math.sin without math.sin
- 12-07-2010, 01:50 AM #1
Member
- 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:
I keep getting these errors: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
- 12-07-2010, 01:58 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
The error pointed that you've missing one closing bracket here.
It should be something like this.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]);
And more clearly,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])));
- 12-07-2010, 02:05 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
It seems like I brought out a hellstorm.
Did I not code my array correctly?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
- 12-07-2010, 02:10 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Could you post the corrected code over here to see, the complete one?
- 12-07-2010, 02:12 AM #5
Member
- 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]))); } }
- 12-07-2010, 02:18 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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]))); } }
- 12-07-2010, 02:26 AM #7
Member
- 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?
(it is in my pastebin link for more clarification, my current equation does not work.)Java Code:3 5 7 maxe x x x x x - --- + --- - --- + ....... (+/-) --- 3! 5! 7! maxe!
- 12-07-2010, 02:35 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 03:07 AM #9
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
I am not sure what you mean by separate them in a numerator and denominator.
Is that not correct? Also is it called a summation right?Java Code:(Math.pow(x, a[4]) / a[4]) ^ ^ Numerator Denominators
- 12-07-2010, 03:31 AM #10
Member
- 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])));
- 12-07-2010, 06:16 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 06:23 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Math in java
By blug in forum New To JavaReplies: 2Last Post: 11-22-2010, 02:21 PM -
Help with java math
By gabrielfirestorm in forum New To JavaReplies: 6Last Post: 10-25-2010, 01:54 PM -
Math Class
By ritwik07 in forum New To JavaReplies: 2Last Post: 09-14-2009, 04:06 PM -
Help with math in java
By fernando in forum New To JavaReplies: 1Last Post: 08-06-2007, 06:05 AM -
Date math
By orchid in forum New To JavaReplies: 2Last Post: 04-18-2007, 07:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks