0% found this document useful (0 votes)
6 views3 pages

Proj.

Uploaded by

Koto Dennis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Proj.

Uploaded by

Koto Dennis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import numpy as np

def f(x):
return x**3 - 3*x**2 + x - 1

def bisection_method(a, b, tol=1e-6, max_iter=100):


iter_data = []
if f(a) * f(b) >= 0:
print("Bisection method fails.")
return None
iter_count = 0
while (b - a) / 2 > tol and iter_count < max_iter:
c = (a + b) / 2
iter_data.append((iter_count, f(c)))
if f(c) == 0:
return c, iter_data
if f(c) * f(a) < 0:
b=c
else:
a=c
iter_count += 1
return (a + b) / 2, iter_data

def secant_method(x0, x1, tol=1e-6, max_iter=100):


iter_data = []
iter_count = 0
while abs(x1 - x0) > tol and iter_count < max_iter:
x_next = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0))
iter_data.append((iter_count, f(x_next)))
x0 = x1
x1 = x_next
iter_count += 1
return x1, iter_data

def newton_raphson_method(x0, tol=1e-6, max_iter=100):


iter_data = []
iter_count = 0
while abs(f(x0)) > tol and iter_count < max_iter:
x0 = x0 - f(x0) / (3*x0**2 - 6*x0 + 1)
iter_data.append((iter_count, f(x0)))
iter_count += 1
return x0, iter_data

def fixed_point_iteration_method(g, x0, tol=1e-6, max_iter=100):


iter_data = []
iter_count = 0
while abs(g(x0) - x0) > tol and iter_count < max_iter:
x0 = g(x0)
iter_data.append((iter_count, f(x0)))
iter_count += 1
return x0, iter_data

def write_data(filename, method_name, root, iterations):


with open(filename, 'a') as file:
file.write(f"{method_name}:\n")
file.write("Root: {}\n".format(root))
for iter_count, fx in iterations:
file.write(f"Iteration {iter_count}: f(x) = {fx}\n")
file.write("\n")

# Define the function for fixed-point iteration method


def g(x):
return (3*x**2 - x + 1) / 3

# Initial guesses
a, b = -10, 10
x0, x1 = -10, 10
initial_guess_newton = 10
initial_guess_fixed_point = 10

# Find roots using different methods and write data to file


with open("data.dat", 'w') as file:
file.write("") # Clear previous content
root_bisection, iter_data_bisection = bisection_method(a, b)
write_data("data.dat", "Bisection Method", root_bisection, iter_data_bisection)
root_secant, iter_data_secant = secant_method(x0, x1)
write_data("data.dat", "Secant Method", root_secant, iter_data_secant)

root_newton, iter_data_newton = newton_raphson_method(initial_guess_newton)


write_data("data.dat", "Newton-Raphson Method", root_newton, iter_data_newton)

root_fixed_point, iter_data_fixed_point = fixed_point_iteration_method(g,


initial_guess_fixed_point)
write_data("data.dat", "Fixed-Point Iteration Method", root_fixed_point,
iter_data_fixed_point)

You might also like