# Newton's Method

Tarrell Fletcher

Ranch Hand

Posts: 60

Campbell Ritchie

Marshal

Posts: 52516

118

Tarrell Fletcher

Ranch Hand

Posts: 60

posted 5 years ago

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?

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?

posted 5 years ago

Good tip for next time: Not a bad thing (among many) to get straight

Winston

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Campbell Ritchie

Marshal

Posts: 52516

118

posted 5 years ago

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

posted 5 years ago

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

posted 5 years ago

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

Articles by Winston can be found here

Tarrell Fletcher

Ranch Hand

Posts: 60

Tarrell Fletcher

Ranch Hand

Posts: 60

posted 5 years ago

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

Articles by Winston can be found here