syms x y eq1 = xy - 1; eq2 = x^2 + y^2 - 4; % Define the initial guess for the variables…
- User
syms x y eq1 = xy - 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') >> NewtonRapsonNonlinear Error using sym/cat>checkDimensions CAT arguments dimensions not consistent.
Error in sym/cat>catMany (line 33) [resz, ranges] = checkDimensions(sz,dim);
Error in sym/cat (line 25) ySym = catMany(dim, args);
Error in sym/horzcat (line 19) ySym = cat(2,args{:});
Error in NewtonRapsonNonlinear (line 11) J = jacobian([eq1, eq2], [x, y]);
fix the errors correctly
- Assistant
syms x y eq1 = xy - 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)];
code% 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; - User
syms x y eq1 = xy - 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') >> NewtonRapsonNonlinear Error using sym/subs Inconsistency between sizes of second and third arguments.
- Assistant
syms x y eq1 = xy - 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 = [eq1; eq2]; f_val = double(subs(f, [x, y], guess));
code% 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_val; % Update the variable values guess = guess + delta.'; % Update the error and iteration counter error = norm(f_val); iter = iter + 1;