Alright well I was out sick for a while and when I came back we had already started our new project. Basically for the first version it just has to find the root (zero) of a function which is set into the code as a variable, given the low and high (also variables). I know I need to use recursion in the root finder method but I was gone for that also so I am not totally clear on it either. Here is what I've wrote so far, I'm kinda stuck on where to go with it, so if anyone could help me out a little it would be greatly appreciated.
Code:public class main { double t = 0.001; // t represents tolerance double Lo = 10; double Hi = -10; public void main() { } double abs(double x) {//Pre accepts double x //Post returns absolute value if (x >= 0) return x; else return (-x); } int sgn (double x) {//Pre accepts double x //Post returns -1 if negative, 0 if 0, 1 is postive if (x > 0){ return 1;} if (x < 0){ return -1;} if (x==0){ return 0;} } double f (double x) {// f(x)-the function return x-2; } double RootFinder (double Lo, double Hi) { double Mid = ((Lo + Hi)/2); if (((sgn(f(Lo)))==(-1)))&&((sgn(f(mid)))==(-1))) RootFinder(mid, Hi); if (((sgn(f(mid)))==(1)) && ((sgn(f(Hi))==(1))) RootFinder(Lo, mid); if (((sgn(f(mid)))==(1) && ((sgn(f(Hi)))==(-1))) RootFinder(mid, Hi); if ((abs(Hi-Lo)) < t) return Mid; }


Reply With Quote


Bookmarks