syms x y eq1 = x*y - 1; eq2 = x^2 + y^2 - 4; % Define the initial guess for the…
- User
syms x y eq1 = x*y - 1; eq2 = x^2 + y^2 - 4;
% Define the initial guess for the variables x0 = 1.8; y0 = 0.5; guess = [x0; y0];
% Calculate the Jacobian matrix J = jacobian([eq1, eq2], [x, y]);
% Set the tolerance and maximum iteration values tol = 10^(-6); max_iter = 50;
% Initialize the iteration counter and the error iter = 0; error = inf;
% Update the variable values using the Newton-Raphson method while error > tol && iter < max_iter % Calculate the function values for the current guess f = [subs(eq1, [x, y], guess.'); subs(eq2, [x, y], guess.')];
% Calculate the Jacobian matrix for the current guess J_val = double(subs(J, [x, y], guess.'));
% Calculate the required change in the variables delta = -pinv(J_val) * f;
% Update the variable values guess = guess + delta.';
% Update the error and iteration counter error = norm(f); iter = iter + 1; end
% Plot the results [X, Y] = meshgrid(-2:0.1:2, -2:0.1:2); Z1 = X.^2 - Y.^2 - 1; Z2 = 2*X.*Y - 2; figure contour(X, Y, Z1, [0 0], 'LineWidth', 2) hold on contour(X, Y, Z2, [0 0], 'LineWidth', 2) scatter(guess(1), guess(2), 100, 'r', 'filled') xlabel('X') ylabel('Y') title('Newton-Rapson Method for System of Non-Linear Equations') legend('Equation 1', 'Equation 2', 'Solution') >> NewtonRapsonNonlinear Error using * Dimensions do not match.
Error in NewtonRapsonNonlinear (line 30) delta = -pinv(J_val) * f.';
NewtonRapsonNonlinear Error using sym/subs Inconsistency between sizes of second and third arguments.
Error in NewtonRapsonNonlinear (line 24) f = [subs(eq1, [x, y], guess.'); subs(eq2, [x, y], guess.')];
fix the error correctly
- Assistant
syms x y eq1 = x*y - 1; eq2 = x^2 + y^2 - 4;
% Define the initial guess for the variables x0 = 1.8; y0 = 0.5; guess = [x0; y0];
% Calculate the Jacobian matrix J = jacobian([eq1, eq2], [x, y]);
% Set the tolerance and maximum iteration values tol = 10^(-6); max_iter = 50;
% Initialize the iteration counter and the error iter = 0; error = inf;
% Update the variable values using the Newton-Raphson method while error > tol && iter < max_iter % Calculate the function values for the current guess f = [subs(eq1, [x, y], guess); subs(eq2, [x, y], guess)];
% Calculate the Jacobian matrix for the current guess J_val = double(subs(J, [x, y], guess));
% Calculate the required change in the variables delta = -pinv(J_val) * f;
% Update the variable values guess = guess + delta.';
% Update the error and iteration counter error = norm(f); iter = iter + 1; end
% Plot the results [X, Y] = meshgrid(-2:0.1:2, -2:0.1:2); Z1 = X.^2 - Y.^2 - 1; Z2 = 2*X.*Y - 2; figure contour(X, Y, Z1, [0 0], 'LineWidth', 2) hold on contour(X, Y, Z2, [0 0], 'LineWidth', 2) scatter(guess(1), guess(2), 100, 'r', 'filled') xlabel('X') ylabel('Y') title('Newton-Raphson Method for System of Nonlinear Equations') legend('Equation 1', 'Equation 2', 'Solution')