How can I plot a graph according to the results

This is my code for find the minimum of a function using Newton's Method. Here I need to plot a graph according to the results. But I cannot figure out which parameters to use. Hope for a little help)
This is the example of the graph which I need.
Thank you.
clc
clear
%Newton 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;
i=0;
n=0;
b = [-20*sqrt(5); 4*sqrt(5)];
x =[0 10]';
[H,antiGrad,F]=QF(x(1),x(2));
n = n + 7;
norm_aG = norm(antiGrad);
Xmin=-inv(H)*b;
Points(:,i+1)=x;
funcvalues(1)=F;
while(norm_aG > e)
i=i+1;
del=linsolve(H,antiGrad);
x=x+del;
[H,antiGrad,F]=QF(x(1),x(2));
n=n+7;
norm_aG=norm(antiGrad);
Points(:,i+1)=x;
funcvalues(i+1)=F;
end
fprintf('Precision parameter %.4f\n',e);
Precision parameter 0.0100
fprintf('Point of minimum [%.4f ,%.4f]\n', x(1),x(2));
Point of minimum [2.2361 ,0.0000]
fprintf('Min: %10.4f \n', F);
Min: -66.0000
fprintf('Iterations: %d \n',i );
Iterations: 1
fprintf('Number of calculated objective function values: %d\n\n',n );
Number of calculated objective function values: 14
% drawGraph(Xmin, Points, func);
function [H,antiGrad,I]=QF(x,y)
derivative_x = 20*x - 4*y - 20*sqrt(5);
derivative_y = - 4*x + 14*y + 4*sqrt(5);
antiGrad = -[derivative_x,derivative_y]';
H = [20 -4 ; -4 14];
I = x.^2.*10 + 4.*x.*y + 7.*y.^2 - 4.*sqrt(5).*(5*x-y) - 16;
%contour(x, y, I, func,'r', 'showText', 'on');
%plot(Points(1,:), Points(2, :), 'g-*');
end

2 commentaires

Janidu
Janidu le 5 Jan 2023
I want to plot like above mentioned graph. Yes iteration is one. So there should be one red circle like that.
Torsten
Torsten le 5 Jan 2023
Modifié(e) : Torsten le 5 Jan 2023
I think it will be a rotated ellipse instead of a circle.

Connectez-vous pour commenter.

 Réponse acceptée

Torsten
Torsten le 5 Jan 2023
Modifié(e) : Torsten le 6 Jan 2023
clc
clear
%Newton method
func = @(x,y) 10*x.^2 - 4*x.*y + 7*y.^2 - 4*sqrt(5)*(5*x-y) - 16 ;
e = 0.01;
i=0;
n=0;
b = [-20*sqrt(5); 4*sqrt(5)];
x =[0 10]';
[H,antiGrad,F]=QF(x(1),x(2));
n = n + 7;
norm_aG = norm(antiGrad);
Xmin=-inv(H)*b;
Points(:,i+1)=x;
funcvalues(i+1)=F;
while(norm_aG > e)
i=i+1;
del=linsolve(H,antiGrad);
x=x+del;
[H,antiGrad,F]=QF(x(1),x(2));
n=n+7;
norm_aG=norm(antiGrad);
Points(:,i+1)=x;
funcvalues(i+1)=F;
end
hold on
plot(Points(1,:),Points(2,:))
fimplicit(@(x,y)func(x,y)-funcvalues(1),[-15 15])
hold off
fprintf('Precision parameter %.4f\n',e);
Precision parameter 0.0100
fprintf('Point of minimum [%.4f ,%.4f]\n', x(1),x(2));
Point of minimum [2.2361 ,0.0000]
fprintf('Min: %10.4f \n', F);
Min: -66.0000
fprintf('Iterations: %d \n',i );
Iterations: 1
fprintf('Number of calculated objective function values: %d\n\n',n );
Number of calculated objective function values: 14
% drawGraph(Xmin, Points, func);
function [H,antiGrad,I]=QF(x,y)
derivative_x = 20*x - 4*y - 20*sqrt(5);
derivative_y = - 4*x + 14*y + 4*sqrt(5);
antiGrad = -[derivative_x,derivative_y]';
H = [20 -4 ; -4 14];
I = x.^2.*10 - 4.*x.*y + 7.*y.^2 - 4.*sqrt(5).*(5*x-y) - 16;
%contour(x, y, I, func,'r', 'showText', 'on');
%plot(Points(1,:), Points(2, :), 'g-*');
end

3 commentaires

Janidu
Janidu le 5 Jan 2023
Thank you very much!
I had to correct I in your code:
I = x.^2.*10 - 4.*x.*y + 7.*y.^2 - 4.*sqrt(5).*(5*x-y) - 16;
instead of
I = x.^2.*10 + 4.*x.*y + 7.*y.^2 - 4.*sqrt(5).*(5*x-y) - 16;
Janidu
Janidu le 6 Jan 2023
ahh got it! Thank you very much again!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects 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