Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 6 of 6

09262004, 10:47 PM #1
 Join Date
 Sep 2004
 Posts
 12
 Thanks
 0
 Thanked 0 Times in 0 Posts
new to programming and java, need help for class
Hello everyone! I was hoping someone might be able to give me a hand with the program I am writing for my intro cs class. We are studying algorithms and implementing them with java. I am totally new to programming, though I DO remember programming my TI85 graphing calculator to output formulas back in high school several years ago! I am struggling with the code, not the math.
Here is the problem:
for f(x) = 0.1x^3 + 3.6
create an algorithm to find the root of the function and implement it in java
For the algorithm, I am using Newton's Method to find the zero. Newton's Method is:
xn+1 = xn  ( f(xn) / ( f'(xn) )
Here is the code I have written:
Code:public class root { public static void main(String[] args) { //begin arguments //introduce variable u (the "guess" or [x sub n]), variable v ("guess +" or [x sub n+1]) double u=10000; double v; //for function for(u=10000; u<=10000; v=(u(((0.3*u*u*u)+36)/((0.9*u)*(0.9*u))))) {if ((abs(double u))(abs(double v)) <= 0.001); {System.out.println("The root of the function is " ); //I used Newton's Method here. In general terms, the formula is: //(x sub n+1) = (x sub n)  [ f(x) / f'(x) ] } } } }
ReneeLast edited by Antoniohawk; 09262004 at 11:48 PM.

09262004, 11:47 PM #2
 Join Date
 Aug 2002
 Location
 Kansas City, Kansas
 Posts
 1,518
 Thanks
 0
 Thanked 2 Times in 2 Posts
Welcome to Coding Forums. In the future, make sure to wrap your code in [code] tags so that it will retain the formatting.
I'm a beginner to Java so I may not be able to help you much. About the only thing that I know is proper formatting. It's really unfair for a teacher to dump a project on you when you haven't programmed in Java before.
Code:public class root { public static void main(String[] args) { //begin arguments //introduce variable u (the "guess" or [x sub n]), variable v ("guess +" or [x sub n+1]) double v; //for function for(double u = 10000; u <= 10000; v = (u  (((0.3 * u * u * u) + 36) / ((0.9 * u) * (0.9 * u))))){ if ((abs(double u))(abs(double v)) <= 0.001) System.out.println("The root of the function is " ); } //I used Newton's Method here. In general terms, the formula is: //(x sub n+1) = (x sub n)  [ f(x) / f'(x) ] } }
Last edited by Antoniohawk; 09272004 at 12:00 AM.
xhtml :: attributes . tags . <abbr> vs. <acronym> CSS :: Box Model Hack
[CF TShirts] [CP Resources] [ithium hosting] [A New Generation of Tree Menu]

09272004, 12:17 AM #3
 Join Date
 May 2002
 Location
 Marion, IA USA
 Posts
 6,292
 Thanks
 4
 Thanked 84 Times in 83 Posts
Originally Posted by Antoniohawk
lol I had a programming language course in college that required us to write a midside program in a different language every week and usually languages we had never seen or used before.Spookster
CodingForums Supreme Overlord
All Hail Spookster

09272004, 12:31 AM #4
 Join Date
 Aug 2002
 Location
 Kansas City, Kansas
 Posts
 1,518
 Thanks
 0
 Thanked 2 Times in 2 Posts
Originally Posted by Spookster
Back to the problem at hand (before we were rudely interrupted by Spooks ) I think that the problem lies in your for loop. As you have it right now, the for loop will probably run forever because there is no iterating variable that makes u any bigger. What you probably need to do is put that v = blah into inside of the loop.
Code:public class testeroo { public static void main(String[] args) { //begin arguments //introduce variable u (the "guess" or [x sub n]), variable v ("guess +" or [x sub n+1]) double v; //for function for(double u = 10000; u <= 10000; u++){ v = (u  (((0.3 * u * u * u) + 36) / ((0.9 * u) * (0.9 * u)))); if (Math.abs(u)  Math.abs(v) <= 0.001) System.out.println("The root of the function is " + v); } //I used Newton's Method here. In general terms, the formula is: //(x sub n+1) = (x sub n)  [ f(x) / f'(x) ] } }
Edit:
Also, you need to use Math.abs(u) and Math.abs(v). The last problem might lie in the fact that you need to ouput something. Right now your output is "The root of the function is ", but there's nothing after it.
Last edited by Antoniohawk; 09272004 at 12:39 AM.
xhtml :: attributes . tags . <abbr> vs. <acronym> CSS :: Box Model Hack
[CF TShirts] [CP Resources] [ithium hosting] [A New Generation of Tree Menu]

09272004, 07:33 AM #5
 Join Date
 Sep 2004
 Posts
 12
 Thanks
 0
 Thanked 0 Times in 0 Posts
Thanks!
Thanks for the help. I think I see where I was going wrong now. When I ran the program, I got a bunch of outputs so I will have to tweak that a little but I think that should send me off in the right direction.

09282004, 06:05 AM #6
 Join Date
 Sep 2004
 Posts
 12
 Thanks
 0
 Thanked 0 Times in 0 Posts
My program
In case anyone cares...here's what my final program looks like (and it works...got a little help from my instructor today after class...I was getting there at least!):
Code:public class Root2 { public static void main(String[] args) { //begin arguments //introduce variable u (the "guess" or [x sub n]), variable v ("guess +" or [x sub n+1]) double v; double u; //for function for(u = 100; u <= 100; u=u+.0001) { v = u  (((0.1 * u * u * u) + 3.6) / (0.3 * u * u)); if (Math.abs(v  u)<= 0.0001) System.out.println("The root of the function is " + v);//end if }//end for //I used Newton's Method here. In general terms, the formula is: // (x sub n+1) = (x sub n)  [ f(x) / f'(x) ] }//end main }//end class