Solve the system 81x1 == - cos(x1)+9x+27 sin(x3) 3x2 sin(x1) + 3 cos(x3) = 18x3 = -2 cos(x1) + 6x2 + 3 sin(x3) using Newton-Raphson method. Use appropriate initial iterate, tolerance 10-15 and maximum iter- ation of 100. Include in your documentation the solution, relative error, and the number of iteration. should contain the code you used to solve the problem using the Newton Method.

icon
Related questions
Question

import numpy as np

def f(x):
    """
    Defines the vector-valued function f(x) for the given system of equations.
    
    Parameters:
    - x: The vector of unknowns.
    
    Returns:
    - f_x: The value of f(x).
    """
    f1 = 81 * x[0] + np.cos(x[0]) - 9 * x[1]**2 - 27 * np.sin(x[2])
    f2 = 3 * x[0] - np.sin(x[0]) - 3 * np.cos(x[2]) - 3 * x[1]
    f3 = 18 * x[0] + 2 * np.cos(x[0]) - 6 * x[1] - 3 * np.sin(x[2])
    return np.array([f1, f2, f3])

def jacobian_f(x):
    """
    Defines the Jacobian matrix of f(x) for the given system of equations.
    
    Parameters:
    - x: The vector of unknowns.
    
    Returns:
    - J: The Jacobian matrix of f(x).
    """
    J = np.zeros((3, 3))
    J[0, 0] = 81 - np.sin(x[0])
    J[0, 1] = -18 * x[1]
    J[0, 2] = -27 * np.cos(x[2])
    J[1, 0] = 3 - np.cos(x[0])
    J[1, 1] = -3
    J[1, 2] = 3 * np.sin(x[2])
    J[2, 0] = 18 - 2 * np.sin(x[0])
    J[2, 1] = -6
    J[2, 2] = -3 * np.cos(x[2])
    return J

def newton_raphson_method(x0, tol=1e-15, max_iter=100):
    """
    Solves the system of equations using the Newton-Raphson method.
    
    Parameters:
    - x0: The initial guess for the solution.
    - tol: The tolerance for convergence.
    - max_iter: The maximum number of iterations.
    
    Returns:
    - x: The solution vector.
    - rel_error: The relative error.
    - num_iter: The number of iterations performed.
    """
    x = x0.copy()
    for num_iter in range(1, max_iter + 1):
        f_x = f(x)
        J = jacobian_f(x)
        delta_x = np.linalg.solve(J, -f_x)
        x += delta_x
        if np.linalg.norm(delta_x) / np.linalg.norm(x) < tol:
            rel_error = np.linalg.norm(f(x)) / np.linalg.norm(f(x0))
            return x, rel_error, num_iter
    raise ValueError("Newton-Raphson method did not converge.")

# Set the initial guess and tolerance
x0 = np.array([0.5, 0.5, 0.5])
tol = 1e-15
max_iter = 100

# Solve the system using the Newton-Raphson method
x, rel_error, num_iter = newton_raphson_method(x0, tol, max_iter)

# Print the solution, relative error, and number of iterations
print("Solution:")
print("x1 =", x[0])
print("x2 =", x[1])
print("x3 =", x[2])
print("Relative Error:", rel_error)
print("Number of Iterations:", num_iter)

Solve the system
81x1
==
- cos(x1)+9x+27 sin(x3)
3x2 sin(x1) + 3 cos(x3)
=
18x3 = -2 cos(x1) + 6x2 + 3 sin(x3)
using Newton-Raphson method. Use appropriate initial iterate, tolerance 10-15 and maximum iter-
ation of 100. Include in your documentation the solution, relative error, and the number of iteration.
should contain the code you used to solve the problem using the Newton Method.
Transcribed Image Text:Solve the system 81x1 == - cos(x1)+9x+27 sin(x3) 3x2 sin(x1) + 3 cos(x3) = 18x3 = -2 cos(x1) + 6x2 + 3 sin(x3) using Newton-Raphson method. Use appropriate initial iterate, tolerance 10-15 and maximum iter- ation of 100. Include in your documentation the solution, relative error, and the number of iteration. should contain the code you used to solve the problem using the Newton Method.
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer