programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Newton's Method

Tarrell Fletcher
Ranch Hand
Posts: 60
How am I suppose to code the Newton's Method? You know the whole Xn+1 = Xn - [ f(x) / f ' (x) ].

Campbell Ritchie
Marshal
Posts: 56533
172
Write down on a piece of paper how it is supposed to work. Then you can work out how to code it from the pseudo-code.

Tarrell Fletcher
Ranch Hand
Posts: 60
Here is my current code for the Newton Method. But I just realized I don't know how raise numbers to the exp in java.

I know the double Xn part is wrong because you don't multiply by the exp. But how would I go about raising it to that particular power?

Winston Gutkowski
Bartender
Posts: 10575
66
Tarrell Fletcher wrote:Here is my current code for the Newton Method. But I just realized I don't know how raise numbers to the exp in java.

Good tip for next time: Not a bad thing (among many) to get straight before you start coding. In the meantime, have a look at the java.util.Math class.

Winston

Campbell Ritchie
Marshal
Posts: 56533
172
There is a method in the Math class which raises one number to the power of another. Only its name is abbreviated to three letters, the way you abbreviate cos, sin and log.

Tarrell Fletcher
Ranch Hand
Posts: 60
Ok here is the code once more. But now I am faced with a bigger problem I didn't know was going to happen. Usually when doing this method you have to take that Xn value and use that in place of x and then keep repeating the process til the numbers barely start changing right. Well how do I go about storing that Xn value and then having the process go again until I can get a number equal itself with maybe like 5 decimal places?

Tarrell Fletcher
Ranch Hand
Posts: 60
I wanna do something like, while ( Xn != df.format(Xn)) then you keep going until it is. Do I need to do some type of do while loop and put the loop like above the Xn calculation?

Winston Gutkowski
Bartender
Posts: 10575
66
Tarrell Fletcher wrote:I wanna do something like, while ( Xn != df.format(Xn)) then you keep going until it is. Do I need to do some type of do while loop and put the loop like above the Xn calculation?

Sure. And furthermore you can do it to as many (or few) decimal places as you like. The usual way is to check a 'delta' (difference to you and me) between the last calculation and this one. If it's less than a certain value, you're "accurate enough". All you have to do is to work out what that needs to be for 5 DPs.

Winston

Tarrell Fletcher
Ranch Hand
Posts: 60
Ok see really need to review all my coding because I can do it by hand but trying to code it is difficult. Lol I just don't know what the computer is willing to take from me. I been at this thing for about a day now and its driving me up the walls.

Tarrell Fletcher
Ranch Hand
Posts: 60
Ok I think I got it....

do while ( n1 != n){

n1 = n
Xn = the steps
n = Xn

}

Winston Gutkowski
Bartender
Posts: 10575
66
Tarrell Fletcher wrote:Ok I think I got it....

Actually, assuming "the steps" are your steps to calculate a "better" result, and you have a start value for 'n', it's a bit more like:

delta = difference you're willing to accept for accuracy
n1 = n + delta + 1
do while ( abs(n1 - n) > delta ) {
n1 = n
Xn = the steps
n = Xn
}

Winston

Tarrell Fletcher
Ranch Hand
Posts: 60
I managed to get it to work. I don't know why I was thinking about using an array. But hey thats expected from me I suppose lol.