d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Iso Help W/ Java Math Problem > Beginniner's Level?
Add Reply New Topic New Poll
Member
Posts: 3,422
Joined: Apr 8 2007
Gold: 0.00
Nov 12 2014 03:30am
Hi everyone,

I've never posted here before, but do visit from time to time to observe and learn what I can. Unfortunately, I'm somewhat stuck on a question (math problem) and was wondering if I could get some help from the community. Any help would be appreciated.

Anyways, I have to write a code to find a root of a function on a given interval using the bisection method.

My code is currently written to solve the problem through recursion (because that's the only way I could come up with right now), but the problem is I don't know how to specify the base case, or special case.
What I would like to ask is how can I specify the base case so that the recursion can end? One of the specification is that the interval's precision is to be about 1/1000000.

I look forward to some assistance. Thank you.

PS - It's been a very long time since I've touched math, but I'm assuming the "root" would eventually be the midpoint, or a+b / 2. I could be wrong
Member
Posts: 398
Joined: Jan 14 2014
Gold: 4.31
Nov 12 2014 04:53am
i knida know what yur problem is but id have to see your code to give you a base case for your method to see the recusive part to find the base... so ill pm if you see this first pm me your code.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 12 2014 09:51am
Code

public double recurse(double param) {
if (baseCase) { // whatever your baseCase condition is, evaluated to boolean
return someNumber; // no more recursion!
}
return recurse(change the parameter);
}
Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Nov 12 2014 08:47pm
wouldn't you hit your base case when
Code
abs(a - b) <= 1/1000000


This post was edited by Aimed_Shot on Nov 12 2014 08:47pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 13 2014 12:13am
Uh...something like this maybe?

Code
public abstract class Bisection {

private double a, b;

public Bisection(double a, double b)
{
this.a = a;
this.b = b;
}

public abstract double f(double x);

public double calculate(double tolerance)
{
return calculate(a, b, tolerance);
}

private double calculate(double a, double b, double tolerance)
{
double c = (a + b) / 2;

if(Math.abs(a-b) < tolerance)
return c;

if((f(a) * f(c)) < 0)
return calculate(a,c,tolerance);

else
return calculate(c,b,tolerance);
}
}


Code

public class XSquaredMinusTwo extends Bisection
{

public XSquaredMinusTwo(double a, double b)
{
super(a, b);
}


public double f(double x) {

return Math.pow(x, 2) - 2.0;
}

public static void main(String[] args)
{
Bisection b = new XSquaredMinusTwo(1.0, 2.0);

System.out.println(b.calculate(1.0/1000000.0));
}



}



Sorry if the encapsulation seems weird. I couldn't think of a better way off the top of my head.
Member
Posts: 3,422
Joined: Apr 8 2007
Gold: 0.00
Nov 13 2014 12:30am
Quote (carteblanche @ Nov 12 2014 07:51am)
Code
public double recurse(double param) {
  if (baseCase) { // whatever your baseCase condition is, evaluated to boolean
        return someNumber; // no more recursion!
    }
  return recurse(change the parameter);
}


I get this much (sort of), just had trouble figuring out a condition for the special case.

Quote (PublicStaticVoidMain @ Nov 12 2014 02:53am)
i knida know what yur problem is but id have to see your code to give you a base case for your method to see the recusive part to find the base... so ill pm if you see this first pm me your code.



I didn't paste my code because it's 1) sloppy, 2) wanted insight and different perspective on how I could view the problem so that I could attempt to solve it, but here's what I initially had before Aimed_Shot provided a different condition:

Code
public static void checkf(double a, double b)
{
// check for precision (special case)
if(a % (1 / 1000000) == 0 && b % (1 / 1000000) == 0)
{
System.out.println("a = " + a + "/nb = " + b);
}
if(f(a) * f(b) < 0)
{
System.out.println("if statement");
System.out.println("f(a) * f(b) = " + f(a) * f(b));
checkf(f(a), f((a + b) / 2));
}
else
{
System.out.println("else statement");
System.out.println("f(a) * f(b) = " + f(a) * f(b));
checkf(f((a + b) / 2), f(b));
}
}


I did modulus because... I don't know why I tried modulus after looking at it again. It was a stupid idea on my part, guess it was a desperate try more than anything.

Quote (Aimed_Shot @ Nov 12 2014 06:47pm)
wouldn't you hit your base case when
Code
abs(a - b) <= 1/1000000


Thank you! I tried this, and I've definitely made progress so far. I'm comparing results with that shown on Wolfram, but it seems my recursive call properly computes until it has to change from the lower range to the higher range.

*edit

Hey, Minkomonster, didn't see your post until just now. That's similar to what I currently have right now, except my function takes in 2 parameters - lower limit and upper limit, but I think that is what is throwing me off:
Code

public static void checkf(double a, double b)
{
// check for precision (special case)
if(Math.abs(a - b) <= 1/1000000)
{
System.out.println("\na = " + a + "\nb = " + b);
return;
}
if(f(a) * f(b) < 0)
{
System.out.println("\ninterval: [" + a + ", " + b + "]");
System.out.println("f(" + a + ") = " + f(a));
System.out.println("f(" + b + ") = " + f(b));
checkf(a, (a + b) / 2);
}

else
{
System.out.println("\ninterval: [" + a + ", " + b + "]");
System.out.println("f(" + a + ") = " + f(a));
System.out.println("f(" + b + ") = " + f(b));
checkf((a + b) / 2, b);
}

}

After the 5th recursive call, I get the value 1.0625 as one of the new midpoint, however, I did write the problem out on paper a few times and have never encountered that exact number. Same with the computation done on wolfram - http://www.wolframalpha.com/input/?i=x^3%2Bx-3+bisection+[1%2C+2]

Thanks everybody for all the responses so far. I appreciate it.

This post was edited by zomgdanny on Nov 13 2014 12:41am
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 13 2014 12:36am
Code
if(a % (1 / 1000000) == 0 && b % (1 / 1000000) == 0)


That is doing integer division. 1 / 1000000 is 0. This is not what you want. Try 1.0 / 1000000 to get a double. Otherwise you will get an ArithmeticException due to the division by 0 in your modulus operation.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 13 2014 12:53am
Code
if(Math.abs(a - b) <= 1/1000000)

1/1000000 evaluates to 0 due to integer division. You aren't testing your precision properly. Try 1.0/1000000

Code
if(f(a) * f(b) < 0)


That is wrong. Compare with my solution above to see where you went wrong.


Other than that, your algorithm is fine.

This post was edited by Minkomonster on Nov 13 2014 12:53am
Member
Posts: 3,422
Joined: Apr 8 2007
Gold: 0.00
Nov 13 2014 04:20am
Quote (Minkomonster @ Nov 12 2014 10:53pm)
Code
if(Math.abs(a - b) <= 1/1000000)

1/1000000 evaluates to 0 due to integer division. You aren't testing your precision properly. Try 1.0/1000000

Code
if(f(a) * f(b) < 0)


That is wrong. Compare with my solution above to see where you went wrong.


Other than that, your algorithm is fine.


I checked your solution, and it works for me.
I also left the condition as integer division, and the program did not terminate. I see where I went wrong.

I appreciate all the help you guys have given!
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll