Skip to content
QuantReadySign In
#447easyNumerical Methods

Bisection Method for Exponential Root

Time Limit: 2sMemory: 256MB

Problem

Implement the bisection method to find the root of f(x)=exbf(x) = e^x - b on the interval [lo,hi][\text{lo}, \text{hi}].

The root is x=ln(b)x = \ln(b), but you must find it using bisection — not the closed-form formula.

Algorithm: Repeat nn times:

  1. Compute mid=(lo+hi)/2\text{mid} = (\text{lo} + \text{hi}) / 2
  2. If f(mid)>0f(\text{mid}) > 0, set hi=mid\text{hi} = \text{mid}; otherwise set lo=mid\text{lo} = \text{mid}

After nn iterations, output (lo+hi)/2(\text{lo} + \text{hi}) / 2.

Input Format

Four space-separated values: b lo hi n

Output Format

The midpoint approximation of ln(b)\ln(b), to 4 decimal places.

Examples

Example 1
Input(Four space-separated values: b lo hi n)
1.0 -1.0 1.0 20
Output
0.0000

ln(1) = 0. Bisection on [-1, 1] converges to 0.0000.

Example 2
Input(Four space-separated values: b lo hi n)
2.0 0.0 1.0 25
Output
0.6931

ln(2) = 0.6931. Bisection on [0, 1] converges accurately.

Constraints

  • 0.01 ≤ b ≤ 10^6
  • lo < hi
  • f(lo) and f(hi) must have opposite signs
  • 1 ≤ n ≤ 100
  • Output to 4 decimal places
Loading interactive editor…