need help Bisect program

need help Bisect program

Page 1 of 1

1 Replies - 981 Views - Last Post: 05 May 2007 - 07:40 PM Rate Topic: -----

#1 doggypoo123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-April 07

need help Bisect program

Posted 03 May 2007 - 01:05 PM

public interface Function {
			public double eval(double x);
		}

_____________________________________
public class Cubic implements Function {
			public double eval(double x) {
	  return  x * x * x - 3.0 * x + 1.0;

			}
		}


_____________________________________________
 import java.io.*;
  import java.util.*; 
	public class Solver{

			private Function func;

			public Solver(Function f) {func = f;}

			private int sign(double x) {
				//some thing is wrong here.
					if(x>0) return -1;
				 if(x<0) return -1;
				 if(x=0) return 0;
			}

			private double bisect(double xlo, double xhi, double tol) {
				// This recursive method uses the bisection method
				// uses the sign method that doesnít work
			   
			   double xmid= (xlo+xhi)/2;
			   int k=0;
			   
			   
			  if(f(xlo)==0){
				  list.add(f(xlo));
				  k=1;
				}
			  if(f(xhi)==0){
				  list.add(f(xhi));
				  k=1; 
				}
			   
			   
			   while(k==0) {
				   //x12 = (x1+x2)/2;
				   if(f(xmid)*func(x1)<0)
						xhi=xmid;
				   else
				   xlow=xmid;
				   double temp=(x2-x1);
				   if( temp < tol) {
					   list.add(f(temp));
					   k=1;
				   }
				} 
			   
			}

			public List<Double> solve(double a, double b, int n, double tol) {
			  
				 double h=(b-a)/n;
			  for (int i=0; i<n;i++){
				  x[i-1]=a + (i-1)*h;
				  x[i]=a+i*h;
				  if (x[i].sign = 0) list.add(f(x[i-1]));
				  if (x[i-1].sign = 0) list.add(f(x[i-1]));
				  if (x[i].sign + x[i-1].sign= 0) bisect(x[i-1],x[i],tol);
				  if (x[i].sign + x[i-1].sign= -2) bisect(x[i],x[i-1],tol);
				  return solve;

// I know I need to create an array list but I donít know where to do it
			 }	
			}
		}


_____________________________________________
/////////////////////////////////main class
import java.io.*;
	import java.util.*; 
 public class Program4 {
		public static void main (String[] args) {
				 Scanner kbd;
			   Solver x;
			   
				 
				 kbd = new Scanner(System.in);
				  System.out.printf("Lower bound? ");
					 double x1 = kbd.nextDouble();
										  
					 System.out.printf("Upper bound? ");
					 double x2= kbd.nextDouble();
					 
										  
					 System.out.printf("Subintervals? ");
					 int s = kbd.nextInt();
					 x = new Solver();
					 x.solve(x1,x2,s,0.0000001);
					// this is where I  donít know how to get an array returned, should I create one here. Also im have trouble making the Solver object

					}
					
   }

This post has been edited by William_Wilson: 05 May 2007 - 07:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: need help Bisect program

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: need help Bisect program

Posted 05 May 2007 - 07:40 PM

please do not forget to use [code ][ /code] tags
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1