Find the minimum of a function using Steepest Descent Method
Afficher commentaires plus anciens
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);
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
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
