Skip to content

Instantly share code, notes, and snippets.

@limistah
Last active December 3, 2025 12:26
Show Gist options
  • Select an option

  • Save limistah/3ddd13adacafaef862ab7957b2206287 to your computer and use it in GitHub Desktop.

Select an option

Save limistah/3ddd13adacafaef862ab7957b2206287 to your computer and use it in GitHub Desktop.
Program to solve numerical method using Bisection Method, Newton Raphson and Successive Approximation Method. Java
23D/7HCS/550 Owolabi Anthony Oladimeji
23D/7HCS/551 Adeniyi Adesola Sulaiman
23D/7HCS/552 Amoo Emmanuel Ayodeji
23D/7HCS/553 Ahmed Rilwan Abiola
23D/7HCS/554 Yahya Haneefat Opeyemi
23D/7HCS/556 Adeyemi Oluwaseun Abraham
23D/7HCS/557 Isiaka Aleem Aremu
23D/7HCS/559 Alo Oluwafemi Jacob
View the Result of the sample function here: https://imgur.com/a/o9bS59d
import java.util.Scanner;
public class Main {
// Sample function: f(x) = x^3 - x - 2
static double f(double x) {
return x * x * x - x - 2.0;
}
// Derivative: f'(x) = 3x^2 - 1
static double df(double x) {
return 3.0 * x * x - 1.0;
}
static void bisectionMethod(double a, double b, double tol, int maxIt) {
System.out.println("Bisection method");
double fa = f(a), fb = f(b);
if (fa == 0.0) {
System.out.println("Root found at a = " + a + " (f(a) == 0)");
return;
}
if (fb == 0.0) {
System.out.println("Root found at b = " + b + " (f(b) == 0)");
return;
}
if (fa * fb > 0.0) {
System.out.printf("Bisection requires f(a) and f(b) to have opposite signs.%n");
System.out.printf("f(a) = %.12g, f(b) = %.12g%n", fa, fb);
return;
}
System.out.format("%6s%18s%18s%18s%18s%18s%n", "iter", "a", "b", "mid", "f(mid)", "err");
double mid = a;
for (int iter = 1; iter <= maxIt; iter++) {
mid = 0.5 * (a + b);
double fm = f(mid);
double err = 0.5 * (b - a);
System.out.format("%6d%18.10f%18.10f%18.10f%18.10e%18.10e%n", iter, a, b, mid, fm, err);
if (Math.abs(fm) < tol || err < tol) {
System.out.printf("%nConverged: root ≈ %.12f after %d iterations%n", mid, iter);
return;
}
if (fa * fm < 0.0) {
b = mid;
fb = fm;
} else {
a = mid;
fa = fm;
}
}
System.out.printf("%nStopped after max iterations. Last mid = %.12f, f(mid) = %.12g%n", mid, f(mid));
}
static void newtonRaphson(double x0, double tol, int maxIt) {
System.out.println("Newton-Raphson method");
System.out.format("%6s%18s%18s%18s%18s%n", "iter", "x", "f(x)", "f'(x)", "err");
double x = x0;
for (int iter = 1; iter <= maxIt; iter++) {
double fx = f(x);
double dfx = df(x);
if (Math.abs(dfx) < 1e-15) {
System.out.format("%6d%18.10f%18.10e%18.10e%18s%n", iter, x, fx, dfx, "DERIV~0");
System.out.println("Derivative near zero. Stopping to avoid division by zero.");
return;
}
double xNext = x - fx / dfx;
double err = Math.abs(xNext - x);
System.out.format("%6d%18.10f%18.10e%18.10e%18.10e%n", iter, x, fx, dfx, err);
if (Math.abs(fx) < tol || err < tol) {
System.out.printf("%nConverged: root ≈ %.12f after %d iterations%n", xNext, iter);
return;
}
x = xNext;
}
System.out.printf("%nStopped after max iterations. Last x = %.12f, f(x) = %.12g%n", x, f(x));
}
// Successive Approximations: x_{n+1} = x_n - lambda * f(x_n)
static void successiveApproximations(double x0, double lambda, double tol, int maxIt) {
System.out.println("Successive Approximations (x_{n+1} = x_n - lambda * f(x_n))");
System.out.format("%6s%18s%18s%18s%18s%n", "iter", "x_n", "x_{n+1}", "f(x_n)", "err");
double x = x0;
for (int iter = 1; iter <= maxIt; iter++) {
double fx = f(x);
double xNext = x - lambda * fx;
double err = Math.abs(xNext - x);
System.out.format("%6d%18.10f%18.10f%18.10e%18.10e%n", iter, x, xNext, fx, err);
if (!Double.isFinite(xNext) || Math.abs(xNext) > 1e100) {
System.out.println("\nIteration appears to diverge (non-finite or too large). Stopping.");
return;
}
if (Math.abs(fx) < tol || err < tol) {
System.out.printf("%nConverged: root ≈ %.12f after %d iterations%n", xNext, iter);
System.out.printf("f(root) = %.12e%n", f(xNext));
return;
}
x = xNext;
}
System.out.printf("%nStopped after max iterations. Last x = %.12f, f(x) = %.12g%n", x, f(x));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Root finding demo: f(x) = x^3 - x - 2");
System.out.println("Choose method:");
System.out.println(" 1) Bisection");
System.out.println(" 2) Newton-Raphson");
System.out.println(" 3) Successive Approximations (x_{n+1} = x_n - lambda * f(x_n))");
System.out.print("Enter choice (1-3): ");
if (!sc.hasNextInt()) return;
int choice = sc.nextInt();
double tol;
int maxIt;
switch (choice) {
case 1: {
System.out.print("Enter interval a b (with f(a)*f(b) < 0): ");
double a = sc.nextDouble();
double b = sc.nextDouble();
System.out.print("Tolerance (e.g. 1e-8): ");
tol = sc.nextDouble();
System.out.print("Max iterations: ");
maxIt = sc.nextInt();
bisectionMethod(a, b, tol, maxIt);
break;
}
case 2: {
System.out.print("Enter initial guess x0: ");
double x0 = sc.nextDouble();
System.out.print("Tolerance (e.g. 1e-12): ");
tol = sc.nextDouble();
System.out.print("Max iterations: ");
maxIt = sc.nextInt();
newtonRaphson(x0, tol, maxIt);
break;
}
case 3: {
System.out.print("Enter initial guess x0: ");
double x0 = sc.nextDouble();
System.out.print("Enter lambda (step size, e.g. 0.1 or 0.01): ");
double lambda = sc.nextDouble();
System.out.print("Tolerance (e.g. 1e-8): ");
tol = sc.nextDouble();
System.out.print("Max iterations: ");
maxIt = sc.nextInt();
successiveApproximations(x0, lambda, tol, maxIt);
break;
}
default:
System.out.println("Invalid choice.");
}
sc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment