Sure! Here are the computer codes for each problem:
Problem 1:
(a) Solve g(x) = x and show that P = 1 and P = 3 are fixed points.
'''
import numpy as np
def g(x):
return -(1/4)x**2 + 2x - (3/4)
x_vals = np.linspace(-10, 10, 1000)
y_vals = g(x_vals)
Plot g(x)
plt.plot(x_vals, y_vals)
plt.xlabel('x')
plt.ylabel('g(x)')
plt.title('Plot of g(x)')
plt.grid(True)
plt.show()
Solve g(x) = x
x_sol = np.roots([1/4, -2, 3/4])
print("The fixed points of g(x) = x are:")
for sol in x_sol:
print(sol)
'''
(b) Use the starting value p0 = 0.8 and compute p1, p2, and p3.
'''
def fixed_point_iteration(p0, num_iter):
p_vals = [p0]
for i in range(num_iter):
p_next = g(p_vals[i])
p_vals.append(p_next)
return p_vals
p0 = 0.8
num_iter = 3
p_vals = fixed_point_iteration(p0, num_iter)
print("The values p1, p2, and p3 using p0 = 0.8 are:")
for i in range(num_iter + 1):
print("p{} = {}".format(i, p_vals[i]))
'''
(c) Use the starting value p0 = 2.9 and compute p1, p2, and p3.
'''
p0 = 2.9
p_vals = fixed_point_iteration(p0, num_iter)
print("The values p1, p2, and p3 using p0 = 2.9 are:")
for i in range(num_iter + 1):
print("p{} = {}".format(i, p_vals[i]))
'''
(d) Find the errors Ek and relative errors Rk for the values pk in (b) and (c).
'''
fixed_points = [1, -3/2]
errors_b = [abs(p - fixed_points[0]) for p in p_vals]
relative_errors_b = [(abs(p - fixed_points[0]) / fixed_points[0]) * 100 for p in p_vals]
errors_c = [abs(p - fixed_points[1]) for p in p_vals]
relative_errors_c = [(abs(p - fixed_points[1]) / fixed_points[1]) * 100 for p in p_vals]
print("Errors and relative errors for values pk in (b):")
for i in range(num_iter + 1):
print("E{} = {}, R{} = {}%".format(i, errors_b[i], i, relative_errors_b[i]))
print("Errors and relative errors for values pk in (c):")
for i in range(num_iter + 1):
print("E{} = {}, R{} = {}%".format(i, errors_c[i], i, relative_errors_c[i]))
'''
Problem 2:
'''
def g(x):
return x * (np.cos(x) + x**2 * np.sin(x))
def bisection_method(f, a, b, tol):
if f(a) * f(b) > 0:
print("No root exists in the given interval.")
return None
while abs(b - a) > tol:
c = (a + b) / 2
if f(a) * f(c) < 0:
b = c
else:
a = c
return c
a_interval1, b_interval1 = 2, 3
a_interval2, b_interval2 = 6, 7
tolerance = 1e-4
root1 = bisection_method(g, a_interval1, b_interval1, tolerance)
root2 = bisection_method(g, a_interval2, b_interval2, tolerance)
print("Root within [2, 3]:", root1)
print("Root within [6, 7]:", root2)
'''
Problem 3: (Assuming initial guess x0 = 1, y0 = -1)
'''
def f1(x, y):
return (x - 1)2 + y2 + x*y - 20
def f2(x, y):
return x**2 + y + 1
def partial_fx(x, y):
return 2*x + y
def partial_fy(x, y):
return 2*y + x
def partial_gx(x, y):
return 2*x
def partial_gy(x, y):
return 1
def newton_method(f1, f2, partial_fx, partial_fy, partial_gx, partial_gy, x0, y0, tol, max_iter):
for i in range(max_iter):
fx = f1(x0, y0)
fy = f2(x0, y0)
J = partial_fx(x0, y0) * partial_gy(x0, y0) - partial_fy(x0, y0) * partial_gx(x0, y0)
delta_x = -(partial_gy(x0, y0) * fx - partial_fy(x0, y0) * fy) / J
delta_y = -(partial_fx(x0, y0) * fy - partial_fy(x0, y0) * fx) / J
x0 += delta_x
y0 += delta_y
if abs(fx) < tol and abs(fy) < tol:
break
return x0, y0
x0, y0 = 1, -1
tolerance = 1e-4
max_iterations = 100
root_x, root_y = newton_method(f1, f2, partial_fx, partial_fy, partial_gx, partial_gy, x0, y0, tolerance, max_iterations)
print("Solution of the nonlinear system:")
print("x =", root_x)
print("y =", root_y)
'''
Problem 4:
'''
import numpy as np
def Tn(x):
if n == 0:
return 1
elif n == 1:
return x
else:
return 2xTn(x) - Tn(x)
n = 100
roots = np.zeros(n)
for i in range(n):
x_guess = np.cos(np.pi * (i + 0.75) / (n + 0.5))
tol = 1e-4
max_iter = 100
for j in range(max_iter):
x_next = x_guess - Tn(x_guess) / Tn(x_guess, 1)
if abs(x_next - x_guess) < tol:
roots[i] = x_next
break
x_guess = x_next
print("The 100 roots of T100(x) = 0 are:")
for root in roots:
print(root)
'''
Feel free to use and modify these codes as needed. Let me know if you have any questions!