Solving a Nonlinear Equation using Newton-Raphson Method

3 visualizaciones (últimos 30 días)
Juan Sebastian Castro Barrero
Juan Sebastian Castro Barrero el 12 de Feb. de 2021
Respondida: arushi el 27 de Ag. de 2024
It's required to solve that equation:
F(1)=1+(x(1)^2)-(x(2)^2)+((exp(x(1)))*cos(x(2)));
F(2)=(2*(x(1))*(x(2)))+((exp(x(1)))*sin(x(2)));
starting initials (-1,4)
5 iterations.
Please help me with the code ... I want the code to be with steps and iterations and if possible calculate the error also, please.

Respuestas (1)

arushi
arushi el 27 de Ag. de 2024
Hi Juan,
To solve the system of nonlinear equations using an iterative method like the Newton-Raphson method, we can write a MATLAB script that performs the iterations and computes the error in each step. Here is a step-by-step guide with the MATLAB code:
% Define the function F
F = @(x) [1 + x(1)^2 - x(2)^2 + exp(x(1))*cos(x(2));
2*x(1)*x(2) + exp(x(1))*sin(x(2))];
% Define the Jacobian matrix of F
J = @(x) [2*x(1) + exp(x(1))*cos(x(2)), -2*x(2) - exp(x(1))*sin(x(2));
2*x(2) + exp(x(1))*sin(x(2)), 2*x(1) + exp(x(1))*cos(x(2))];
% Initial guess
x0 = [-1; 4];
% Number of iterations
num_iterations = 5;
% Tolerance for convergence (if needed)
tolerance = 1e-6;
% Array to store errors
errors = zeros(num_iterations, 1);
% Iterative process
x = x0;
for k = 1:num_iterations
% Calculate the function value and Jacobian at the current point
F_val = F(x);
J_val = J(x);
% Newton-Raphson update
delta_x = -J_val \ F_val;
x = x + delta_x;
% Calculate error as the norm of the update step
error = norm(delta_x);
errors(k) = error;
% Display iteration details
fprintf('Iteration %d:\n', k);
fprintf('x = [%f, %f]\n', x(1), x(2));
fprintf('F(x) = [%f, %f]\n', F_val(1), F_val(2));
fprintf('Error = %f\n\n', error);
% Check for convergence (optional)
if error < tolerance
fprintf('Convergence achieved.\n');
break;
end
end
Iteration 1:
x = [-0.504703, 2.012047]
F(x) = [-14.240462, -8.278412]
Error = 2.048726
Iteration 2:
x = [-0.374867, 1.308330]
F(x) = [-3.051423, -1.485110]
Error = 0.715594
Iteration 3:
x = [-0.301307, 1.175764]
F(x) = [-0.392853, -0.317060]
Error = 0.151608
Iteration 4:
x = [-0.293178, 1.172634]
F(x) = [-0.006912, -0.025662]
Error = 0.008711
Iteration 5:
x = [-0.293163, 1.172660]
F(x) = [0.000082, -0.000039]
Error = 0.000030
% Display final results
fprintf('Final solution after %d iterations:\n', k);
Final solution after 5 iterations:
fprintf('x = [%f, %f]\n', x(1), x(2));
x = [-0.293163, 1.172660]
fprintf('Errors in each iteration:\n');
Errors in each iteration:
disp(errors(1:k));
2.0487 0.7156 0.1516 0.0087 0.0000
Hope this helps.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by