Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: new to programming and java, need help for class

1. ## 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

• 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.

• 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.

• 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.

• ## 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.

• ## 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

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•