Find the minimum of a function using Steepest Descent Method

I build this code for find the minimum of a function and draw the graph according to the method. But there are some bugs which I cannot solve. I will attach a similar graph also. Hope to have some corrections on this code. )
Thank you.
%steepest descent method
clc
clear
func = @(x) 10.*x(1).^2 - 4.*x(1).*x(2) + 7.*x(2).^2 - 4.* sqrt5 (5.*x(1) - x(2)) - 16 ;
e = 0.01; %accuracy
i=0; %number of iterations
n=0; %number of calculated objective function values
H = [20 -4 ; -4 14];
n = n + 4;
b = [-20*sqrt(5); 4*sqrt(5)];
c = -16 ;
x =[0 10]';
Xmin=-inv(H)*b;
F= x'*H*x/2+x'*b+c;
n = n + 1;
antiGrad= -(H*x+b);
n = n + 2;
norm_aG = norm(antiGrad);
Points(:,i+1)=x;
func(1)=F;
while norm_aG>e
i=i+1;
kappa = norm_aG^2/dot(H*antiGrad,antiGrad);
x=x+kappa*antiGrad;
F= x'*H*x/2+x'*b+c;
n=n+1;
antiGrad= -(H*x+b);
n=n+2;
norm_aG = norm(antiGrad);
Points(:,i+1)=x;
func(i+1)=F;
end
function I = drawGraph(Xmin, Points, func)
X=linspace(Xmin(1)-5,Xmin(1)+5);
Y=linspace(Xmin(2)-5,Xmin(2)+5);
grid on
hold on
box on
xlabel('X');
ylabel('Y');
xmin=min(Points(1,:))-5;
xmax=max(Points(1,:))+5;
ymin=min(Points(2,:))-5;
ymax=max(Points(2,:))+5;
u=linspace(xmin, xmax);
v=linspace(ymin, ymax);
[XX, YY] = meshgrid(u,v);
I = XX.^2.*10 - 4.*XX.*YY + 7.*YY.^2 - 4.*sqrt(5).*(5*XX-YY) - 44;
contour(XX, YY, I, func,'r', 'showText', 'on');
plot(Points(1,:), Points(2, :), 'b-*');
end
fprintf('Precision parameter %.2f\n',e);
Function definitions in a script must appear at the end of the file.
Move all statements after the "drawGraph" function definition to before the first local function definition.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON
fprintf('point of minimum: [%.2f ,%.2f]\n', x(1),x(2));
fprintf('Min: %10.2f \n', F);
fprintf('Iterations: %d \n',i );
fprintf('number of calculated objective function values: %d\n\n',n );

 Réponse acceptée

Your
function I = drawGraph
definition ends at the end after plot(Points(1,:), Points(2, :), 'b-*'); . Your lines displaying the minimum and so on are not within any function definition. Those lines should be moved to before the function statement.

5 commentaires

sorry I tried many times and keep getting more more errors.(
%steepest descent method
func = @(x) 10.*x(1).^2 - 4.*x(1).*x(2) + 7.*x(2).^2 - 4.* sqrt5 (5.*x(1) - x(2)) - 16 ;
e = 0.01; %accuracy
i=0; %number of iterations
n=0; %number of calculated objective function values
H = [20 -4 ; -4 14];
n = n + 4;
b = [-20*sqrt(5); 4*sqrt(5)];
c = -16 ;
x =[0 10]';
Xmin=-inv(H)*b;
F= x'*H*x/2+x'*b+c;
n = n + 1;
antiGrad= -(H*x+b);
n = n + 2;
norm_aG = norm(antiGrad);
Points(:,i+1)=x;
funcvalues(1)=F;
while norm_aG>e
i=i+1;
kappa = norm_aG^2/dot(H*antiGrad,antiGrad);
x=x+kappa*antiGrad;
F= x'*H*x/2+x'*b+c;
n=n+1;
antiGrad= -(H*x+b);
n=n+2;
norm_aG = norm(antiGrad);
Points(:,i+1)=x;
funcvalues(i+1)=F;
end
fprintf('Precision parameter %.2f\n',e);
Precision parameter 0.01
fprintf('point of minimum: [%.2f ,%.2f]\n', x(1),x(2));
point of minimum: [2.24 ,0.00]
fprintf('Min: %10.2f \n', F);
Min: -66.00
fprintf('Iterations: %d \n',i );
Iterations: 8
fprintf('number of calculated objective function values: %d\n\n',n );
number of calculated objective function values: 31
drawGraph(x, Points, funcvalues);
function I = drawGraph(Xmin, Points, func)
X=linspace(Xmin(1)-5,Xmin(1)+5);
Y=linspace(Xmin(2)-5,Xmin(2)+5);
grid on
hold on
box on
xlabel('X');
ylabel('Y');
xmin=min(Points(1,:))-5;
xmax=max(Points(1,:))+5;
ymin=min(Points(2,:))-5;
ymax=max(Points(2,:))+5;
u=linspace(xmin, xmax);
v=linspace(ymin, ymax);
[XX, YY] = meshgrid(u,v);
I = XX.^2.*10 - 4.*XX.*YY + 7.*YY.^2 - 4.*sqrt(5).*(5*XX-YY) - 44;
contour(XX, YY, I, func,'r', 'showText', 'on');
plot(Points(1,:), Points(2, :), 'b-*');
end
ohh thank you very much!! Understood!!
Your func appears to be unused ??
Yes earlier. Now there is a warning in 36th line. But code is running. )

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by