This Python program uses the bisection method to solve the nonlinear equation:
$$x + e^x = 5$$
Rewritten as a root-finding problem:
$$F(x) = x + e^x - 5 = 0$$
The goal is to find a value of x such that F(x) = 0.
The result is rounded to four decimal places.
import math
def f(x):
return x + math.exp(x) - 5
def bisection(a, b, tol=1e-4, max_iter=100):
if f(a) * f(b) > 0:
raise ValueError("Function does not change sign in the given interval.")
for _ in range(max_iter):
c = (a + b) / 2
if abs(f(c)) < tol or (b - a) / 2 < tol:
return round(c, 4)
if f(a) * f(c) < 0:
b = c
else:
a = c
return round((a + b) / 2, 4)
# Run the program
root = bisection(1, 2)
print(f"Root of the equation: {root}")
Root of the equation: 1.6094
- The function F(x) is defined as x + e^x - 5
- The initial interval [a, b] must contain a sign change (e.g., [1, 2])
- At each step, the midpoint c is computed and checked
- The interval is narrowed based on the sign of F(c)
- The process continues until the desired precision is reached