Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java program which finds an approximate solution to an equation f(x)=0.

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java program which finds an approximate solution to an equation f(x)=0.

    I have to write a program which finds an approximate solution to an equation f(x)=0 for some function f using the bisection method. To solve the program using the method first find two values of x, A,and B, such that when evaluated in the function f(x) they give opposite signs for the y value. Treat the positive value as an upper bound and the negative value as a lower bound. Divide the space between A and B in half and evaluate the function at that new point. I'm supposed to test my program using multiple polynomials. Use at least 3 methods. One to read 5 coefficients, one to calculate the value of the polynomial, and one to do the binary bisection search.

    public class Bisection2
    {
     
    	public static void main(String args[])
    	{
     
    	}
     
    		private double precision = .000001;
    		private double cubic(double x)
    		{
    			double Fn = ((Math.pow(x,3)) - (7 * (Math.pow(x,2))) + (5 * x) + 3);
    			return (Fn);
    		}
    		private  double bisector(double left, double right)
    		{
    				double midpoint;
    				while (Math.abs(right - left) > precision)
    				{
    				// Find the Midpoint of the funtion
    				midpoint = ((left + right) / 2);
    				System.out.print(midpoint);
    				System.out.print("\n");
    				//determine the appropriate half to search in
    				if ((cubic(left) * cubic(midpoint)) > 0)
    				left = midpoint;
    				else
    				right = midpoint;
    				}
    				return ((left + right) / 2);
    		}
    		private int Main()
    		{
    			System.out.print(bisector(0, 2));
    			System.out.print("\n");
    			System.out.print(bisector(5, 7));
    			System.out.print("\n");
    		return 0;
    		}
    	}
    public class BisectionMethod {
     
    	  public static void main(String argv[]) {
    	    double x = 0, del = 1e-6, a = 1, b = 2;
    	    double dx = b-a;
    	    int k = 0;
    	    while (Math.abs(dx) > del) {
    	      x = (a+b)/2;
    	      if ((f(a)*f(x)) < 0) {
    	        b  = x;
    	        dx = b-a;
    	      }
    	      else {
    	        a = x;
    	        dx = b-a;
    	      }
    	      k++;
    	    }
    	    System.out.println("Iteration number: " + k);
    	    System.out.println("Root obtained: " + x);
    	    System.out.println("Estimated error: " + dx);
    	  }
     
    	// Method to provide function f(x)=exp(x)*log(x)-x*x.
     
    	  public static double f(double x) {
    	    return Math.exp(x)*Math.log(x)-x*x;
    	  }
    	}

    Here are two programs I have created, but I don't think they are exactly what the assignment is asking for. I think I have to use the system console to ask for the coefficients of x^whatever power. and if the x raised to that power does not exist enter in a zero for the coefficient. I'm not exactly sure how I would do this and the first program I created isn't running either. I just started programming and have been stuck on this one for a while now. Any help would be greatly appreciated. Creating the methods is what I struggle the most with.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Java program which finds an approximate solution to an equation f(x)=0.

    Using the System console.

    Try the Scanner class.

    Scanner (Java Platform SE 7 )

    Scanner console = new Scanner(System.in);

    double something = console.nextDouble();

Similar Threads

  1. Java Equation correct syntax
    By macko in forum What's Wrong With My Code?
    Replies: 18
    Last Post: November 5th, 2011, 12:16 AM
  2. Please help me with the solution
    By abhyudayaupadhyay in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 6th, 2011, 04:21 PM
  3. Linear Equation Help !!!
    By thangavel in forum Algorithms & Recursion
    Replies: 1
    Last Post: January 13th, 2011, 06:32 AM
  4. Help with Quadratic forumla equation in java please.
    By taylor6132 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2010, 08:27 PM
  5. Replies: 0
    Last Post: April 5th, 2010, 12:41 PM

Tags for this Thread