newton Raphson method multivariable with single function

4 vues (au cours des 30 derniers jours)
Thirusabaresaan P
Thirusabaresaan P le 15 Juin 2021
im trying to minimize a function by newtons method with 3 variables but its showing error while solving
the code is
%code 2
% Newton's Method 2-dimension
% Written by thirusabaresaan
% Date: May 14, 2021
clc
clear
format long
% Function Definition (Enter your Function here):
syms X Y Z
f =(3/2)*X^2+(3/2)*Y^2+Z^2-X*Y-10*(X+Y+Z)+75 % equation
% Initial Guess (Choose Initial Guesses):
X0(1) = -5
Y0(1) = 5
Z0(1) = 0
u = 10^(-2) % Convergence Criteria
i = 1 % Iteration Counter
% Gradient and Hessian Computation:
J = subs(gradient(f),[X Y Z],[X0(1) Y0(1) Z0(1)]) % Gradient
H = subs(hessian(f),[X Y Z],[X0(1) Y0(1) Z0(1)]) % Hessian
S = inv(H) % Search Direction
% Optimization Condition:
while (norm(J) > u)
I = [X0(i),Y0(i),Z0(i)]'
x0(i+1) = I(1)-S(1,:)*J
Y0(i+1) = I(2)-S(2,:)*J
Z0(i+1) = I(3)-S(3,:)*J
i = i+1
J = subs(gradient(f),[X Y Z],[X0(i) Y0(i) Z0(i)]) % Gradient updated
H = subs(hessian(f),[X Y Z],[X0(i) Y0(i) Z0(i)]) %% Hessian updated
S = inv(H) % New Search Direction
end
% Result Table:`
Iter = 1:i;
X_coordinate = X0';
Y_coordinate = Y0';
Z_coordinate = Z0';
Iterations = Iter';
T = table(Iterations,X_coordinate,Y_coordinate,Z_coordinate = Z0');
% Output:
fprintf('Initial Objective Function Value: %d\n\n',subs(f,[X,Y,Z], [X0(1),Y0(1),Z0(1)]))
if (norm(J) < u)
fprintf('Minimum succesfully obtained...\n\n')
end
fprintf('Number of Iterations for Convergence: %d\n\n', i)
X0(i)
Y0(i)
Z(i)
subs(f,[X,Y,Z], [X0(i),Y0(i),Z0(i)])
disp(T)
I got error over here
J = subs(jacobian(f),[X Y Z],[X0(i) Y0(i) Z0(i)])
can any one help me to solve this

Réponses (1)

vidyesh
vidyesh le 13 Oct 2023
Hi Thirusabaresaan P,
I understand that you want to minimize a function using Newton-Raphson method, but the code is generating an error while updating the gradient value.
By making the following changes to your code, it should run smoothly without encountering any errors:
  • In the line "x0(i+1) = I(1)-S(1,:)*J," change 'x0' to 'X0':
Original:
x0(i+1) = I(1)-S(1,:)*J
Revised:
X0(i+1) = I(1)-S(1,:)*J
  • In the line "T = table(Iterations,X_coordinate,Y_coordinate,Z_coordinate = Z0');" replace "Z_coordinate = Z0'" with "Z_coordinate":
Original:
T = table(Iterations,X_coordinate,Y_coordinate,Z_coordinate = Z0');
Revised:
T = table(Iterations,X_coordinate,Y_coordinate,Z_coordinate);
  • In the third last line of code, change "Z(i)" to "Z0(i)":
Hope this answer helps.

Catégories

En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by