write aim, objective, algorithm, Matlab code with graph and interpretation for Secant…
- User
write aim, objective, algorithm, Matlab code with graph and interpretation for Secant Method
- Assistant
Aim: The aim of this project is to implement the Secant Method to find the root of a given nonlinear function using MATLAB.
Objective: The objective is to create an algorithm and MATLAB code to find the root of a nonlinear function using the Secant Method, plot the iterations on a graph, and interpret the results.
Algorithm for Secant Method:
- Define the nonlinear function f(x) and the initial guesses x0 and x1.
- Set the maximum number of iterations (max_iter) and the tolerance (tol).
- Initialize the iteration counter (iter) to 1.
- Calculate the value of the function at the initial guesses: f(x0) and f(x1).
- While (iter < max_iter) and (|x1 - x0| >= tol),
- Calculate the new guess x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0))
- Set x0 = x1 and x1 = x2
- Calculate the new function values f(x0) and f(x1)
- Increment the iteration counter iter by 1
- The root is approximately x1, the function value at the root is f(x1).
MATLAB Code:
% Define the nonlinear function f = @(x) x^3 - 6*x^2 + 11*x - 6; % Replace this equation with the desired function % Define initial guesses x0 = 0; x1 = 1; % Set maximum number of iterations and tolerance max_iter = 100; tol = 1e-6; % Initialize iteration counter iter = 1; % Calculate function value at initial guesses fx0 = f(x0); fx1 = f(x1); % Prepare figure for plotting figure; hold on; title('Secant Method Convergence'); xlabel('Iteration'); ylabel('Root Estimate'); plot(0, x0, 'bo'); plot(1, x1, 'bo'); iter_plot = 1:max_iter+1; roots_plot = zeros(1, max_iter+1); roots_plot(1) = x0; roots_plot(2) = x1; % Secant Method main loop while iter < max_iter && abs(x1 - x0) >= tol % Calculate new guess x2 = x1 - (fx1 * (x1 - x0) / (fx1 - fx0)); % Update values x0 = x1; x1 = x2; fx0 = fx1; fx1 = f(x1); % Increment iteration counter iter = iter + 1; % Plot the result plot(iter, x1, 'bo'); roots_plot(iter+1) = x1; end hold off; % Display the root found disp(['Root found: x = ', num2str(x1)]); disp(['Function value at the root: f(x) = ', num2str(fx1)]); % Interpretation of the results figure; plot(iter_plot, roots_plot); title('Secant Method Convergence'); xlabel('Iteration'); ylabel('Root Estimate'); xlim([1, iter]); grid on;Interpretation: The Secant Method MATLAB code returns the root of the given nonlinear function within the specified tolerance, plots the iterations against the root estimate at each iteration, and calculates the function value at the found root.
As the number of iterations increases, the difference between consecutive root estimates decreases, converging to the accurate root. Evaluating the function at the found root returns a small value, indicating that the Secant Method has successfully found an approximate root for the nonlinear function within the specified tolerance.
- User
write aim, objective, algorithm, Matlab code with graph and interpretation for Newton-Rapson method for system of nonlinear equations
- Assistant
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.