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