Solving a Nonlinear Equation using Newton-Raphson Method
6 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
arushi
2024 年 8 月 27 日
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
% Display final results
fprintf('Final solution after %d iterations:\n', k);
fprintf('x = [%f, %f]\n', x(1), x(2));
fprintf('Errors in each iteration:\n');
disp(errors(1:k));
Hope this helps.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!