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:

# Bisection Method

Tarrell Fletcher
Ranch Hand
Posts: 60
Here's my current code for the Bisection Method. I am having an issue getting the program to iterate until the Hn = 0.000.....and other numbers.

As you can see I set the do while to run while (Hn != 0.000) but something is going wrong and I can't seem to figure it out.

Paul Clapham
Sheriff
Posts: 22841
43
Well, usually when you're working with floating-point numbers you don't hope for exact equality, you just want to get reasonably close.

But you say "something is going wrong". You certainly won't be able to figure it out until you identify what that something is. So that's your first step: what makes you think that something is going wrong?

Winston Gutkowski
Bartender
Posts: 10575
66
Tarrell Fletcher wrote:Here's my current code for the Bisection Method. I am having an issue getting the program to iterate until the Hn = 0.000.....and other numbers.

I haven't really looked much at your code, because you haven't made any attempt to document it for poor lugs like me who have no idea what your "bisection" algorithm is supposed to accomplish. However, an educated guess would be that Hn is never (or maybe takes an awful long time to get to) 0. And 0 is 0; the language doesn't make any distinction for how many '0's you put after the decimal point.

Winston

Tarrell Fletcher
Ranch Hand
Posts: 60
Ok so I tested out a problem x^3 - 7x -10 and the root should be like 3.18...... and so on. The program stops but the answers I am getting are all jacked up. Like after I run the do while loop I get Mn to be like 3.25. That should be because as you can see in my if statements I reset the "lower and upper" values to the appropriate values after they are outputted. But it seems like its not taking those new values and using them in the function. Maybe I messed up with trying to set the values to the newly found values. If you run the program you will see.

Tarrell Fletcher
Ranch Hand
Posts: 60
Winston Gutkowski wrote:
Tarrell Fletcher wrote:Here's my current code for the Bisection Method. I am having an issue getting the program to iterate until the Hn = 0.000.....and other numbers.

I haven't really looked much at your code, because you haven't made any attempt to document it for poor lugs like me who have no idea what your "bisection" algorithm is supposed to accomplish. However, an educated guess would be that Hn is never (or maybe takes an awful long time to get to) 0. And 0 is 0; the language doesn't make any distinction for how many '0's you put after the decimal point.

Winston

Yes you are right, but I managed to fix the Newton's Method. I was just told I need to do another iterative method to find the roots. I basically wrote out the Bisection on paper which is
1. Mn = (An+ Bn) /2
2. If F(Mn) = 0 then end program
3. Hn = (Bn - An) / 2
4. F(An) * F(Mn) then An+1 = An and Bn+1 = Mn
5. F(An) * F(Mn) then An+1 = Mn and Bn+1 = An

This is correct right? Well I basically took it one step at a time but something went wrong during that do while looping.

Winston Gutkowski
Bartender
Posts: 10575
66
• 1
Tarrell Fletcher wrote:Maybe I messed up with trying to set the values to the newly found values. If you run the program you will see.

I hate to say, but I don't really want to. What I see is a pile of code with no documentation, and variable names that probably have some meaning to you, but certainly not to me.

Why don't you back up and explain, for us less enlightened souls:
(a) What this program is supposed to do.
(b) What you ran it with.
(c) What result(s) you were expecting, and why.

Oh, and
(d) The result(s) you got; including any error mesages (exactly as they were produced please).

Winston

Tarrell Fletcher
Ranch Hand
Posts: 60
Winston Gutkowski wrote:
Tarrell Fletcher wrote:Maybe I messed up with trying to set the values to the newly found values. If you run the program you will see.

I hate to say, but I don't really want to. What I see is a pile of code with no documentation, and variable names that probably have some meaning to you, but certainly not to me.

Why don't you back up and explain, for us less enlightened souls:
(a) What this program is supposed to do.
(b) What you ran it with.
(c) What result(s) you were expecting, and why.

Winston

Ok this program is originally for the Newton's method. Once I did that I thought I was done and then realized there is a part 2 which says do the same as part one but I must use another/different iterative method/algorithm to find the roots. So I'm guessing I need to do another method, but I was also guessing maybe I just need to do some other iteration method to complete the newtons. But anywho...I tested the program out a bit as I slowly did it and things were checking out. But once I got to the point of making it iterate is where it went wrong. Im still new to this stuff and loops and etc are still a bit foggy to me so I kinda struggle with them. But I managed to do it with my Newtons method so I thought it would be similar. As you see after my do statement is where the if statements come in. Something is going wrong within those if statements because my 2 ending statements in those if statements should have the program do again but use those values but its not for some reason. And the variables I use ok ok down below

In the function I used x^3 -7x -10 [3, 4]

1. Mn = (Lower limit + Upper Limit) /2;
2. Am = The Mn value raised to the power of exp_a which is the exponent which the user is asked to give. ( I used Am because its for the A coefficient)
3. Bm = Just like Am but you apply it to the b coefficient.
Basically this is how I broke down getting F(Mn)
4. Fm = (a value * Am ) + (b * Bm) + c

5. Hn = Upper limit - lower limit
6. aLower = is my lower limit raised to the power of for coefficient a
7. bLower = is just like aLower but for coefficient b

Ummm.... I don't think I needed the aUpper and bUpper and etc. But I left it there just in case.

And from there you should be able to see what I am trying to do. I just want the new values at the end of the if statements to be carried over and be used in the calculating part and so on until I get the numbers to be even to each other. The newton method was easier because eventually after iteration the numbers dont change at all so it was easier to make the while statement set to stop when they are equal. But with this method its like they wont ever be equal because when it divide by that 2 it gets a tad bit longer.

Winston Gutkowski
Bartender
Posts: 10575
66
Tarrell Fletcher wrote:And from there you should be able to see what I am trying to do. I just want the new values at the end of the if statements to be carried over and be used in the calculating part and so on until I get the numbers to be even to each other. The newton method was easier because eventually after iteration the numbers dont change at all so it was easier to make the while statement set to stop when they are equal. But with this method its like they wont ever be equal because when it divide by that 2 it gets a tad bit longer.

OK, that deals with (a), (except I still don't follow what this reduction is for), what about (b-d)?

However, before you start writing reams, there are some things you need to understand about doubles:
1. They are NOT exact (specifically, they CANNOT hold certain values exactly - 0.1 being just one example). 0, however, is one of the exceptions to that rule.
2. (Following on from 1) Checking that one double that is the result of a calculation is equal to another double that is the result of another calculation may never be true (as I believe Campbell already said).

I suggest, before you go any further, you read this.

Winston

Tarrell Fletcher
Ranch Hand
Posts: 60
Winston Gutkowski wrote:
Tarrell Fletcher wrote:And from there you should be able to see what I am trying to do. I just want the new values at the end of the if statements to be carried over and be used in the calculating part and so on until I get the numbers to be even to each other. The newton method was easier because eventually after iteration the numbers dont change at all so it was easier to make the while statement set to stop when they are equal. But with this method its like they wont ever be equal because when it divide by that 2 it gets a tad bit longer.

OK, that deals with (a), (except I still don't follow what this reduction is for), what about (b-d)?

However, before you start writing reams, there are some things you need to understand about doubles:
1. They are NOT exact (specifically, they CANNOT hold certain values exactly - 0.1 being just one example). 0, however, is one of the exceptions to that rule.
2. (Following on from 1) Checking that one double that is the result of a calculation is equal to another double that is the result of another calculation may never be true (as I believe Campbell already said).

I suggest, before you go any further, you read this.

Winston

Oh im sorry I forgot about the other info you wanted. Well for this particular problem I was doing I am expecting my Mn value to be like around 3.18 something.
The result I got was 3.25 as my final output. But thats wrong because that 3.25 should have been stored as my lower value and then used in the calculation and then Mn should just keep changing.

Tarrell Fletcher
Ranch Hand
Posts: 60
See i just looked a bit more into the Bisection Method and I found some new info. I was taught that you should just stop and round if the numbers are barely changing. But this must be what you were talking about when you said Hn < E which I guess Epsilon is absolute value of ( "r" - Mn). But what is the "r"? If I know that then I can go about changing my conditions.

Tarrell Fletcher
Ranch Hand
Posts: 60
Ok I managed to do it and the error was soooooo simple. I realized after just asking to see the Mn output that I actually was getting the right answer. I just didn't see the iteration so I wasn't sure what was going on. But I removed some lines and now it actually works.

Winston Gutkowski
Bartender
Posts: 10575
66
• 1
Tarrell Fletcher wrote:But this must be what you were talking about when you said Hn < E which I guess Epsilon is absolute value of ( "r" - Mn). But what is the "r"? If I know that then I can go about changing my conditions.

Yes, epsilon is also used for a difference (although delta is more usual); but you really have to start using terms/names that other people can understand.
What is 'r'? or Hn? Not to mention Mn, Am, Bm and Fm. These are names that only mean something to you; and they make it very difficult for anyone trying to either (a) read your program, or (b) help you out on forums like this - particularly as you have absolutely no documentation in your code. Half the business (maybe even three-quarters) of being a good programmer is explaining yourself and writing simple, clear code.

Winston

Tarrell Fletcher
Ranch Hand
Posts: 60
Thanks a lot for the advice and thanks for making me understand Epsilon lol. I'm gonna be a good programmer soon because I am getting better and better each time I have to code something. Well as far as knowing how to set it up. I just need to make documenting my work a habit now.

Winston Gutkowski
Bartender
Posts: 10575
66
Tarrell Fletcher wrote:Thanks a lot for the advice and thanks for making me understand Epsilon lol. I'm gonna be a good programmer soon because I am getting better and better each time I have to code something. Well as far as knowing how to set it up. I just need to make documenting my work a habit now.

Good to hear.

BTW, you'll also find that just the business of doing it often sorts out problems for you, because it forces you to order your thoughts.

Anyways, good luck.

Winston

Winston Gutkowski
Bartender
Posts: 10575
66
Tarrell Fletcher wrote:Thanks a lot for the advice and thanks for making me understand Epsilon lol. I'm gonna be a good programmer soon because I am getting better and better each time I have to code something. Well as far as knowing how to set it up. I just need to make documenting my work a habit now.

Actually, I just double-checked and I'm not quite right (nothing new). They are both indeed differences, but Epsilon is the "discrepancy you're willing to accept" (and so is a constant) and the Delta is the difference between the previous calculation and this one (and so recalculated on each round). At least, that's what I gleaned from here.

Winston