Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-26-2004, 10:47 PM   PM User | #1
reneeccski
New Coder

 
Join Date: Sep 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
reneeccski is an unknown quantity at this point
Unhappy 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 TI-85 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) ]
			}	
			}
			}		
			}
Could someone tell me where I'm going wrong? I want the solution accurate to 0.001. The root is actually appriximately -3.301. It is very frustrating to try to implement algorithms in code when my instructor isn't actually teaching java to the class and just expects us to be able to do it!

--Renee

Last edited by Antoniohawk; 09-26-2004 at 11:48 PM..
reneeccski is offline   Reply With Quote
Old 09-26-2004, 11:47 PM   PM User | #2
Antoniohawk
Senior Coder

 
Join Date: Aug 2002
Location: Kansas City, Kansas
Posts: 1,518
Thanks: 0
Thanked 2 Times in 2 Posts
Antoniohawk will become famous soon enough
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) ]
	}
}
That's a start, there's probably a lot more needs to be done to make it work though. I have to go mow the lawn at the moment, but I'll try to help out some more when I'm done.

Last edited by Antoniohawk; 09-27-2004 at 12:00 AM..
Antoniohawk is offline   Reply With Quote
Old 09-27-2004, 12:17 AM   PM User | #3
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
Quote:
Originally Posted by Antoniohawk
It's really unfair for a teacher to dump a project on you when you haven't programmed in Java before.

lol I had a programming language course in college that required us to write a mid-side program in a different language every week and usually languages we had never seen or used before.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 09-27-2004, 12:31 AM   PM User | #4
Antoniohawk
Senior Coder

 
Join Date: Aug 2002
Location: Kansas City, Kansas
Posts: 1,518
Thanks: 0
Thanked 2 Times in 2 Posts
Antoniohawk will become famous soon enough
Quote:
Originally Posted by Spookster
lol I had a programming language course in college that required us to write a mid-side program in a different language every week and usually languages we had never seen or used before.
Now that is just crazy!

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; 09-27-2004 at 12:39 AM..
Antoniohawk is offline   Reply With Quote
Old 09-27-2004, 07:33 AM   PM User | #5
reneeccski
New Coder

 
Join Date: Sep 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
reneeccski is an unknown quantity at this point
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.
reneeccski is offline   Reply With Quote
Old 09-28-2004, 06:05 AM   PM User | #6
reneeccski
New Coder

 
Join Date: Sep 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
reneeccski is an unknown quantity at this point
Smile 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
reneeccski is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:53 AM.


Advertisement
Log in to turn off these ads.