Aim: The aim of this project is to implement the Newton-Raphson method to find the solution for a system of nonlinear equations using MATLAB.
Objective: The objective is to create an algorithm and MATLAB code to find the solution for a system of nonlinear equations using the Newton-Raphson method, plot the iterations on a graph, and interpret the results.
Algorithm for Newton-Raphson method for a System of Nonlinear Equations:
- Define the nonlinear functions f_i(x1, x2, ..., xn) as a vector f(x), where x is a vector of variables [x1, x2, ..., xn].
- Define the Jacobian matrix J, where J_ij = df_i/dx_j.
- Set the maximum number of iterations (max_iter), the tolerance (tol), and an initial guess for the solution x0.
- Initialize the iteration counter (iter) to 1.
- While (iter < max_iter) and (the norm of f(x) >= tol),
- Evaluate the Jacobian matrix J and the function vector f(x) at the current guess x0.
- Solve the linear system J(dx) = -f(x) for dx.
- Update the guess x0 = x0 + dx.
- Increment the iteration counter iter by 1.
- The solution of the system is approximately x0.
MATLAB Code:
% Define the nonlinear functions as a vector
f = @(x) [x(1)^2 + x(2)^2 - 1; x(1)*x(2) - 0.5]; % Example: circle (x^2+y^2=1) and hyperbola (xy=0.5)
% Define the Jacobian matrix
J = @(x) [2*x(1), 2*x(2);
x(2), x(1)];
% Set maximum number of iterations and tolerance
max_iter = 100;
tol = 1e-6;
% Define the initial guess
x0 = [1; 1]; % Example initial guess
% Initialize iteration counter
iter = 1;
% Initialize variable to store iteration history
iteration_history = zeros(2, max_iter + 1);
iteration_history(:, 1) = x0;
% Newton-Raphson Method main loop
while iter < max_iter && norm(f(x0)) >= tol
% Evaluate the Jacobian matrix and function vector at current guess
J_curr = J(x0);
f_curr = f(x0);
% Solve the linear system
dx = -J_curr \ f_curr;
% Update the guess
x0 = x0 + dx;
% Increment iteration counter
iter = iter + 1;
% Store the new solution in the iteration history
iteration_history(:, iter) = x0;
end
% Display the solution found
disp('Solution found:');
disp(x0);
% Plot the iteration history
figure;
plot(iteration_history(1, 1:iter), iteration_history(2, 1:iter), 'bo-', 'MarkerSize', 5);
xlabel('x1');
ylabel('x2');
title('Newton-Raphson Iteration History for System of Nonlinear Equations');
grid on;
Interpretation:
The Newton-Raphson method MATLAB code returns the solution for the system of the given nonlinear functions within the specified tolerance, plots the iteration history of the variables in a 2D graph, and calculates the function values at the found solution.
As the number of iterations increases, the variables converge to the accurate solution. The residual function values at the found solution are small, indicating that the Newton-Raphson method has successfully found an approximate solution for the system of nonlinear equations within the specified tolerance.