programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# bisection method in java

Greenhorn
Posts: 9
given two non-linear functions, f(x) and g(x) find:
the point (x,y) of their intersection, that is, solve for f(x) = g(x), over an interval (a,b)
The intersection of nonlinear functions cannot be handled analytically; solutions are arrived at by iteratively adjusting a trial value of x systematically until the point of intersection is discovered, within some small tolerance.One scheme to solve the system is form a new function h = f-g, and solve h for a root x0 substituting the root value back into either f or g to obtain the corresponding y0. Your task is to use the method of bisection to find the point of intersection. Stop iterating when |Xn-Xn+1|<epsilon( tolerance). Your putput shall be a table shoing the following. use the precision of 8 decimal places for decimal numbers.
iteration# x h(x) f(x)-g(x) h'(x)
where h'(x)=[ h(x+dx)-h(x)]/ dx, where dx=0.001
here is my code
import java.io.*;
import java.util.Scanner;
import java.text.DecimalFormat;

public class Bisect
{
public static void main (String[] args)
{

double x, xnew; // value of iterates
double delta; // amount added to get next iterate
double error; // error estimate
double tol; // tolerance (max error)
int n, maxIts; // iteration count and maximum number of iterates
Scanner scan = new Scanner (System.in);

// Get input
System.out.println ("Bisection Method");
System.out.print ("Initial value for x: ");
x = scan.nextDouble();
System.out.println();
System.out.print ("Maximum number of iterations: ");
maxIts = scan.nextInt();
System.out.println ();
System.out.print ("Tolerance: ");
tol = scan.nextDouble();
System.out.println();

// Instantiate an object to format output to 6 decimal places
double factor = 1e6; // = 1 * 10^6 = 100000.
double result = Math.round(x * factor) / factor;
int a=1;
int b=3;
int c=2;
int exp_a=2;
int exp_b=1;
System.out.println("f(x) = ("+a+"x)^"+exp_a+" + ("+b+"x)^"+exp_b+" + ("+c+")");

// Perform bisection method
double a = 0, b = 2;
n = 1;
error = 100;
double dx = b - a;
System.out.format ("%10s%10s%10s%10s%10s","n","xn+1","h(xn)","[f(xn)-g(x)]","h'(x)\n");
while (n <= maxIts && error > tol)
{
x = ((a + b) / 2);
if ((f(a)*f(x)) < 0)
{
b = x;
dx = b - a;
}
else
{
a = x;
dx = b-a;

System.out.format ("%10s%10s%10s%10s%10s\n",n, Math.round(xnew * factor) / factor, Math.round(h(x) * factor) / factor, Math.round(dx * factor) / factor ,Math.round(f(x) * factor) / factor ,Math.round(hprime(x) * factor) / factor);
n++;

}

}

// ***********************************
// g(x)=x^3 - x^2 -4x - 1
// ***********************************
public static double g(double x)
{
return (-1 + x * (-4 + x * (-1 +x)));
}
// ***********************************
// f(x)=x^3 - x +1
// ***********************************
public static double f(double x)
{
return ( 1 + x * (-1 + x * (0 + x)) );
}
// ***********************************
// h(x)= f(x) - g(x)=x^2 + 3x +1
// ***********************************
public static double h(double x)
{
return (2 + x * (3 + x * (0 + 1));
}

// ********************
// The derivative of h
// ********************
public static double hprime(double x)
{
return (3 + x * (2 + 0 * x));
}
I am having many errors, can someone help me make sure i am doing the right thing and if so, why am i getting the errors. its not calling my functions
thanks

atem frank
Greenhorn
Posts: 9
Never mind guys, i got it, jeeeez it was just a parenthesis.lollll but just want to ask if i am doing the right thing about the question
thanks

lowercase baba
Bartender
Posts: 12565
49
Welcome!!
A friendly tip - I've taken your code and put it into code tags - it makes it MUCH easier to read. Please check out our FAQ on UseCodeTags.

atem frank
Greenhorn
Posts: 9
Thanks bro, i apologies for not following that instruction, can you please tell me if i am actually solving the problem i placed? because i am having a hard time how to solve roots x0 and y0.
any help will be appreciated

Marshal
Posts: 56600
172
It should actually look like this: notice that I have broken the long lines and remember how to do it.
I am afraid there are other style problems, including too many blank lines, multiple‑line //comments, spaces before comma, inconsistent indentation and several declarations on the same line. I think you are mixing tab and spaces for indenting. Sorting out those things as suggested here will make your code much easier to read.
One of your comments says you are creating an object to round values to 6 places, but you are not creating an object there. Your main method is far too long; it should be refactored to lots of smaller methods.

Master Rancher
Posts: 2045
75
atem frank wrote:Thanks bro, i apologies for not following that instruction, can you please tell me if i am actually solving the problem i placed? because i am having a hard time how to solve roots x0 and y0.
any help will be appreciated

hi Atem,

I see some problems.

1) in your while-loop, you are not setting the error variable to h(x), so you will always loop the maximum number of iterations.
Since your test is: while f(a) * f(x) < 0, the iteration will work, though.

2) you could simply define h(x) = f(x) - g(x), saving you from typing in the h(x)

3) you supply values for a and b. Now, if this interval does not contain any
points for which f(x) = g(x), you will not find a solution. You could use the hprime to find some
interval that should contain a correct x.

4) in this case, your interval might contain more than one solution (hx is of degree 2), and your test may fail

5) you are only solving for a value of x for which f(x) = 0. What about g(x)? you should sovle for h(x) = 0

6) what is this hprime for? At first I thought you were to use some form of Newton-Raphson, but the problem