Not getting the right output
이전 댓글 표시
Here I'm trying to find the minimum of my function using "gradient method with step splitting". Here my output is not correct. And also code has some warnings and graph isn't correctly plotting.
So my function is : 10*(x)^2 - 4*x*y + 7*(y)^2 - 4*sqrt(5)*(5*x+y) - 16
and the right point of min should be : ( sqrt5 , 0 )
and the min of the function should be -66.
Hope for a little help.
Thanks.
clc;
close all;
clear;
hold on;
Funct = @(vect) 10*vect(1).^2 - 4*vect(1)*vect(2) + 7*vect(2).^2 + 4*sqrt(5)*(5*vect(1)-vect(2)) - 16;
vect= [0 10];
gradientx = @(x,y) 20.*x - 4.*y - 20*sqrt(5);
gradienty = @(x,y) (-4).*x + 14.*y + 4*sqrt(5);
Grad = [gradientx(vect(1),vect(2)) gradienty(vect(1),vect(2))];
e = 0.01;
kappa = 10;
fn=Funct(vect);
abs_grad = norm(Grad);
n = 3;
i = 0;
Fnew=0;
massiv= [vect(1) ;vect(2)];
while(kappa > e)
newvect = vect - kappa*(Grad./abs_grad);
Fnew=Funct(newvect);
if fn - Fnew<0.5*kappa*(abs_grad)
kappa = kappa/2;
else
fn = Fnew;
fimplicit(@(x,y) 10*(x)^2 - 4*x*y + 7*(y)^2 - 4*sqrt(5)*(5*x+y) - 16-fn, 'Color','r')
vect=newvect;
Grad = [gradientx(vect(1),vect(2)) gradienty(vect(1),vect(2))];
abs_grad = norm(Grad);
n=n+2;
i = i + 1;
massiv = [massiv [vect(1);vect(2)]];
end
n = n + 1;
end
grid on;
box on;
title e=0.01;
xlabel('X');
ylabel('Y');
plot(massiv(1,:),massiv(2,:),'b-*');
result = Funct(massiv(:,end));
fprintf('Precision parameter %.4f\n',e);
fprintf('Point of minimum [%.4f ,%.4f]\n', vect(1),vect(2));
fprintf('Min: %10.4f \n', result);
fprintf('Iterations: %d \n',i );
fprintf('Number of calculated objective function values: %d\n\n',n );
댓글 수: 7
Funct = @(vect) 10*vect(1).^2 - 4*vect(1)*vect(2) + 7*vect(2).^2 + 4*sqrt(5)*(5*vect(1)-vect(2)) - 16;
or
@(x,y) 10*(x)^2 - 4*x*y + 7*(y)^2 - 4*sqrt(5)*(5*x+y) - 16
Which function is correct ?
After you decided which one you want to use, check also the gradient.
Janidu
2023년 1월 7일
Janidu
2023년 1월 7일
If you want to call your gradients now with one input argument,
gradientx = @(x) 20*x(1) - 4*x(2) - 20*sqrt(5);
gradienty = @(x) (-4)*x(1) + 14*x(2) + 4*sqrt(5);
it's ok. But then you will also change the computational calls:
Grad = [gradientx(x(1),x(2)) gradienty(x(1),x(2))];
fimplicit only accepts functions of the form f(x,y). Thus you have to change
fimplicit(@(x) Funct (x) - fn, 'Color','r')
accordingly.
And take care that Funct is vectorized.
@(x,y) 10*(x)^2 - 4*x*y + 7*(y)^2 - 4*sqrt(5)*(5*x+y) - 16-fn
is not vectorized.
I wonder why you change all these things in the previous code that worked. Do you like the challenge of mastering errors ?
Janidu
2023년 1월 7일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Gaussian Process Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
