import math
def f(x):
return [Link](x) - x * [Link](x)
def df(x):
return -[Link](x) - [Link](x) * (1 + x)
x = 0.5 # Initial guess
print(f"Initial guess: {x}")
print(f"f({x}) = {f(x)}")
print(f"df({x}) = {df(x)}")
print("\nIterations:")
for i in range(1, 6):
fx = f(x)
dfx = df(x)
h = -fx / dfx
x_new = x + h
print(f"n={i}: x_{i-1}={x:.6f}, f(x)={fx:.6f}, f'(x)={dfx:.6f},
x_{i}={x_new:.6f}")
if abs(x_new - x) < 0.00005: # Convergence check for 4 decimal
places
print(f"Converged to {x_new:.6f}")
break
x = x_new
Initial guess: 0.5
f(0.5) = 0.05322192654030866
df(0.5) = -2.9525074446543953
Iterations:
n=1: x_0=0.500000, f(x)=0.053222, f'(x)=-2.952507, x_1=0.518026
n=2: x_1=0.518026, f(x)=-0.000817, f'(x)=-3.043492, x_2=0.517757
n=3: x_2=0.517757, f(x)=-0.000000, f'(x)=-3.042124, x_3=0.517757
Converged to 0.517757
To find the real root of the equation cos x − x e x =0 using the Newton-Raphson method, correct to
four decimal places, we follow the steps below.
1. The Newton-Raphson Formula
f (x n)
The iterative formula is given by: x n+1=x n − ′
f (x n )
Where:
x
f ( x)=cos x − x e
′
f ( x) is the derivative of f (x).
′ d d x
Finding the Derivative f ′ ( x): Using the product rule for x e x: f ( x)= (cos x)− (x e )
dx dx
′ x x ′ x
f ( x)=− sin x −(1 ⋅e + x ⋅e ) f ( x)=− sin x − e (1+ x)
2. Initial Guess ( x 0)
We first locate an interval where the root lies by checking values (remember to use radians).
f (0)=cos (0)−0=1 (Positive)
1
f (1)=cos (1)−1 ⋅e ≈ 0.54 − 2.718=− 2.178 (Negative)
Since the sign changes between x=0 and x=1, a root exists in this interval. Since f (0) is closer
to 0 than f (1), let's choose a starting value closer to 0, say x 0=0.5 .
3. Iteration Table
We repeat the process until the value of x stabilizes to four decimal places.
Current Value Next Value (
′
Iteration (n ) ( x n) f (x n) f ( xn ) x n+1 )
1 0.5000 0.0532 −2.9525 0.5180
2 0.5180 − 0.0008 −3.0435 0.5177
3 0.5177 0.0000 −3.0421 0.5177
Detailed Calculations:
0.0532
Iteration 1: x 1=0.5 − ≈ 0.5+0.0180=0.5180
−2.9525
−0.0008
Iteration 2: x 2=0.5180 − ≈ 0.5180 −0.0003=0.5177
−3.0435
0.0000
Iteration 3: x 3=0.5177 − ≈ 0.5177
− 3.0421
Since x 2 and x 3 are identical up to four decimal places, we stop here.
Conclusion
The real root of the equation cos x − x e x =0 correct to four decimal places is:
0.5177
Would you like me to show how to check this result by plugging it back into the original
equation?