import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

# Function f(x) = 3x - e^x

def f(x):

return 3 * x - np.exp(x)

# Modified Secant method function

def secant_method_fixed_x0(x0, x1, tolerance, max_iterations):

iterations_data =

for iteration in range(max_iterations):

f_x0 = f(x0)

f_x1 = f(x1)

# Check for division by zero

if f_x1 - f_x0 == 0:

print(“Error: Division by zero”)

return None

# Calculate the new xn

xn = x1 - (f_x1 * (x1 - x0)) / (f_x1 - f_x0)

# Store iteration data

eps = abs(f(xn)) # Update error to be based on function value

iterations_data.append([iteration + 1, x0, x1, f_x0, f_x1, eps])

# Check if the tolerance is met

if eps < tolerance:

break

# Update x1 for the next iteration, keep x0 fixed

x1 = xn

return iterations_data, xn, iteration + 1

# Input parameters

x0 = -1 # Fixed x0 as per your table

x1 = -0.5 # Initial guess for x1

tolerance = 0.01 # Desired tolerance

max_iterations = 10 # Maximum number of iterations

# Run the Secant method with fixed x0

result, final_xn, total_iterations = secant_method_fixed_x0(x0, x1, tolerance, max_iterations)

# If we have results, display them in a table and plot the convergence

if result:

# Convert the result into a DataFrame for pretty display

columns = [‘n’, ‘x0’, ‘xn’, ‘f(x0)’, ‘f(xn)’, ‘eps’]

df = pd.DataFrame(result, columns=columns)

# Display the result in tabular form

print(df)

print(f"\nSolution found: xn = {final_xn}, after {total_iterations} iterations")

# Plot the convergence of eps (error)

plt.plot(df[‘n’], df[‘eps’], marker=‘o’, color=‘b’, label=‘eps’)

plt.xlabel(‘Iteration’)

plt.ylabel(‘eps (error)’)

plt.title(‘Error Dynamics in Secant Method’)

plt.grid(True)

plt.legend()

plt.show()

else:

print(“No solution found due to error.”)